Android ListView按项目上的图像

时间:2015-08-31 16:56:40

标签: android listview

我有一个带有uniqe行的ListView。 1个图片和1个TextView连续。 我想按下图片并祝贺所选的listitem位置。 所以这张照片可以作为按钮使用。 我的代码是工作。但它还不错? 如果在运行onItemClick()之后首先运行onClick()方法会发生什么?或者它永远不会发生?

谢谢!

private ListView list;
private int pressedId;  

...

list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        pressedId = position;
                    }
                });

...

@Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.imageView_delete:
            {
                Toast.makeText(this,"id:"+pressedId,Toast.LENGTH_SHORT).show();
                break;
            }

1 个答案:

答案 0 :(得分:0)

public class ExampleAdapter extends ArrayAdapter<ExampleClass> {

    public ServicesChildAdapter(Context context, List<ExampleClass> items) {
        super(context, R.layout.example_layout, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ItemHolder holder;
        final ExampleClass CurrentItem = items.get(position);
        if (convertView== null) {
            LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView= inflater.inflate(R.layout.example_layout, parent, false);
            holder = new ItemHolder();
            holder.ExampleImageView= (ImageView) convertView.findViewById(R.id.ExampleImageView);
            convertView.setTag(holder);
        } else {
            holder = (ItemHolder) convertView.getTag();
        }
        holder.CurrentItem = CurrentItem;

            //TODO: Click listener on the view you want.
            holder.RemoveItemTextView.setOnClickListener(new OnClickListener(){ 
                @Override
                public void onClick(View v) {
                    Log.d(getClass.getSimpleName(), "item was clicked");
                }
            });
        }
        return convertView;
    }
}