gpt4 book ai didi

java - 如何使用游标适配器将数据库中的数据添加到回收器 View ?

转载 作者:行者123 更新时间:2023-12-01 11:08:54 26 4
gpt4 key购买 nike

因此,我尝试使用回收器 View 和游标适配器来实现我的数据库,但出现错误。我不能很好地理解它们,所以我不知道我是否没有正确实现适配器或者是否是其他东西。以下是 java 文件:

MainActivity.java:

public class MainActivity extends AppCompatActivity {

public static final String EXTRA_MESSAGE = "com.nick.mowen.receiptmanager.LOCATION";
public String Places[];
public RecyclerView RV;
private RVAdapter adapter;
Cursor mCursor;
ManagerDatabaseAdapter managerDatabaseAdapter;
List<MainInfo> mainInfo = new ArrayList<MainInfo>();
RecyclerView.LayoutManager layoutManager;
private Context activity;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
managerDatabaseAdapter = new ManagerDatabaseAdapter(this);
setContentView(R.layout.activity_main);
adapter = new RVAdapter(getActivity(), managerDatabaseAdapter.getTheCursor());
RV = (RecyclerView) findViewById(R.id.mainV);
layoutManager = new LinearLayoutManager(getActivity());
RV.setLayoutManager(layoutManager);
RV.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
// getMenuInflater().inflate(R.menu.menu_main, li)
return true;
}

public static void getData () {

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this,SettingsActivity.class);
intent.putExtra(EXTRA_MESSAGE,true);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
return true;
}

return super.onOptionsItemSelected(item);
}

/*@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView userText= (TextView) view;
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}*/

public void addInstance(View view) {
Intent intent = new Intent(this,LocationAdder.class);
intent.putExtra(EXTRA_MESSAGE,true);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
}

public Context getActivity() {
return activity;
}
}

这是回收器 View 适配器:

public class RVAdapter extends Adapter<RVAdapter.MyViewHolder> {

private LayoutInflater inflater;
CursorAdapter mCursorAdapter;
Context mContext;
//Cursor cursor;
//public Cursor mCursor;
MainInfo mainInfo;
ManagerDatabaseAdapter.ManagerHelper managerHelper;
public ManagerDatabaseAdapter managerDatabaseAdapter;
// public List<MainInfo> mainInfoList = Collections.emptyList();

public RVAdapter (Context context, Cursor cursor) {
mContext = context;
mCursorAdapter = new CursorAdapter(mContext, cursor, 0) {
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
//Cursor mCursor = getCursor();

final LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.custom_row, parent, false);

int nameCol = cursor.getColumnIndex("Codes");
String name = cursor.getString(nameCol);

TextView nameText = (TextView) view.findViewById(R.id.mainV);
if (nameText != null) {
nameText.setText(name);
}
return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
int nameCol = cursor.getColumnIndex("Code");
String name = cursor.getString(nameCol);
TextView nameText = (TextView) view.findViewById(R.id.mainV);
if (nameText != null) {
nameText.setText(name);
}
}
};
//this.mainInfoList = mainInfoList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int i) {
/*MainInfo mainInfo = mainInfoList.get(i);
holder.Title.setText(mainInfo.get_Title());
holder.Body.setText(mainInfo.get_SubT());*/
mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
}

@Override
public int getItemCount() {
//return mainInfoList.size();
return mCursorAdapter.getCount();
}

class MyViewHolder extends RecyclerView.ViewHolder {

TextView Title;

MyViewHolder(View itemView) {
super(itemView);
Title = (TextView) itemView.findViewById(R.id.Row_Header);
//Body = (TextView) itemView.findViewById(R.id.Row_Footer);
}
}
}

以下是获取光标的方法:

public Cursor getTheCursor() {
String[] columns = {ManagerHelper.UID,ManagerHelper.NAME,ManagerHelper.CODE};
SQLiteDatabase db = helper.getWritableDatabase();
Cursor mCursor = db.query(ManagerHelper.TABLE_NAME, columns, null, null, null, null, null);
return mCursor;
}

最后这是我收到的错误:

Error

最佳答案

问题在于,在 onCreate 中,您使用了 getActivity() 方法,但从未初始化 Activity 对象。

请尝试将您的 onCreate 更改为如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
managerDatabaseAdapter = new ManagerDatabaseAdapter(this);
setContentView(R.layout.activity_main);
adapter = new RVAdapter(this, managerDatabaseAdapter.getTheCursor());
RV = (RecyclerView) findViewById(R.id.mainV);
layoutManager = new LinearLayoutManager(this);
RV.setLayoutManager(layoutManager);
RV.setAdapter(adapter);
}

这应该可以修复您的 NPE,并且您可以看到还缺少哪些其他部分。并删除 getActivity() 方法,您不需要在 Activity 中将其用于 Fragment,所以我假设您从 Fragment 实现中复制了该代码。

关于java - 如何使用游标适配器将数据库中的数据添加到回收器 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com