gpt4 book ai didi

java - 用户向 Recyclerview 添加数据

转载 作者:行者123 更新时间:2023-11-30 01:38:45 25 4
gpt4 key购买 nike

我在将数据添加到我的 recyclerview by user 时遇到了麻烦。我不想对其进行硬编码。我有一个 fab,它打开带有 3 个输入字段的警报对话框,用户应在其中输入 exam_nameexam_descexam_author。这 3 个字段应该写入 3 个不同的字符串,由 ItemsDate 调用。这应该使用用户输入的字符串创建一张新卡。可能我做错了什么,因为我只能硬编码。如果有人能告诉我,我缺少什么,或者我必须在其他地方添加什么,我将非常感激。我是 android 编码的初学者。提前致谢!

这是我的 MainActivity.java

final Context context = this;
protected String Name1 = String.valueOf(R.id.exam_name);
protected String Desc1 = String.valueOf(R.id.exam_desc);
protected String Author1 = String.valueOf(R.id.exam_author);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.cardList);
ItemData itemsData[] = { new ItemData(Name1, Desc1, Author1)
};
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
MyAdapter mAdapter = new MyAdapter(itemsData);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new ScaleInAnimator());

fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LayoutInflater factory = LayoutInflater.from(context);

final View textEntryView = factory.inflate(R.layout.text_entry, null);

final EditText input1 = (EditText) textEntryView.findViewById(R.id.exam_namein);
final EditText input2 = (EditText) textEntryView.findViewById(R.id.exam_descin);
final EditText input3 = (EditText) textEntryView.findViewById(R.id.exam_authorin);
input1.setText("", TextView.BufferType.EDITABLE);
input2.setText("", TextView.BufferType.EDITABLE);
input3.setText("", TextView.BufferType.EDITABLE);

final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setIcon(R.drawable.ic_input_blue_grey_500_24dp).setTitle("Create new exam").setView(textEntryView).setPositiveButton(R.string.save,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {

Name1 = input1.getText().toString();
Desc1 = input2.getText().toString();
Author1 = input3.getText().toString();

Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "New Theme: "+Name1 +" created successful.", Snackbar.LENGTH_LONG);
snackbar.setActionTextColor(Color.RED);
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(getResources().getColor(R.color.colorPrimary));
snackbar.show();
}
}).setNegativeButton(getString(R.string.cancel),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
Snackbar snackbar = Snackbar.make(findViewById(android.R.id.content), "You canceled, theme was not created", Snackbar.LENGTH_SHORT);
// Changing action button text color
View sbView = snackbar.getView();
TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(getResources().getColor(R.color.md_red_500));
snackbar.show();

}
});
alert.show();
}
});
...

这是MyAdapter.java

public ItemData[] itemsData;
public MyAdapter(ItemData[] itemsData) {
this.itemsData = itemsData;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
int viewType) {
View itemLayoutView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card_layout, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
viewHolder.txtViewTitle2.setText(itemsData[position].getDesc());
viewHolder.txtViewTitle3.setText(itemsData[position].getAuthor());
}

@Override
public void onClick(View v) {

}
public static class ViewHolder extends RecyclerView.ViewHolder {

public TextView txtViewTitle;
public TextView txtViewTitle2;
public TextView txtViewTitle3;

public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.exam_name);
txtViewTitle2 = (TextView) itemLayoutView.findViewById(R.id.exam_desc);
txtViewTitle3 = (TextView) itemLayoutView.findViewById(R.id.exam_author);
}
}
@Override
public int getItemCount() {
return itemsData.length;
}
}

这里是ItemData.java

public class ItemData {
private String examname1;
private String examdesc1;
private String examauthor1;

public ItemData(String examname1,String examdesc1, String examauthor1){
this.examname1 = examname1;
this.examdesc1 = examdesc1;
this.examauthor1 = examauthor1;

}
public String getTitle() {
return examname1;
}
public String getDesc() {
return examdesc1;
}
public String getAuthor() {
return examauthor1;
}
}

我认为我的目的不需要这些布局。非常感谢!!!

最佳答案

Arrays不允许您轻松添加数据,我建议更改 itemsDataArrayList<ItemData> .

在您获得 EditText 之后数据并将其保存到Strings ,您需要将其添加到您的数据集 itemsData ,

itemsData.add(new ItemData(Name1, Desc1, Author1));  

然后告诉你的Adapter您的数据已更改

mAdapter.notifyDataSetChanged();

如果你只添加了一个项目,调用

mAdapter.notifyItemInserted(position);

关于java - 用户向 Recyclerview 添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34788548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com