我有一个列表视图,在列表视图中我需要通过单击列表项上的图像来获取所选项目位置(我在单击该图像时显示图像颜色更改)。我正在获取列表项的位置通过点击图像,但是,一旦点击图像,我在其他列表项目上获得了另外两个颜色更改图像。我无法找到原因,我的列表大小是13项。它突出显示每5个项目图像颜色一次单击。在这里我使用带有drawable的切换按钮来改变图像颜色。请帮帮我。
这是我的代码 这是列表项
上的切换图像<ToggleButton
android:id="@+id/tick_mark"
android:layout_gravity="center"
android:gravity="center"
android:textOff=""
android:textOn=""
android:background="@drawable/selected"
android:layout_width="30sp"
android:layout_height="30sp"
/>
这是我的适配器类
public class Choose_adapter extends BaseAdapter {
ArrayList<Choose_pojo>data;
Activity activity;
LayoutInflater inflater;
LinearLayout more_btn;
public Choose_adapter(Activity context,ArrayList<Choose_pojo> items){
this.data =items;
this.activity =context;
inflater =(LayoutInflater)activity.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view=convertView;
if(convertView==null)
{
view=inflater.inflate(R.layout.choose_diet_row,null);
}
final ToggleButton tick_mark =(ToggleButton)view.findViewById(R.id.tick_mark);
tick_mark.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
IngredientSession.addIndex1(position);
}
});
return view;
}
}
这是我的活动类
public class Choose_activity extends Activity{
Choose_adapter adapter;
ListView lv;
ArrayList<Choose_pojo> list1= Choose_pojo.feedData();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_diet_list);
lv =(ListView)findViewById(R.id.diet_list);
loadInitials();
}
private void loadInitials() {
adapter=new Choose_adapter(this,list1);
lv.setAdapter(adapter);
}
}
这里我使用pojo类Choose_pojo和静态方法
public class Choose_diet_pojo {
String item_name;
double energy_weight;
double fat_weight;
double fiber_wieght;
double protien_weight;
double Ca_wt;
double AvP_wt;
double Sodium_wt;
double Methionine_wt;
double Lysine;
String title;
String mintitle;
public double getEnergy_weight() {
return energy_weight;
}
public void setEnergy_weight(double energy_weight) {
this.energy_weight = energy_weight;
}
public double getFat_weight() {
return fat_weight;
}
public void setFat_weight(double fat_weight) {
this.fat_weight = fat_weight;
}
public double getFiber_wieght() {
return fiber_wieght;
}
public void setFiber_wieght(double fiber_wieght) {
this.fiber_wieght = fiber_wieght;
}
public double getProtien_weight() {
return protien_weight;
}
public void setProtien_weight(double protien_weight) {
this.protien_weight = protien_weight;
}
public double getCa_wt() {
return Ca_wt;
}
public void setCa_wt(double ca_wt) {
Ca_wt = ca_wt;
}
public double getAvP_wt() {
return AvP_wt;
}
public void setAvP_wt(double avP_wt) {
AvP_wt = avP_wt;
}
public double getSodium_wt() {
return Sodium_wt;
}
public void setSodium_wt(double sodium_wt) {
Sodium_wt = sodium_wt;
}
public double getMethionine_wt() {
return Methionine_wt;
}
public void setMethionine_wt(double methionine_wt) {
Methionine_wt = methionine_wt;
}
public double getLysine() {
return Lysine;
}
public void setLysine(double lysine) {
Lysine = lysine;
}
public String getMintitle() {
return mintitle;
}
public void setMintitle(String mintitle) {
this.mintitle = mintitle;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getItem_name() {
return item_name;
}
public void setItem_name(String item_name) {
this.item_name = item_name;
}
public static ArrayList<Choose_pojo> feedData() {
ArrayList<Choose_pojo> list = new ArrayList<Choose_pojo>();
Choose_pojo data1 = new Choose_pojo();
data1.setTitle("abc");
data1.setMintitle("ab");
data1.setEnergy_weight(3373);
data1.setFat_weight(3.5);
data1.setFiber_wieght(1.9);
data1.setProtien_weight(7.5);
data1.setAvP_wt(0.12);
data1.setCa_wt(0.01);
data1.setLysine(0.24);
data1.setMethionine_wt(0.18);
data1.setSodium_wt(0.02);
list.add(data1);
Choose_pojo data2 = new Choose_pojo();
data2.setTitle("pqr");
data2.setMintitle("pq");
data2.setEnergy_weight(1300);
data2.setFat_weight(4.0);
data2.setFiber_wieght(10);
data2.setProtien_weight(14.8);
data2.setAvP_wt(0.38);
data2.setCa_wt(0.14);
data2.setLysine(0.6);
data2.setMethionine_wt(0.2);
data2.setSodium_wt(0.06);
list.add(data2);
return list;
}
}
在上面的数据1中,像我这样的数据2达到数据13