我正在尝试修改Android Studio中的库存主/明细模板。我的主要目标是尝试正确显示简单的List<Obj>
,我在onCreate()
中手动填充。
我的问题是应该从ListActivity
传递给DetailActivity
(然后传递给Fragment)的额外内容不会通过。
完整代码here
我怀疑我修改的RecyclerView适配器(和支架)有问题。我也不喜欢缺货的DummyContent类。
我还会了解你的观点是否最好删除这个DummyContent类并创建自己的数据源,或者在所谓的现实生活中更容易和更方便地修改现有的Dummy内容源。
如果您希望在此处粘贴更多代码段,请告知我们,并提前感谢您的帮助。
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final List<Obj> mValues;
public SimpleItemRecyclerViewAdapter(List<Obj> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.data_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.obj = mValues.get(position);
holder.name.setText(holder.obj.getName());
holder.image.setText(holder.obj.getImage());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(Keys.KLUCZ, holder.obj.getName()); // tutaj musze przeslac Id
DataDetailFragment fragment = new DataDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.data_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, DataDetailActivity.class);
intent.putExtra(Keys.KLUCZ, holder.obj.getName());
Log.d("DataListActivity", "obj.getName = "+holder.obj.getName());
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final TextView name;
public final TextView image;
public final View mView;
public Obj obj;
public ViewHolder(View view) {
super(view);
mView = view;
name = (TextView) view.findViewById(R.id.id);
image = (TextView) view.findViewById(R.id.content);
}
}
接收活动:
if (savedInstanceState == null) {
// Create the detail fragment and add it to the activity
// using a fragment transaction.
Bundle arguments = new Bundle();
arguments.putString(Keys.KLUCZ,
getIntent().getStringExtra(Keys.KLUCZ));
DataDetailFragment fragment = new DataDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.add(R.id.data_detail_container, fragment)
.commit();
}
答案 0 :(得分:0)
自己找到它: 我错误地实例化了我的Obj对象:
是:
mItem = new Obj(Keys.KLUCZ, "some string");
应该是:
mItem = new Obj(getArguments().getString(Keys.KLUCZ), "Jakies nazwisko");