【问题标题】:Button to each row in listview Android [duplicate]listview Android中每一行的按钮[重复]
【发布时间】:2014-05-20 12:25:06
【问题描述】:

我有一个使用 ViewHolder 模式的带有自定义适配器的 Listview...它工作正常,但现在我在我的布局中为 listview 中的每一行添加了两个按钮。

当按钮被点击时,我想获取位置(用于获取数据),我实现了 OnClickListener

我该怎么做?

这是我的代码的一部分:

public class SearchListAdapter extends BaseAdapter implements OnClickListener {

public static final String TAG = "SearchListAdapter";

private LayoutInflater inflater;
private ArrayList<VideoYoutube> arrayList;
private ImageLoader imageLoader;
private Activity activity;

public SearchListAdapter(Activity a, ArrayList<VideoYoutube> array){

    activity = a;
    arrayList = array;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}


@Override
public int getCount() {
    return arrayList.size();
}

@Override
public VideoYoutube getItem(int position) {
    return arrayList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder = null;

    //Check if it's on memory
    if(vi == null) {
        //The view is not a recycled one: we have to inflate
        vi = inflater.inflate(R.layout.list_row_search, parent, false);

        holder = new ViewHolder();
        holder.video_title = (TextView)vi.findViewById(R.id.title);
            holder.button_play = (Button)vi.findViewById(R.id.button_play);
        holder.button_show = (Button)vi.findViewById(R.id.button_show);

        holder.button_play.setOnClickListener(this);
        holder.button_show.setOnClickListener(this);

        vi.setTag(holder);
    }
    else {

        // View recycled !
        // no need to inflate
        // no need to findViews by id
        holder = (ViewHolder) vi.getTag();
    }

    Object o = getItem(position);

    //Set video information
    holder.video_title.setText(o.getYtTitle());

    return vi;
}


@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.button_play:
        Log.d(TAG, "Play: " + v.getId());
        break;
    case R.id.button_show:
        Log.d(TAG, "Show: " + v.getId());
        break;
    }
}   

}

【问题讨论】:

    标签: android listview


    【解决方案1】:

    据我了解,您可以使用这个:

    private ListView mListView;
    
    //Method Constructor
    mListView = l;
    
    @Override
    public void onClick(View v) {
    
        int position;
    
        switch(v.getId()){
        case R.id.button_play:
            position = mListView.getPositionForView((View) v.getParent());
            Log.d(TAG, "Clicked: " + position);
    //search in your arrayList with the position that you'll get
    
            break;
        }
    }
    

    【讨论】:

    • 感谢它对我有用!!!
    【解决方案2】:

    您可以为带有所需数据的按钮设置标签,例如项目 ID 或位置。然后你可以在点击监听器中得到你需要的东西,像这样:

    public View getView(int position, View convertView, ViewGroup parent) {
    
        ...
    
        Object o = getItem(position);
    
        //Set video information
        holder.video_title.setText(o.getYtTitle());
    
        holder.button_play.setTag(position);
        holder.button_show.setTag(position);
    
        return vi;
    }
    
    ...
    
    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.button_play:
            Object o = getItem((int) v.getTag())
            Log.d(TAG, "Play: " + o.getYtTitle());
            break;
        case R.id.button_show:
            Object o = getItem((int) v.getTag())
            Log.d(TAG, "Show: " + o.getYtTitle());
            break;
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 2017-09-29
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      相关资源
      最近更新 更多