2016-07-12 88 views
0

我試圖讓listView具有與listView上的每個項目對應的按鈕。例如,如果我在列表中有一個產品,我想單擊該按鈕並在單擊該按鈕時顯示該特定產品的信息。我如何在適配器上爲我的按鈕添加單擊監聽器,以便它可以根據listview中的每個項目工作?爲listView中的每個項目添加自定義適配器中的按鈕

這是我的自定義陣列適配器。

public class MyAdapter extends BaseAdapter { 
private Context mContext; 
private List<Bean> mList; 

public MyAdapter(Context context,List<Bean> list){ 
    mContext=context; 
    mList=list; 
} 

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

@Override 
public Object getItem(int position) { 
    return mList.get(position); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    //use convertView recycle 
    if(convertView==null){ 
     holder=new ViewHolder(); 
     convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false); 
     holder.textView= (TextView) convertView.findViewById(R.id.textView2); 
     holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2); 
     convertView.setTag(holder); 
    }else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    //set text and url 
    holder.textView.setText(mList.get(position).getText()); 
    Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView); 

    return convertView; 
} 

class ViewHolder{ 
    TextView textView; 
    ImageView imageView; 

} 
} 

這是我的按鈕

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Information" 
    android:id="@+id/button5" 
    android:layout_below="@+id/button4" 
    android:layout_alignRight="@+id/button4" 
    android:layout_alignEnd="@+id/button4" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" /> 
+0

在ListView行中添加按鈕的方式與您添加TextView和ImageView的方式相同。有什麼問題嗎? –

+0

將按鈕添加到您的xml文件content_orders,然後將其添加到自定義適配器ViewHolder中,並且ListView中的每一行都將包含一個按鈕。 – DAVIDBALAS1

回答

1

把按鈕視圖與TextView的和ImageView的的您的行佈局。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    //use convertView recycle 
    if(convertView==null){ 
     holder=new ViewHolder(); 
     convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false); 
     holder.textView= (TextView) convertView.findViewById(R.id.textView2); 
     holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2); 
     convertView.setTag(holder); 
    }else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 
holder.clickableButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       //Code goes here 

      } 
     }); 

    //set text and url 
    holder.textView.setText(mList.get(position).getText()); 
    Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView); 

    return convertView; 
} 
+0

奇怪的是,這個答案被接受,但你宣佈clickableButton並綁定視圖持有人與它。 –

1

您可以將點擊偵聽器添加到getView()方法中的按鈕。然後,您可以使用對「持有人」的引用來自定義所要列舉的項目特定操作。

0

如果您提供的按鈕xml代碼位於content_orders佈局中。然後代碼應該是這樣的

public class MyAdapter extends BaseAdapter { 
private Context mContext; 
private List<Bean> mList; 

public MyAdapter(Context context,List<Bean> list){ 
    mContext=context; 
    mList=list; 
    } 

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

@Override 
public Object getItem(int position) { 
    return mList.get(position); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder holder; 
    //use convertView recycle 
    if(convertView==null){ 
    holder=new ViewHolder(); 
    convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false); 
    holder.textView= (TextView) convertView.findViewById(R.id.textView2); 
    holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2); 
holder.btn= (Button) convertView.findViewById(R.id.button5); 
    convertView.setTag(holder); 
}else{ 
    holder = (ViewHolder) convertView.getTag(); 
} 

//set text and url 
holder.textView.setText(mList.get(position).getText()); 
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(500,500).into(holder.imageView); 

holder.btn.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    // do something 
Toast.makeText(mContext,String.valueOf(position),Toast.LENGTH_SHORT).show(); 
    } 
}); 

    return convertView; 
} 

class ViewHolder{ 
    TextView textView; 
    ImageView imageView; 
    Button btn; 

} 
} 
相關問題