2017-07-14 66 views
0

在我的應用程序中,我顯示列表視圖中的一些項目,每行有一個名稱和一個刻度圖像。勾選圖像只顯示當選擇項目(選中),選擇工作正常,但問題是,當一個新的項目被選中時,先前選擇的項目的勾號不會不可見。這可以如何解決?任何幫助將這裏瞭解 是我的代碼列表視圖顯示以前的選擇[ANDROID]

適配器

public class ListAdapter extends BaseAdapter { 
    private LayoutInflater inflater; 

    Context context; 
    private ArrayList<String> responseList; 

    private LinearLayout parent_choice_list_container; 
    private TextView item_name; 
    private ImageView iv_choice_list_item_selected; 
     public ListAdapter (Context context, ArrayList<String> responseList) { 
     this.context = context; 
     this.responseList = responseList; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    public void setSelectedIndex(int ind) { 
     selectedIndex = ind; 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getCount() { 
     return responseList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return position; 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (view == null) { 
      view = inflater.inflate(R.layout.menu_list_item, null); 
     } 
     parent_choice_list_container = (LinearLayout) view.findViewById(R.id.parent_choice_list_container); 
     item_name = (TextView) view.findViewById(R.id.item_name); 
     iv_choice_list_item_selected = (ImageView) view.findViewById(R.id.iv_choice_list_item_selected); 
     iv_logo = (ImageView) view.findViewById(R.id.iv_blog_category_logo); 
     if (responseList.get(position).isSelected()) { 
      iv_choice_list_item_selected.setVisibility(View.VISIBLE); 
     } else { 
      iv_choice_list_item_selected.setVisibility(View.GONE); 
     }   list_item_name.setText(responseList.get(position).getName()); 
     return view; 
    } 
} 

FragmentPart(項目點擊監聽器)

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    response.get(position).setSelected(true); 
    ListAdapter.notifyDataSetChanged(); 
} 
+0

選中此鏈接(http://www.androhub.com/android-listview-radiobutton/)。 –

回答

0

這聽起來像您使用的是CheckBox

你試過有條件更新的View知名度取決於是否該項目isChecked(和isSelected)?

0

應設置標籤爲每個圖像以跟蹤其狀態的(0 =未選擇/不可見,1 =選擇/可見),例如:

if (responseList.get(position).isSelected()) { 
     iv_choice_list_item_selected.setTag(1); 
    } 

並可以添加這樣的:

if (iv_choice_list_item_selected.getTag() == 1) { 
     iv_choice_list_item_selected.setVisibility(View.VISIBLE); 
    } else { 
     iv_choice_list_item_selected.setVisibility(View.GONE); 
    }    
+0

沒有它的不工作,它反而顯示在任意位置的勾號圖像,甚至沒有被選中 – User