android - 如何在另一个活动中访问私有访问方法?
问题描述
我想在另一个活动中使用私有访问方法!。但我不知道该怎么做。或者我还能有什么其他方法。这是 AddSaleActivity 属性,getAllItems () 方法位于其末尾。
public class AddSaleActivity extends AppCompatActivity {
private Button buttonInsert;
private RadioGroup radio_sale_type;
private RadioButton radio_benzin;
private RadioButton radio_gasOil;
private String sale_name;
private SQLiteDatabase mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_sale);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
GasStationDBHelper dbHelper = new GasStationDBHelper(this);
mDatabase = dbHelper.getWritableDatabase();
buttonInsert = findViewById(R.id.button_insert);
radio_sale_type = findViewById(R.id.radio_group_ful_type);
radio_benzin = findViewById(R.id.radio_benzin);
radio_gasOil = findViewById(R.id.radio_gasOil);
buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addItem();
}
});
}
private void addItem(){
if (radio_sale_type.getCheckedRadioButtonId() == -1)
{
Toast.makeText(this, "لطفا نوع سوخت را انتخاب کنید!", Toast.LENGTH_SHORT).show();
return;
}
if (radio_benzin.isChecked()){
sale_name = "فروش بنزین";
}
if (radio_gasOil.isChecked()){
sale_name = "فروش گازوئیل";
}
ContentValues cv = new ContentValues();
cv.put(GasStationContract.GasStationEntry.COLUMN_NAME,sale_name);
mDatabase.insert(GasStationContract.GasStationEntry.TABLE_NAME,null,cv);
radio_sale_type.clearCheck();
Toast.makeText(this, "فروش با موفقیت ثبت شد", Toast.LENGTH_SHORT).show();
finish();
}
private Cursor getAllItems() {
return mDatabase.query(
GasStationContract.GasStationEntry.TABLE_NAME,
null,
null,
null,
null,
null,
GasStationContract.GasStationEntry.COLUMN_TIMESTAMP + " DESC"
);
}}
要创建视图列表,我想在适配器的 HomeFragment 中使用 getAllItems()。但是这个方法有私有访问权限,不能做
public class HomeFragment extends Fragment {
private ArrayList<SaleItem> mSaleItems;
private HomeViewModel homeViewModel;
private RecyclerView mRecyclerView;
private GasStationAdapter mAdapter;
private RecyclerView.LayoutManager mlayoutManager;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
//final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
//textView.setText(s);
}
});
mRecyclerView = root.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mAdapter = new GasStationAdapter(getContext(), AddSaleActivity.getAllItems());
mAdapter.swapCursor(AddSaleActivity.getAllItems());
mRecyclerView.setAdapter(mAdapter);
createSaleList();
//buildRecyclerView();
return root;
}
public void createSaleList(){
mSaleItems = new ArrayList<>();
mSaleItems.add(new SaleItem("فروش بنزین", "1254000 تومان", "12450 لیتر"));
mSaleItems.add(new SaleItem("فروش گازویبل", "175000 تومان", "5000 لیتر"));
mSaleItems.add(new SaleItem("فروش بنزین", "1254000 تومان", "12450 لیتر"));
}
public void buildRecyclerView(){
mRecyclerView.setHasFixedSize(true);
mlayoutManager = new LinearLayoutManager(getContext());
//mAdapter = new SaleAdapter(mSaleItems);
mRecyclerView.setLayoutManager(mlayoutManager);
mRecyclerView.setAdapter(mAdapter);
}}
这是适配器
public class GasStationAdapter extends RecyclerView.Adapter<GasStationAdapter.GasStationViewHolder> {
private Context mContext;
private Cursor mCursor;
public GasStationAdapter(Context context, Cursor cursor) {
mContext = context;
mCursor = cursor;
}
public class GasStationViewHolder extends RecyclerView.ViewHolder {
public TextView sale_nameText;
public GasStationViewHolder(View itemView) {
super(itemView);
sale_nameText = itemView.findViewById(R.id.textView_sale_name);
}
}
@NonNull
@Override
public GasStationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.sale_item, parent, false);
return new GasStationViewHolder(view);
}
@Override
public void onBindViewHolder(GasStationViewHolder holder, int position) {
if (!mCursor.moveToPosition(position)) {
return;
}
String name = mCursor.getString(mCursor.getColumnIndex(GasStationContract.GasStationEntry.COLUMN_NAME));
holder.sale_nameText.setText(name);
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor(Cursor newCursor) {
if (mCursor != null) {
mCursor.close();
}
mCursor = newCursor;
if (newCursor != null) {
notifyDataSetChanged();
}
}
}
解决方案
让你SQLiteOpenHelper
成为单身人士:
class GasStationDBHelper extends SQLiteOpenHelper {
private static volatile sInstance = null;
static synchronized GasStationDBHelper instance(Context context) {
if(sInstance == null)
sInstance = new GasStationDBHelper(context.getApplicationContext());
return sInstance;
}
Cursor getAllItems() {
return getReadableDatabase().query(
GasStationContract.GasStationEntry.TABLE_NAME,
null,
null,
null,
null,
null,
GasStationContract.GasStationEntry.COLUMN_TIMESTAMP + " DESC"
);
}
}
现在,无论您想要什么Cursor
,只需使用GasStationDBHelper.instance(context).getAllItems()
编辑:另外,我不会亲自返回 a Cursor
,但我会将查询结果映射到数据对象,以便您可以控制确保光标正确关闭,即:
List<Item> getAllItems() {
try(Cursor c = getReadableDatabase().query(TABLE_NAME,null,null,null,null,null,COLUMN_TIMESTAMP + " DESC") {
if(c == null || !c.moveToFirst)
return new ArrayList<>();
final List<Item> out = new ArrayList<>(c.getCount());
while(c.moveToNext()) {
out.add(Item.fromCursorRow(c));
}
return out;
}
return new ArrayList<>();
}
这将有助于确保您不会泄漏资源。
推荐阅读
- swiftui - 如何处理 swiftui @State optional unwrap
- python - 如何与 Open CV 中的 BGR 图像像素值进行比较?
- java - 使用@IdClass 休眠一个实体的@ManyToOne
- ios - 为什么这个 SwiftUI 代码会导致编译器超时
- javascript - 在 vuetify 2.x 组件上使用填充高度
- android - 从我的firebase实时数据库中的所有用户获取过滤列表值,其中列表是用户的孩子
- python - 在 pygame 中,我试图让一个球在碰到我的一张图像时消失,我希望不要使用精灵,因为我还没有学过它们
- grammar - 用琶音编写 PEG 递归表达式语法有困难
- java - 我可以更改已签名的 android 项目包名称吗?
- html - 如何以非交互方式连续调整 2 个 Div