当我在Recycler视图中添加新数据项(从Activity to activity获取)时,当前项目已丢失。插入新项目后如何不丢失回收站视图中的当前数据? 谢谢。
这是我的代码:
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();
}
}
}
从MainActivity获取数据到此活动,但是当我向RecylerView添加新项目时,现有项目已丢失。
答案 0 :(得分:1)
我相信您希望将class.getResource(path)
设为全局变量,以使其值可在各个活动中使用。在Android术语中,您试图拥有List<GetItemData> getItemDataList
为此,首先创建一个Application类,例如MyApp.java,
getItemDataList
现在,将此类添加为清单
中的应用程序标记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);
}
}
android:name
在MainActivity中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tony.myapp">
<application
android:name=".MyApp"
.....>
然后,您在RecyclerActivity中需要做的就是,
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.
看起来你的Java命名约定已经搞砸了。例如,类和变量名应始终为名词,绝不使用动词。有关详细信息,请参阅Naming Conventions。