Reputation: 3
I want to show data in recycler view but it only show last added data and not show previously added data i tried for sqlite database but it didn't work so i tried first ststic data but it also show same error . I try googling this but there no work on this problem. i also try notifydatachanged() but it also not worked. here is my adapter code:
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);
}
}
}
this is my activity
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();
}
}
whenever i fetch the data in recylcer view it only show last added data.
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>
for more code check here: https://github.com/AnkitsinhBa2p/test
Upvotes: 0
Views: 96
Reputation: 439
You are using static fields in model class. Due to this you get same data whenever you get item in adapter to set values. Change your model class to this and it should work.
public class files {
private int id;
private String fname;
private String time;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFname() { return fname; }
public void setFname(String fname) {
this.fname = fname;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public files() {
}
public files(int id, String fname, String time) {
this.id = id;
this.fname = fname;
this.time = time;
}
public files(String fname, String time) {
this.fname = fname;
this.time = time;
}
}
Upvotes: 1
Reputation: 129
So the changes are as such In your activity2.java file add this ->
package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class activity2 extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private dbhelper db;
private ArrayList<files> allfiles;
private fileAdapter madapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
RecyclerView recyclerView = findViewById(R.id.rec1);
allfiles = new ArrayList<>();
db = new dbhelper(this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
allfiles.addAll(db.getAllData());
madapter = new fileAdapter(this, allfiles);
recyclerView.setAdapter(madapter);
madapter.notifyDataSetChanged();
}
}
Then in your "file.java" modal class ->
package com.example.test;
public class files {
private int id;
private String fname;
private String time;
public files(String fname, String time) {
this.fname = fname;
this.time = time;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
Create only one constructor which will be used in your db for getting the data.
Now in your "dbhelper.java" class add this ->
public ArrayList<files> getAllData() {
ArrayList<files> filesList = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
filesList.clear(); //Added this line to clear the arraylist before fetching and adding the whole data again. It will make the list duplicate free.
do {
filesList.add(new files(
cursor.getString(1),
cursor.getString(2)
)); // Fetched the data by using the modal class constructor
} while (cursor.moveToNext());
}
cursor.close();
return filesList;
}
The main issue was in the dbhelper's getAllData() function. I modified it by clearing the list at first and then fetched the data by using the constructor of the modal class and correspondingly added that data into the arraylist of the db.
Then lastly into the "fileAdapter.java" class make these following changes to fetch the data properly from the modal class
public fileAdapter(Context context, ArrayList<files> filelist) {
this.context = context;
this.filelist = filelist;
}
@Override
public void onBindViewHolder(@NonNull fileAdapter.ViewHolder viewHolder, int i) {
final files mFile = filelist.get(i);
viewHolder.name.setText(mFile.getFname());
viewHolder.time.setText(mFile.getTime());
viewHolder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "click on item: " + mFile.getFname(), Toast.LENGTH_LONG).show();
}
});
}
And finally it works properly.
Upvotes: 0
Reputation: 129
Just made little changes in your code which are as follows: First go into your activity_activity2.xml file and add this code
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rec1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".activity2" />
Just keep the code clean and simple Then go into its corresponding logical file, which is named as activity2.java and this ->
package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class activity2 extends AppCompatActivity {
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 = findViewById(R.id.rec1);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
db = new dbhelper(this);
allfiles.addAll(db.getAllData());
madapter = new fileAdapter(this, allfiles);
recyclerView.setAdapter(madapter);
madapter.notifyDataSetChanged();
}
}
I made changes in the LinearLayoutManger's function and added a single Loc at the end of code to refresh the data i.e. notifyDataSetChanged().
Now go into the row_file.xml file and added this ->
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:padding="8dp"
android:textColor="@android:color/black"
android:textStyle="bold" />
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Time"
android:padding="8dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
The main issue was over here. You have provided weight_sum attribute to the cardview and given weight to both the textview. i dont know in which orientation you wanted to create the layout. i just removed it and made it "vertical". But if you want then you can do the weight_sum by providing this to the "linear layout" view group which i have added into the cardview.
Note: If you want to render more than one views in any of the orientation then you need to add a viewgroup inside the cardview.
And here comes the part which is "fileAdapter" class. Just add these changes into your code ->
package com.example.test;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
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);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new ViewHolder(
LayoutInflater.from(viewGroup.getContext())
.inflate(
R.layout.row_file,
viewGroup,
false
)
);
}
@Override
public void onBindViewHolder(@NonNull fileAdapter.ViewHolder viewHolder, int i) {
viewHolder.name.setText(files.getFname());
viewHolder.time.setText(files.getTime());
viewHolder.cardView.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();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView name, time;
CardView cardView;
private ViewHolder(View v) {
super(v);
cardView = v.findViewById(R.id.cardView);
name = v.findViewById(R.id.name);
time = v.findViewById(R.id.time);
}
}
}
I have removed the "relative layout" from here as it was not required and made the cardview the parent container and binded it into the "ViewHolder" innner class of the "fileAdapter" class file. Thats all.
I have tested the code at my side and its working fine. If there is any issue at yours just let me know. Thank you.
Upvotes: 0
Reputation: 115
I think you just need to call the
notifyItemRangeInserted
instead of the
notifyDataSetChanged
Example:
private void newdata() {
int currentSize = madapter.getItemCount();
allfiles.add(new files("1","e"));
allfiles.add(new files("2","4"));
allfiles.add(new files("3","5"));
allfiles.add(new files("4","6"));
allfiles.add(new files("5","7"));
madapter.notifyItemRangeInserted(currentSize , allfiles.size() - currentSize);
}
Upvotes: 0
Reputation: 613
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();
}
}
in This File First, you pass blank ArrayList in Adapter and SetAdapter so, here not any record in ArrayList. After setAdapter, you add newData() Method to add five-item to ArrayList and notify the adapter, so you get five items in the list. So, what's an issue? please, describe more...
Upvotes: 0