0

我有一个带有自定义数组适配器的自定义列表视图。我的问题是在单击编辑图像时,我需要在编辑文本上使用相关文本。我搜索了很多,但我无法得到解决方案。

我的代码是:

public class SimpleAdapter1 extends ArrayAdapter<Data> implements OnClickListener {

private ArrayList<Data> items;
Context context;
TextView tv1;
ImageView edit;
ImageView delete;

public SimpleAdapter1(Context context, int textViewResourceId,
        List<Data> catDesc) {
    super(context, textViewResourceId, catDesc);
    this.items = (ArrayList<Data>) catDesc;
    this.context = context;     
    System.out.println(items);
}

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.masterlist, null);
    }
    Data dt = items.get(position);
    if (dt != null) {
        tv1 = (TextView) v.findViewById(R.id.mastext1);
        if (tv1 != null) {
            tv1.setText(dt.getDesc());
        }
    }
    });
    tv1.setOnClickListener(this);       
    edit = (ImageView) v.findViewById(R.id.editImage);
    delete = (ImageView) v.findViewById(R.id.deleteImage);
    edit.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {

        }
    });
    delete.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            Toast.makeText(context,
                    items.get(position).getDesc() + " delete",
                    Toast.LENGTH_SHORT).show();
        }
    });
    return v;
}

public void onClick(View v) {
    switch (v.getId()) {
    case R.id.mastext1:         
        break;  
    }

}
}

这是图像

4

1 回答 1

0

在您的 SampleAdapter 中添加:

private DeleteClickListener mDeleteClickListener;

public void setOnDeleteClickListener(DeleteClickListener deleteClickListener) {
    mDeleteClickListener = deleteClickListener;
}

public static interface DeleteClickListener {
    public void onDelete(int id);
}

然后从您的活动中implements DeleteClickListener调用 this myAdapter.setOnDeleteClickListener(this);。现在在你getView()这样做:

 delete.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        if(mDeleteClickListener!=null){
             mDeleteClickListener.onDelete(position);
        }
    }
});

PS:编辑按钮的步骤相同。

于 2013-02-20T11:02:07.957 回答