我有一个自定义列表视图,其中包含3个textview和5个imageview(星号)。前两个textview是可见的,第三个是不可见的。我从服务器获取数据并将其放入listview无论多少。
我想要什么
如果我点击任何一个星(1或2或3或4或5)我想从textview获取隐藏的id。点击后,星形图像视图会更改图像。假设我点击第三颗星然后将评定3颗星,其他将被评为未评级。
我做了什么
现在我可以从服务器获取数据并将其放入listview但是假设我点击任何项目的星号,然后最后一个项目的星星改变他们的图像而不是点击的项目。它从隐藏的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获取文本 请给我任何提示或参考
答案 0 :(得分:1)
我在listview存在的Activity中执行此操作,而不是在customadapter中。
adapterListview = new ListArrayAdapter(this, items);
this.listview.setAdapter(adapterListview);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// global variable
idItem = (int) id;
switch (idItem) {
case 0:
// set your text and image for this item
break;
case 1:
// set your text and image for this item
break;
case 2:
// set your text and image for this item
break;
case 3:
// set your text and image for this item
break;
}
}
});
id = 0是第一颗星,id = 1是第二颗星,依此类推......
答案 1 :(得分:0)
可能是你的ListView正在接受触摸事件,并且正在转移到每个孩子。因此所有的星星都被突出显示