0

我有一个列表视图来显示我的自定义布局。
在自定义布局中,我有一个 RatingBar 或任何想法,想为它们设置一个动作!
如何在自定义布局中将 OnClickListener 添加到我的 RatingBar。
我在我的主要活动中设置了 listview 适配器。

adapter = new ListviewBooksList(this, bookList);
lstBook.setAdapter(adapter);

工作并查看信息,但我不明白如何在自定义布局中将 OnClickListener 添加到我的 RatingBar!

public class ListviewBooksList extends BaseAdapter {
static final String KEY_NAME = "bookName";
static final String KEY_FAV = "fav";
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;    
public ListviewBooksList(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
    return data.size();
}
public HashMap<String, String> getItem(int position) {

    HashMap<String, String> nameList = new HashMap<String, String>();
    nameList = data.get(position);
    return nameList;
}
public long getItemId(int position) {
    return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.listview_books, null);
    TextView bookName = (TextView)vi.findViewById(R.id.listveiw_books_name);
    RatingBar fav = (RatingBar)vi.findViewById(R.id.star_fav_books);
    nameList = data.get(position);
    bookName.setText(nameList.get(KEY_NAME));
    fav.setRating(Float.valueOf(nameList.get(KEY_FAV)));
    return vi;
}

}

4

1 回答 1

0

当您为列表项生成视图时,您只需在 getView() 方法中将侦听器添加到 RatingBar。

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.listview_books, null);
    TextView bookName = (TextView)vi.findViewById(R.id.listveiw_books_name);
    RatingBar fav = (RatingBar)vi.findViewById(R.id.star_fav_books);
    nameList = data.get(position);
    bookName.setText(nameList.get(KEY_NAME));
    fav.setRating(Float.valueOf(nameList.get(KEY_FAV)));

    fav.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {   
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
            // action to perform when event is triggered            
        }     
    });

    return vi;
}
于 2013-06-17T21:48:16.510 回答