[英]How to add data from a database to a recycler view using a cursor adapter?
So I have tried implementing my database with a recycler view and cursoradapter, but I am getting errors. 因此,我尝试使用回收者视图和cursoradapter实现数据库,但出现错误。 I can not understand them well so I dont know if I did not implement the adapter right or if it is something else.
我不太了解它们,所以我不知道我是不是正确实现了适配器还是其他东西。 Here are the java files:
这是Java文件:
MainActivity.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;
}
}
Here is the recycler view adapter: 这是回收者视图适配器:
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);
}
}
}
Here is the method to get the cursor: 这是获取游标的方法:
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;
}
And finally here is the error I am getting: 最后这是我得到的错误:
The problem is that in onCreate you're using the method getActivity() but you're never initializing the activity object. 问题是在onCreate中,您使用的是方法getActivity(),但从未初始化活动对象。
Please try to change your onCreate to something like this: 请尝试将您的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);
}
That should fix your NPE and you can see what other parts are missing. 那应该可以修复您的NPE,您可以看到缺少的其他部分。 And remove the getActivity() method, you don't need that in an Activity it is used on a Fragment, so I supposed you copied that code from a Fragment implementation.
并删除getActivity()方法,您不需要在Activity中将其用于Fragment,所以我认为您是从Fragment实现中复制了该代码。
If you are running a query with a CursorLoader and you want RecyclerView instead of ListView. 如果您正在使用CursorLoader运行查询,并且想要RecyclerView而不是ListView。
You can try my CursorRecyclerViewAdapter CursorAdapter in RecyclerView 您可以在RecyclerView中尝试我的CursorRecyclerViewAdapter CursorAdapter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.