如何从自定义列表中将单击的视图转换为父活动

时间:2013-02-20 10:54:40

标签: android android-arrayadapter

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

我的代码是:

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;  
    }

}
}

This is the image

1 个答案:

答案 0 :(得分:0)

在SampleAdapter中添加:

private DeleteClickListener mDeleteClickListener;

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

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

然后从您implements DeleteClickListener称之为myAdapter.setOnDeleteClickListener(this);的活动开始。 现在在getView()执行此操作:

 delete.setOnClickListener(new OnClickListener() {

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

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