如何在基本适配器中按下按钮时添加新项目
Posted
技术标签:
【中文标题】如何在基本适配器中按下按钮时添加新项目【英文标题】:How to add new item on button press in Base Adapter 【发布时间】:2020-03-05 13:17:10 【问题描述】:我正在尝试在按下按钮时创建一个新的 ListView 项目,我想我在某个地方有一些错误,但由于我是这个主题的新手,所以我不知道在哪里。
我尝试过多次重写代码,我尝试过使用 notifyDataSetChanged(); - 它什么都不做 尝试在此处搜索其他主题... 这是我的 MainActivity.java:
public Button btn;
private ListView lv;
private CustomeAdapter customeAdapter;
public ArrayList<EditModel> editModelArrayList;
int populateListMaxNum =3;
int listNumber = populateListMaxNum;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView);
btn = (Button) findViewById(R.id.btn);
editModelArrayList = populateList();
customeAdapter = new CustomeAdapter(this,editModelArrayList);
lv.setAdapter(customeAdapter);
/* TODO activate button */
btn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
addToList();
Toast.makeText(getApplicationContext(), "button", Toast.LENGTH_LONG).show();
);
private ArrayList<EditModel> populateList() //this part works perfectly
ArrayList<EditModel> list = new ArrayList<>();
for(int i = 0; i < populateListMaxNum; i++)
EditModel editModel = new EditModel();
//editModel.setEditTextValue(String.valueOf(i));
list.add(editModel);
return list;
/*TODO make it work = expand */
private void addToList() // this part doesn't work nor it doesn't fail
EditModel editModel = new EditModel();
editModelArrayList.add(editModel);
customeAdapter.notifyDataSetChanged();
这是我的 CustomeAdapter.java 类: 公共类 CustomeAdapter 扩展 BaseAdapter
private Context context;
public static ArrayList<EditModel> editModelArrayList;
public CustomeAdapter(Context context, ArrayList<EditModel> editModelArrayList)
this.context = context;
CustomeAdapter.editModelArrayList = editModelArrayList;
@Override
public int getViewTypeCount()
return getCount();
@Override
public int getItemViewType(int position)
return position;
@Override
public int getCount()
return editModelArrayList.size();
@Override
public Object getItem(int position)
return editModelArrayList.get(position);
@Override
public long getItemId(int position)
return 0;
@Override
public View getView(final int position, View convertView, ViewGroup parent)
final ViewHolder holder;
if (convertView == null)
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.lv_item, null, true);
holder.editText = convertView.findViewById(R.id.editid);
convertView.setTag(holder);
else
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)convertView.getTag();
holder.editText.setText(editModelArrayList.get(position).getEditTextValue());
holder.editText.addTextChangedListener(new TextWatcher()
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2)
editModelArrayList.get(position).setEditTextValue(holder.editText.getText().toString());
@Override
public void afterTextChanged(Editable editable)
);
return convertView;
private class ViewHolder
protected EditText editText;
我希望创建一个新的列表项(EditText + TextView),但没有任何反应(除了 Toast 消息) (经过一些调整,应用程序由于 arrayindexoutofboundsexception length=3 index=3 错误而崩溃,但不在此设置中) 以下是所有必需的文件:https://gist.github.com/Atingas/52778a247a78131e5b8cb0239fa30965
【问题讨论】:
【参考方案1】:lv_item.xml 中的主要线性布局具有 match_parent 高度。尝试将其更改为 wrap_content。似乎一排占据了整个屏幕。
【讨论】:
以上是关于如何在基本适配器中按下按钮时添加新项目的主要内容,如果未能解决你的问题,请参考以下文章
如何检测在 PyQt5 中按下了动态添加的按钮之一? [复制]