Reputation: 21
When I added a new data item (get from Activity to activity) in Recycler view, current item was lost. How to not miss current data in recycler view after insert new item? Thank you.
This is my code:
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RecyclerActivity extends AppCompatActivity implements View.OnClickListener{
private RecyclerView listRecyclerView;
private StaggeredGridLayoutManager gaggeredGridLayoutManager;
private List<GetItemData> getItemDataList = new ArrayList<>();
private Button btnBack;
private MyAdapter myAdapter;
final int[] mfp_photo = {R.drawable.dg, R.drawable.er, R.drawable.ib,
R.drawable.m, R.drawable.md, R.drawable.mk, R.drawable.r, R.drawable.rf,
R.drawable.rn, R.drawable.sm};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
listRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
listRecyclerView.setHasFixedSize(true);
gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
listRecyclerView.setLayoutManager(gaggeredGridLayoutManager);
Bundle bundle = getIntent().getExtras();
GetItemData getItemData = new GetItemData(bundle.getString("Name"), bundle.getString("Age"), bundle.getString("Date of Birth"),
bundle.getString("Address"), bundle.getString("Gender"), bundle.getInt("list_length"));
myAdapter = new MyAdapter(getItemDataList, this);
myAdapter.addItem(getItemData);
listRecyclerView.setAdapter(myAdapter);
btnBack = (Button) findViewById(R.id.buttonBack);
btnBack.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonBack:
Intent intent = new Intent(this, new MainActivity().getClass());
startActivity(intent);
break;
default:break;
}
}
class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView fp_photo;
public TextView fp_name;
public MyViewHolder(View itemView) {
super(itemView);
fp_name = (TextView) itemView.findViewById(R.id.fp_name);
fp_photo = (ImageView) itemView.findViewById(R.id.fp_photo);
}
}
class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context context;
private List<GetItemData> getItemDataList_Adapter;
public MyAdapter(List<GetItemData> getItemDataList_Adapter, Context context) {
super();
this.context = context;
this.getItemDataList_Adapter = getItemDataList_Adapter;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (parent.getTag() != null) {
return (MyViewHolder)(parent.getTag());
} else {
View view = LayoutInflater.from(context).inflate(R.layout.cardview, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
}
@Override
public void onBindViewHolder(MyViewHolder holder,int position) {
Random rand = new Random();
int _rpositon = rand.nextInt(mfp_photo.length);
holder.fp_photo.setImageResource(mfp_photo[position]);
holder.fp_name.setText(getItemDataList_Adapter.get(position).getName());
}
@Override
public int getItemCount() {
return getItemDataList_Adapter.size();
}
public void addItem(GetItemData Data) {
getItemDataList_Adapter.add(Data);
notifyDataSetChanged();
}
}
}
Data is get from MainActivity to this activity, but when I added new item to RecylerView, the existing item was lost.
Upvotes: 1
Views: 260
Reputation: 2451
I believe you are looking to make List<GetItemData> getItemDataList
a global variable such that its value is available across Activities. In android terms, you are trying to have an application context for getItemDataList
For this, first create an Application class say MyApp.java,
public class MyApp extends Application {
private ArrayList<ItemData> dataList;
public ArrayList<ItemData> getDataList() {
return dataList;
}
public void setDataList(ArrayList<ItemData> dataList) {
this.dataList = dataList;
}
public void addData(ItemData data){
if(dataList == null){
dataList = new ArrayList<>();
}
dataList.add(data);
}
}
Now, add this class as android:name
for the application tag in manifest,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tony.myapp">
<application
android:name=".MyApp"
.....>
In the MainActivity:
ItemData itemData = new ItemData("John Doe", "16",
"15 July 2000",
"Address", "Male", 10);
MyApp myApp = (MyApp) getApplicationContext();
myApp.addData(itemData);
// Logic for going to RecyclerActivity here.
// No need to pass getItemData as a bundle.
Then, all you need to do in the RecyclerActivity is,
MyApp myApp = (MyApp) getApplicationContext();
List<ItemData> itemDataList = myApp.getDataList();
myAdapter = new MyAdapter(itemDataList, this);
listRecyclerView.setAdapter(myAdapter);
Looks like you have got your Java naming conventions messed up. For example, class and variable names should always be nouns, never use verbs. Refer Naming Conventions for more info.
Upvotes: 1