我遇到了同样的问题:onListItemClick is not working for listview?
但答案对我不起作用。这是我的代码:
MyAdapter:
static class ViewHolder {
protected TextView textName;
protected TextView textDate;
protected TextView textTime;
protected TextView textCity;
protected TextView textStreet;
protected TextView textMusic;
protected TextView numVotes;
protected ImageButton BtMap;
protected RatingBar rbFavorite;
protected RatingBar rating;
protected ImageView imgMusic;
protected ImageView imgPic;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if(rowResourceId!=R.layout.search_empty_list) {
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(rowResourceId, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.textName = (TextView) view.findViewById(R.id.textView1);
viewHolder.numVotes = (TextView) view.findViewById(R.id.textView2);
viewHolder.rbFavorite = (RatingBar) view.findViewById(R.id.FavoriteBar);
viewHolder.rating = (RatingBar) view.findViewById(R.id.rateBar);
viewHolder.textDate = (TextView) view.findViewById(R.id.textDatum);
viewHolder.textTime = (TextView) view.findViewById(R.id.textTime);
viewHolder.textCity = (TextView) view.findViewById(R.id.textCity);
viewHolder.textStreet = (TextView) view.findViewById(R.id.textStreet);
viewHolder.textMusic = (TextView) view.findViewById(R.id.textMusic);
viewHolder.imgMusic = (ImageView) view.findViewById(R.id.imageMusic);
viewHolder.imgPic = (ImageView) view.findViewById(R.id.imgPic);
viewHolder.rbFavorite.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float nr, boolean fromUser) {
club element = (club) viewHolder.rbFavorite.getTag();
if(nr>0) {
element.setFavorite(1);
} else {
element.setFavorite(0);
}
}
});
viewHolder.rbFavorite.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
float rating = viewHolder.rbFavorite.getRating();
Bundle bundle = new Bundle();
if(rating>0) {
viewHolder.rbFavorite.setRating(0);
bundle.putString("action", "removeFavorite");
} else {
viewHolder.rbFavorite.setRating(1);
bundle.putString("action", "addFavorite");
}
String user_id="1";
String login="1";
if(Slider.user!=null){
user_id = Slider.user.getId();
login = "0";
}
bundle.putString("param1", user_id);
bundle.putString("param2", list.get(position).getClub_id()+"");
bundle.putString("param3", login);
bundle.putString("param4", "");
bundle.putString("param5", "");
if(user_id!=null && !user_id.equalsIgnoreCase("") && !user_id.equalsIgnoreCase("0")) {
httposts task = new httposts();
task.execute(bundle);
}
}
return true;
}
});
view.setTag(viewHolder);
viewHolder.rbFavorite.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).rbFavorite.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
functions func = new functions();
String open = func.getClubOpening(list.get(position));
if(open.equalsIgnoreCase("close")) {
open = "Geschlossen";
}
holder.textName.setText(list.get(position).getName());
holder.textDate.setText("Heute");
holder.textTime.setText(open);
holder.textCity.setText(list.get(position).getCity());
holder.textStreet.setText(list.get(position).getStreet());
holder.textMusic.setText(list.get(position).getMusic());
holder.numVotes.setText(list.get(position).getNumVotes()+" Bewertungen");
holder.rating.setEnabled(false);
try {
holder.rating.setRating(list.get(position).getRatingStars());
} catch (Exception e) {
}
if(list.get(position).isFavorite()) {
holder.rbFavorite.setRating(1);
} else {
holder.rbFavorite.setRating(0);
}
if (list.get(position).getImage() != null) {
holder.imgPic.setImageBitmap(list.get(position).getImage());
} else {
holder.imgPic.setImageResource(R.drawable.pic1);
}
} else {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(rowResourceId, parent, false);
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText(list.get(0).getName());
}
return view;
}
这是我的row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/shadow" >
<View
android:id="@+id/View05"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/actionbar" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/actionbar_text_color" />
<View
android:id="@+id/View01"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignRight="@+id/imgPic"
android:layout_below="@+id/textDatum"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@color/club_listrow_linecolor" />
<ImageView
android:id="@+id/imgPic"
android:layout_width="fill_parent"
android:layout_height="130dp"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textCity"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:src="@drawable/pic1" />
<TextView
android:id="@+id/textCity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textStreet"
android:layout_alignTop="@+id/View01"
android:layout_marginTop="5dp"
android:text="TextView" />
<TextView
android:id="@+id/textStreet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/View01"
android:layout_alignLeft="@+id/textDatum"
android:layout_marginLeft="100dp"
android:layout_toRightOf="@+id/textDatum"
android:text="TextView"
android:textColor="@color/club_listrow_textcolor" />
<RatingBar
android:id="@+id/rateBar"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignLeft="@+id/imgPic"
android:layout_below="@+id/imgPic"
android:layout_marginTop="40dp"
android:progressDrawable="@drawable/small_ratebar" />
<TextView
android:id="@+id/textDatum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imgPic"
android:layout_below="@+id/View05"
android:layout_marginTop="7dp"
android:text="TextView"
android:textColor="@color/club_listrow_textcolor" />
<TextView
android:id="@+id/textTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imgPic"
android:layout_alignLeft="@+id/imgPic"
android:layout_marginTop="7dp"
android:text="TextView"
android:textColor="@color/club_listrow_textcolor" />
<RatingBar
android:id="@+id/FavoriteBar"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:layout_alignLeft="@+id/textDatum"
android:layout_alignParentTop="true"
android:background="@drawable/actionbar"
android:numStars="1"
android:progressDrawable="@drawable/small_ratebar"
android:stepSize="1" />
<ImageView
android:id="@+id/BtMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/View05"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/imgPic"
android:src="@drawable/map_marker_32" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imgPic"
android:layout_below="@+id/imgPic"
android:layout_marginTop="10dp"
android:src="@drawable/note_32" />
<TextView
android:id="@+id/textMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginLeft="5dp"
android:layout_toLeftOf="@+id/button1"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
<Button
android:id="@+id/button1"
android:layout_width="90dp"
android:layout_height="30dp"
android:layout_alignRight="@+id/View01"
android:layout_below="@id/imgPic"
android:layout_marginTop="6dp"
android:background="@drawable/button"
android:text="@string/events"
android:textColor="@color/button_text"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/rateBar"
android:layout_toRightOf="@+id/rateBar"
android:layout_marginLeft="2dp"
android:text="TextView"
android:textSize="10dp"
android:textColor="@color/club_voteNum" />
有人可以帮助我吗?
答案 0 :(得分:1)
而不是实施onListItemClick
设置convertView后设置convertView.OnClickListner
喜欢
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if(rowResourceId!=R.layout.search_empty_list) {
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(rowResourceId, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.textName = (TextView) view.findViewById(R.id.textView1);
viewHolder.numVotes = (TextView) view.findViewById(R.id.textView2);
viewHolder.rbFavorite = (RatingBar) view.findViewById(R.id.FavoriteBar);
viewHolder.rating = (RatingBar) view.findViewById(R.id.rateBar);
viewHolder.textDate = (TextView) view.findViewById(R.id.textDatum);
viewHolder.textTime = (TextView) view.findViewById(R.id.textTime);
viewHolder.textCity = (TextView) view.findViewById(R.id.textCity);
viewHolder.textStreet = (TextView) view.findViewById(R.id.textStreet);
viewHolder.textMusic = (TextView) view.findViewById(R.id.textMusic);
viewHolder.imgMusic = (ImageView) view.findViewById(R.id.imageMusic);
viewHolder.imgPic = (ImageView) view.findViewById(R.id.imgPic);
viewHolder.rbFavorite.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float nr, boolean fromUser) {
club element = (club) viewHolder.rbFavorite.getTag();
if(nr>0) {
element.setFavorite(1);
} else {
element.setFavorite(0);
}
}
});
viewHolder.rbFavorite.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
float rating = viewHolder.rbFavorite.getRating();
Bundle bundle = new Bundle();
if(rating>0) {
viewHolder.rbFavorite.setRating(0);
bundle.putString("action", "removeFavorite");
} else {
viewHolder.rbFavorite.setRating(1);
bundle.putString("action", "addFavorite");
}
String user_id="1";
String login="1";
if(Slider.user!=null){
user_id = Slider.user.getId();
login = "0";
}
bundle.putString("param1", user_id);
bundle.putString("param2", list.get(position).getClub_id()+"");
bundle.putString("param3", login);
bundle.putString("param4", "");
bundle.putString("param5", "");
if(user_id!=null && !user_id.equalsIgnoreCase("") && !user_id.equalsIgnoreCase("0")) {
httposts task = new httposts();
task.execute(bundle);
}
}
return true;
}
});
view.setTag(viewHolder);
viewHolder.rbFavorite.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).rbFavorite.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
functions func = new functions();
String open = func.getClubOpening(list.get(position));
if(open.equalsIgnoreCase("close")) {
open = "Geschlossen";
}
holder.textName.setText(list.get(position).getName());
holder.textDate.setText("Heute");
holder.textTime.setText(open);
holder.textCity.setText(list.get(position).getCity());
holder.textStreet.setText(list.get(position).getStreet());
holder.textMusic.setText(list.get(position).getMusic());
holder.numVotes.setText(list.get(position).getNumVotes()+" Bewertungen");
holder.rating.setEnabled(false);
try {
holder.rating.setRating(list.get(position).getRatingStars());
} catch (Exception e) {
}
if(list.get(position).isFavorite()) {
holder.rbFavorite.setRating(1);
} else {
holder.rbFavorite.setRating(0);
}
if (list.get(position).getImage() != null) {
holder.imgPic.setImageBitmap(list.get(position).getImage());
} else {
holder.imgPic.setImageResource(R.drawable.pic1);
}
} else {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(rowResourceId, parent, false);
TextView textView1 = (TextView) view.findViewById(R.id.textView1);
textView1.setText(list.get(0).getName());
}
// DO it here
// *****************************************************************************************
// view.OnClickListener
//*****************************************************************************************
return view;
}