Android:在 RecyclerView 中获取用户输入和显示
Posted
技术标签:
【中文标题】Android:在 RecyclerView 中获取用户输入和显示【英文标题】:Android : Getting User input and Display in RecyclerView 【发布时间】:2020-12-12 04:22:48 【问题描述】:如何获取用户输入并显示到 Recycler 视图中
在我以前的活动中,我得到用户输入的项目名称和用户在 Gridview 中选择图像和 想要将这些图像和项目名称传递到列表视图中
从 Activity 1 传递用户输入
done.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
String itemname = listname.getText().toString();
if (!TextUtils.isEmpty(listname.getText().toString()))
startActivity(new Intent(getContext(), CheckHome.class).putExtra("data", itemname).putExtra("image", imageRes));
dismiss();
else
Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
);
想要在回收站视图中将用户输入和所选图像显示到 ListView 中
public class CheckHome extends AppCompatActivity
ArrayList listimages;
ArrayList listname;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkslate_home);
// get the reference of RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
// set a LinearLayoutManager with default vertical orientation
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(linearLayoutManager);
// call the constructor of CustomAdapter to send the reference and data to Adapter
CustomAdapter customAdapter = new CustomAdapter(this, listname,listimages);
recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView
private class CustomAdapter extends RecyclerView.Adapter
ArrayList listimages;
ArrayList listname;
Context context;
public CustomAdapter(Context context,ArrayList listimages, ArrayList listname )
this.listimages = listimages;
this.listname = listname;
this.context = context;
@Override
public RecyclerView.ViewHolder onCreateViewHolder( ViewGroup parent, int viewType)
// infalte the item Layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
return vh;
@Override
public void onBindViewHolder( RecyclerView.ViewHolder holder, int position)
//what to Display Here
@Override
public int getItemCount()
return 0;
private class MyViewHolder extends RecyclerView.ViewHolder
// init the item view's
TextView name;
ImageView image;
TextView listcount;
public MyViewHolder(View itemView)
super(itemView);
// get the reference of item view's
name = (TextView) itemView.findViewById(R.id.list_name);
image = (ImageView) itemView.findViewById(R.id.image_list);
listcount=itemView.findViewById(R.id.list_count);
【问题讨论】:
【参考方案1】:在您的 Activity OnCreate() 方法中,您可以访问通过 Intent 传递的数据,如下所示:
Bundle bundle = getIntent().getExtras();
if (bundle != null)
String itemName= bundle.getString("data");
这里我得到的是字符串,但你也可以传递字符串数组。您可以将此值传递给您的 RecyclerAdapter 类:
CustomAdapter customAdapter = new CustomAdapter(this, listname,listimages);
但不要通过意图传递大量数据。
【讨论】:
想要在 Oncreate() 中传递 Intent 值那么我们需要在 onBindViewHolder 上传递什么 你必须先创建一个数据模型类。请参阅此处的示例:journaldev.com/10024/…【参考方案2】:getItemCount() 也不应该为 0。你必须传递一些大小给它。
喜欢,modelList.size()
【讨论】:
以上是关于Android:在 RecyclerView 中获取用户输入和显示的主要内容,如果未能解决你的问题,请参考以下文章
Android:在recyclerview下方显示按钮,但始终在屏幕上
android: ScrollView 内的 RecyclerView