我想在回收者视图中显示数据,但它仅显示最后添加的数据,而不显示以前添加的数据我尝试了sqlite数据库,但是它没有用,所以我尝试了第一个ststic数据,但是它也显示了同样的错误。我尝试使用Google搜索,但是在此问题上没有任何工作。我也尝试notifydatachanged(),但也没有用。这是我的适配器代码:
public class fileAdapter extends RecyclerView.Adapter<fileAdapter.ViewHolder> {
private final Context context;
private ArrayList<files> filelist;
private dbhelper mdb;
public fileAdapter(Context context, ArrayList<files> filelist){
this.context=context;
this.filelist=filelist;
mdb=new dbhelper(context);
}
@Override
public ViewHolder onCreateViewHolder( ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_file, viewGroup, false);
ViewHolder holder=new ViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(fileAdapter.ViewHolder viewHolder, int i) {
final files file=filelist.get(i);
viewHolder.name.setText(file.getFname());
viewHolder.time.setText(file.getTime());
ViewHolder.relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"click on item: "+files.getFname(),Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return filelist.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView name,time;
public static RelativeLayout relativeLayout;
public ViewHolder(View v) {
super(v);
name=(TextView)v.findViewById(R.id.name);
time=(TextView)v.findViewById(R.id.time);
relativeLayout=(RelativeLayout)v.findViewById(R.id.listraw);
}
}
}
这是我的活动
private static final String TAG = MainActivity.class.getSimpleName();
private dbhelper db;
private ArrayList<files> allfiles=new ArrayList<>();
private fileAdapter madapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rec1);
RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
madapter = new fileAdapter(this,allfiles);
recyclerView.setAdapter(madapter);
newdata();
}
private void newdata() {
files file=new files("1","e");
allfiles.add(file);
file=new files("2","4");
allfiles.add(file);
file=new files("3","5");
allfiles.add(file);
file=new files("4","6");
allfiles.add(file);
file=new files("5","7");
allfiles.add(file);
madapter.notifyDataSetChanged();
}
}
每当我在Recylcer视图中获取数据时,它仅显示最后添加的数据。
activiy.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".activity2">
<android.support.v7.widget.RecyclerView
android:id="@+id/rec1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
/>
list_raw.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listraw"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="10dp"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_marginEnd="50dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/time"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
尝试使用这种方式添加数据:
private static final String TAG = MainActivity.class.getSimpleName();
private dbhelper db;
private ArrayList<files> allfiles=new ArrayList<>();
private fileAdapter madapter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.rec1);
RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
madapter = new fileAdapter(this,allfiles);
recyclerView.setAdapter(madapter);
newdata();
}
private void newdata() {
files file=new files("1","e");
allfiles.add(file);
file=new files("2","4");
allfiles.add(file);
file=new files("3","5");
allfiles.add(file);
file=new files("4","6");
allfiles.add(file);
file=new files("5","7");
allfiles.add(file);
madapter.notifyDataSetChanged();
}
}