我有一个ListView
的自定义布局。此布局包含EditText
和Button
以删除该行。
我希望用户能够将项目添加到列表中(单击外部按钮)或从列表中删除项目(单击行的按钮)。为了能够删除某一行(我需要它的位置),我决定实现OnClickListener
,我将项目位置存储为变量。
问题是,一旦我删除了一行,以下所有行的侦听器都会停止工作。为什么?
由于我必须在用户决定完成后检索EditText
的值,我决定实现自定义适配器并将行存储在其中(ArrayList)。
这是我的活动:
private ListView mList;
private TemplateFieldsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_template);
customFieldsLayout = (LinearLayout) findViewById(R.id.customFieldsContent);
numFields = 0;
childs = new ArrayList<View>();
// mDragListen = new MyDragEventListener();
mList = (ListView) findViewById(R.id.customFieldsList);
adapter = new TemplateFieldsAdapter(this);
mList.setAdapter(adapter);
mList.setClickable(false);
}
public void AddField(View v){
adapter.addItem();
adapter.notifyDataSetChanged();
}
这是我的适配器:
public class TemplateFieldsAdapter extends BaseAdapter{
private LayoutInflater inflater;
private ArrayList<View> rows;
private int count;
private Context mContext;
public TemplateFieldsAdapter(Context context){
mContext = context;
this.inflater = LayoutInflater.from(context);
rows = new ArrayList<View>();
count = 0;
}
@Override
public int getCount() {
return count;
}
@Override
public View getItem(int position) {
return rows.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(rows.size() <= position || rows.get(position) == null){ // If the View is not cached
// Inflates the Common View from XML file
rows.add(position, this.inflater.inflate(R.layout.template_field, null));
rows.get(position).findViewById(R.id.removeImg).setOnClickListener(new DeleteRow(position));
}
return rows.get(position);
}
public void addItem(){
count ++;
}
private class DeleteRow implements View.OnClickListener{
private int position;
public DeleteRow(int pos){
this.position = pos;
}
@Override
public void onClick(View v) {
rows.remove(position);
count --;
notifyDataSetChanged();
}
}
}
我知道我应该在删除项目时更新侦听器(因为位置发生了变化),但我删除了代码以便更容易理解。
如果我添加4个项目然后我删除位置1的项目位置2和3的监听器(现在它们位于位置1和2)停止工作,则不会调用onClick函数。有谁知道如何解决它?
谢谢!
[编辑]我刚发现了一些有趣的东西。在上面的例子中,我说没有调用onClick函数,事实并非如此。但是,如果那时我单击外部按钮(添加新项目)所有未在调用时调用的onClicks 。这真的很奇怪。 [/编辑]
答案 0 :(得分:0)
使用删除
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = null;
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.one_result_details_row, parent, false);
// inflate other items here :
Button deleteButton = (Button) row.findViewById(R.id.Details_Button01);
deleteButton.setTag(position);
deleteButton.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
Integer index = (Integer) view.getTag();
items.remove(index.intValue());
notifyDataSetChanged();
}
}
);
答案 1 :(得分:0)
我找到解决问题的唯一方法是每次调用getView时创建视图:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(rows.size() <= position || rows.get(position) == null){ // If the View is not cached
rows.add(position, this.inflater.inflate(R.layout.template_field, null));
}
else{
View newView = this.inflater.inflate(R.layout.template_field, null);
((TextView)newView.findViewById(R.id.etFieldName)).setText(((TextView)rows.get(position).findViewById(R.id.etFieldName)).getText());
((TextView)newView.findViewById(R.id.etFieldUnit)).setText(((TextView)rows.get(position).findViewById(R.id.etFieldUnit)).getText());
rows.remove(position);
rows.add(position, newView);
}
rows.get(position).findViewById(R.id.removeImg).setTag(position);
rows.get(position).findViewById(R.id.removeImg).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Integer index = (Integer) v.getTag();
rows.remove(index.intValue());
count --;
mContext.updateList();
}
});
rows.get(position).findViewById(R.id.etFieldName).requestFocus();
return rows.get(position);
}