我看到了与我的问题类似的所有问题(在this,this,this和this链接中)
我的Activity中有myAdapter.notifyDataSetChanged(),但它不起作用
我有3个班级,
我在ThirdActivity中做了什么:
我有TextBox来获取数据并将其存储在Database和Button中。在 Button的Onclicklistener,我指定了代码
- 从文本框中获取文字
- 使用DBHelper
将其添加到表中- 从DBHelper
中将数据作为ArrayList进行检索- myAdapter.notifyDataSetChanged()
当我点击按钮时,我获取LogCat中的数据我在OnclickListener中指定但是没有反映到列表视图。
这是我的代码,
ThirdActivity:
public class ThirdActivity extends AppCompatActivity{
private DrawerLayout mDrawerLayout;
DbHelper dbHelper;
EditText et;
Button addButton;
RecyclerView rv;
ArrayList<String> myNotesList;
SimpleRecycler3Adapter adapter3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.thirdactivity);
myNotesList = new ArrayList<>();
et=(EditText) findViewById(R.id.et);
addButton=(Button)findViewById(R.id.addButton);
rv = (RecyclerView) findViewById(R.id.dbListrv);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getBaseContext());
rv.setLayoutManager(linearLayoutManager);
rv.setHasFixedSize(true);
adapter3 = new SimpleRecycler3Adapter(myNotesList);
rv.setAdapter(adapter3);
dbHelper = new DbHelper(this, null, null, 1);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("DB", "Constructor");
String note=et.getText().toString();
dbHelper.addNote(note);
printData();
}
});
}
public void printData(){
Log.d("DB","Constructor");
myNotesList=dbHelper.databasetostring();
Log.d("DB","Data came"+myNotesList.get(myNotesList.size()-1));
// adapter3 = new SimpleRecycler3Adapter(myNotesList);
// rv.setAdapter(adapter3);
adapter3.notifyDataSetChanged();
}
}
SimpleRecyclerViewAdapter:
public class SimpleRecycler3Adapter extends RecyclerView.Adapter<SimpleRecycler3Adapter.NotesHolder> {
private ArrayList<String> myNotesList=new ArrayList<String>();
String TAG="ThirdAdapter kbt";
RecyclerView rv;
public SimpleRecycler3Adapter(ArrayList<String> myList) {
Log.d(TAG,"Constructor");
Log.d(TAG,"Not null");
int i = 0;
while (i < myNotesList.size()) {
myNotesList.add(myList.get(i).toString());
}
Log.d(TAG,"finish");
}
@Override
public NotesHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Log.d(TAG,"On create started");
View view2 = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerlist3_item, viewGroup, false);
Log.d(TAG,"ADAP STAR ONCR second switch 2nd line");
NotesHolder viewHolder2 = new NotesHolder(view2);
Log.d(TAG,"ADAP STAR ONCR second switch 3nd line");
return viewHolder2;
}
@Override
public void onBindViewHolder(NotesHolder notesHolder, int i) {
Log.d(TAG, "ONBIND SECOND i value is " + i);
// notesHolder.thumbnail.setImageResource(R.drawable.placeholder);
notesHolder.dblistitem.setText(myNotesList.get(i));
Log.d(TAG,"ONBIND second title issssss");
}
@Override
public int getItemCount() {
return myNotesList.size();
}
class NotesHolder extends RecyclerView.ViewHolder {
protected ImageView thumbnail;
protected TextView dblistitem;
public NotesHolder(View itemView) {
super(itemView);
Log.d(TAG, "JSON Inside HOLDER");
rv=(RecyclerView)itemView.findViewById(R.id.dbListrv);
// thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
dblistitem = (TextView) itemView.findViewById(R.id.dblistitem);
}
}
}
答案 0 :(得分:2)
您没有更新适配器类中但在活动类中的myNotesList。但适配器使用它的本地myNotesList。
因此,在按钮单击时,使用可用的最新数据更新适配器的myNotesList并通知适配器。
修改强>
将最新数据传递给适配器。在适配器类中使用此方法并在notifyDataSetChanged()之前调用此方法;
public void updateNotes(ArrayList<String> notesList) {
myNotesList = notesList;
}
答案 1 :(得分:1)
1.在将适配器设置为listview后,您正在初始化dbhelper,因此它最初不能包含任何数据
2.更新回收者视图数据列表的步骤如下
myNotesList.clear();
myNotesList.addAll(dbHelper.databasetostring());
adapter3.notifyDataSetChanged();
答案 2 :(得分:1)
您的 SimpleRecyclerViewAdapter 出现问题,只需更改此内容:
while (i < myNotesList.size()) {
myNotesList.add(myList.get(i).toString());
}
为此:
myNotesList = myList;
在活动的printData()
更改中
myNotesList=dbHelper.databasetostring();
为此:
myNotesList.clear();
myNotesList.addAll(dbHelper.databasetostring());
adapter3.notifyDataSetChanged();
<强>解释强>
首先初始化myNotesList
变量:
myNotesList = new ArrayList<>();
然后您初始化adapter3
adapter3 = new SimpleRecycler3Adapter(myNotesList);
但您的适配器未保存引用,而是将其数据复制到另一个变量中:
while (i < myNotesList.size()) {
myNotesList.add(myList.get(i).toString());
}
这样做,如果您更改myNotesList
中的activity
变量,则不会修改您的适配器数据集。
在方法printData()
中,您更改了myNotesList
变量。哪个不会触及您的适配器或其数据
public void printData(){
Log.d("DB","Constructor");
myNotesList=dbHelper.databasetostring();
Log.d("DB","Data came"+myNotesList.get(myNotesList.size()-1));
// adapter3 = new SimpleRecycler3Adapter(myNotesList);
// rv.setAdapter(adapter3);
adapter3.notifyDataSetChanged();
}
答案 3 :(得分:0)
您无法通过更改myList来更改myNotesList。
public SimpleRecycler3Adapter(ArrayList<String> myList) {
Log.d(TAG,"Constructor");
Log.d(TAG,"Not null");
// int i = 0;
// while (i < myNotesList.size()) {
// myNotesList.add(myList.get(i).toString());
// }
this.myNotesList = myList;
Log.d(TAG,"finish");
}
答案 4 :(得分:0)