我有一個自定義列表視圖與3 textview和5 imageview(明星)。前兩個textview是可見的,第三個是不可見的。我從服務器獲取數據並將其放入listview中,無論多少。onClick上的ListView和ListItem行爲
我想
如果我對任何明星單擊(1或2或3或4或5)我想從TextView的是隱藏得ID。並點擊它後明星imageview更改其圖像。假設我點擊第三顆星,那麼3顆星將被評級,其他評級將被降級。
我做什麼
現在我能夠從服務器獲取數據,並把它放到列表視圖但假設我再單擊最後一個項目的明星在改變,而不是點擊項目自身形象的任何項目的明星。並且它從隱藏的textview中獲取最後的id。
這是我的代碼。
public class MyArrayAdapter extends ArrayAdapter<Student> {
StudentWrapper StudentWrapper = null;
Context context;
int layoutResourceId;
Student student;
ArrayList<Student> students = new ArrayList<Student>();
public MyArrayAdapter(Context context, int layoutResourceId,ArrayList<Student> studs) {
super(context, layoutResourceId, studs);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.students = studs;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
if (item == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
item = inflater.inflate(layoutResourceId, parent, false);
StudentWrapper = new StudentWrapper();
StudentWrapper.menuname = (TextView) item.findViewById(R.id.title);
StudentWrapper.hotelname = (TextView) item.findViewById(R.id.artist);
StudentWrapper.menuid = (TextView) item.findViewById(R.id.tvhotelid);
StudentWrapper.firstStar = (ImageView)item.findViewById(R.id.hotelListFirstStar);
StudentWrapper.secondStar = (ImageView)item.findViewById(R.id.hotelListSecondStar);
StudentWrapper.thirdStar = (ImageView)item.findViewById(R.id.hotelListThirdStar);
StudentWrapper.fourthStar = (ImageView)item.findViewById(R.id.hotelListFourthStar);
StudentWrapper.fifthStar = (ImageView)item.findViewById(R.id.hotelListFifthStar);
StudentWrapper.firstStar.setFocusable(false);
StudentWrapper.secondStar.setFocusable(false);
StudentWrapper.thirdStar.setFocusable(false);
StudentWrapper.fourthStar.setFocusable(false);
StudentWrapper.fifthStar.setFocusable(false);
item.setTag(StudentWrapper);
} else {
StudentWrapper = (StudentWrapper) item.getTag();
}
student = students.get(position);
StudentWrapper.menuname.setText(student.getMenuName());
StudentWrapper.hotelname.setText(student.gethotenName());
StudentWrapper.menuid.setText(String.valueOf(student.getMenuId()));
StudentWrapper.fifthStar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "Edit", Toast.LENGTH_LONG).show();
StudentWrapper.firstStar.setImageResource(R.drawable.rated);
StudentWrapper.secondStar.setImageResource(R.drawable.rated);
StudentWrapper.thirdStar.setImageResource(R.drawable.rated);
StudentWrapper.fourthStar.setImageResource(R.drawable.rated);
StudentWrapper.fifthStar.setImageResource(R.drawable.rated);
}
});
StudentWrapper.secondStar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StudentWrapper.firstStar.setImageResource(R.drawable.rated);
StudentWrapper.secondStar.setImageResource(R.drawable.rated);
StudentWrapper.thirdStar.setImageResource(R.drawable.unrated);
StudentWrapper.fourthStar.setImageResource(R.drawable.unrated);
StudentWrapper.fifthStar.setImageResource(R.drawable.unrated);
Toast.makeText(context, "Delete"+student.getMenuId(), Toast.LENGTH_LONG).show();
}
});
StudentWrapper.firstStar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StudentWrapper.firstStar.setImageResource(R.drawable.rated);
StudentWrapper.secondStar.setImageResource(R.drawable.unrated);
StudentWrapper.thirdStar.setImageResource(R.drawable.unrated);
StudentWrapper.fourthStar.setImageResource(R.drawable.unrated);
StudentWrapper.fifthStar.setImageResource(R.drawable.unrated);
Toast.makeText(context, "Delete"+student.getMenuId(), Toast.LENGTH_LONG).show();
}
});
StudentWrapper.fourthStar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StudentWrapper.firstStar.setImageResource(R.drawable.rated);
StudentWrapper.secondStar.setImageResource(R.drawable.rated);
StudentWrapper.thirdStar.setImageResource(R.drawable.rated);
StudentWrapper.fourthStar.setImageResource(R.drawable.rated);
StudentWrapper.fifthStar.setImageResource(R.drawable.unrated);
Toast.makeText(context, "Delete"+student.getMenuId(), Toast.LENGTH_LONG).show();
}
});
StudentWrapper.thirdStar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StudentWrapper.firstStar.setImageResource(R.drawable.rated);
StudentWrapper.secondStar.setImageResource(R.drawable.rated);
StudentWrapper.thirdStar.setImageResource(R.drawable.rated);
StudentWrapper.fourthStar.setImageResource(R.drawable.unrated);
StudentWrapper.fifthStar.setImageResource(R.drawable.unrated);
Toast.makeText(context, "Delete"+student.getMenuId(), Toast.LENGTH_LONG).show();
}
});
return item;
}
static class StudentWrapper {
TextView menuid;
TextView menuname;
TextView hotelname;
ImageView firstStar;
ImageView secondStar;
ImageView thirdStar;
ImageView fourthStar;
ImageView fifthStar;
}
}
如果我點擊任何項目,然後它反映了在最後一個項目的任何明星。我想更改該項目的imageview,並想從該項目的textview中獲取文本。
請給我任何暗示或引用