我有一个ListView,我设置了一个警报对话框,当setText单击按钮时将文本更改为新的Edittext,然后在alertdialog上为此编辑文本设置setView
当我单击按钮AlertDialog显示时,我可以更改列表中的文本
但是当我滚动page时,将文本改回我在MainActivity中设置的文本
我的适配器代码
public class AdapterItem extends BaseAdapter {
private Context mContext;
private ArrayList<ItemModel> mItemModels;
public AdapterItem(Context context, ArrayList<ItemModel> list) {
this.mContext = context;
this.mItemModels = list;
}
@Override
public int getCount() {
return mItemModels.size();
}
@Override
public Object getItem(int position) {
return mItemModels.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(mContext).inflate(R.layout.item_simple_listview, viewGroup, false);
}
ItemModel item = (ItemModel) getItem(position);
ImageButton imageButton = view.findViewById(R.id.imageButton);
TextView text1 = view.findViewById(R.id.textViewONe);
ImageView image = view.findViewById(R.id.imageViewItem);
text1.setText(item.getTextOne());
image.setImageResource(item.getImageView());
final View finalView = view;
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText userinput = new EditText(mContext);
new AlertDialog.Builder(mContext).setTitle("Change Text")
.setMessage("Input Your Own Text").setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView text1 = finalView.findViewById(R.id.textViewONe);
text1.setText(userinput.getText());
}
})
.setCancelable(true).setView(userinput).create().show();
}
});
return view;
}}
答案 0 :(得分:1)
更改modelItem
的内容并在适配器上调用notiDataSetChanged()
。好吧你可以做
imageButton.setTag(position); // set tag the position
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int post = (int) v.getTag(); // get position
final EditText userinput = new EditText(mContext);
new AlertDialog.Builder(mContext).setTitle("Change Text")
.setMessage("Input Your Own Text").setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ItemModel itemModel = mItemModels.get(post);
itemModel.setTextOne(userinput.getText().toString()); // change the data set at the position
notifyDataSetChanged(); // refresh adapter
}
})
.setCancelable(true).setView(userinput).create().show();
}
});
一些建议:切换到listview的recyclerview或至少实现viewholder模式。