通过按行位置单击列表视图中的不同按钮来执行不同的事件

时间:2014-03-19 19:29:12

标签: android listview button

我有一个带有自定义适配器的listview,如下所示: enter image description here

我想为listview中的每个按钮设置不同的操作,例如按列表视图中的位置切换。我怎么能这样做?

这是我的自定义适配器:

public class Customadapter extends ArrayAdapter<Formazionicontainer>{

Context context;

public Customadapter(Context context, int resourceId,
        List<Formazionicontainer> items) {
    super(context, resourceId, items);
    this.context = context;
}

/*private view holder class*/
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    Formazionicontainer rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_group, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.text1);
        holder.imageView = (ImageView) convertView.findViewById(R.id.image1);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();


    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

2 个答案:

答案 0 :(得分:0)

假设您有一个传递给列表视图适配器的数组:

String[] values = {"Inter","Juventus"};

然后你可以做类似的事情:

listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                       switch(position){
                          case 0: // Inter
                                  break;
                          case 1: // Juventus
                                  break;
                       }
        }
    });

<强> //修改

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        Formazionicontainer rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_group, null);
        holder = new ViewHolder();
        holder.button = (Button) convertView.findViewById(R.id.button1);
        holder.button.setOnClickListener(holder);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.text1);
        holder.imageView = (ImageView) convertView.findViewById(R.id.image1);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.item = rowItem;
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    /* you can also add on click listener this way to your views
    holder.imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(YourActivityName.this,
                "I am here at position: " + position,
                Toast.LENGTH_LONG).show();
    }
});
*/

    return convertView;
}

private class ViewHolder implements View.OnClickListener {
    Button button;
    Formazionicontainer item;
    TextView txtTitle;
    ImageView imageView;

    @Override
    public void onClick(View view) {
        // Your things here
    }
}

答案 1 :(得分:0)

一种解决方案是使用ViewHolder作为Button click listener:

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    Formazionicontainer rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_group, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.text1);
        holder.imageView = (ImageView) convertView.findViewById(R.id.image1);
        holder.button = (ImageView) convertView.findViewById(R.id.button1);
        // Set the ViewHolder to handle the Button click
        holder.button.setOnClickListener(holder);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());
    // Update the holder item
    holder.item = rowItem;

    return convertView;
}

private class ViewHolder implements View.OnClickListener {
    TextView txtTitle;
    ImageView imageView;
    Button button;
    Formazionicontainer item;

    @Override
    public void onClick(View view) {
        // Handle the button click here...
        // The item attribute contains the information about the Button's item clicked.
    }
}