2013-05-29 2 views
0

У меня есть список, каждая строка которого имеет кнопку радиона, я хочу выбрать только одну из строк.элемент просмотра списка андроидов с переключателем все еще включен

Моя проблема в том, что я могу выбрать более одного элемента и кнопку радиона, даже когда я нажимаю ее еще раз, на самом деле у меня была эта проблема три месяца назад, и я ее разрешаю, теперь я попробовал такое же решение, но это неудобно не работает .

МОЙ ADAPTER КОД

class AdapterRestaurantSelectOne extends BaseAdapter { 

    private ArrayList<Boolean> rb_status = new ArrayList<Boolean>(); 
    private static LayoutInflater inflater = null; 
    private List<Restaurant> restaurants = null; 
    Context context; 
    LinearLayout ll_CancelDone; 
    public int positionNowSelected; 

    public AdapterRestaurantSelectOne(Context context, 
      List<Restaurant> restaurants, LinearLayout ll_CancelDone, 
      RadioGroup radioGroup) { 
     positionNowSelected = -1; 
     this.context = context; 
     this.restaurants = restaurants; 
     this.ll_CancelDone = ll_CancelDone; 
     inflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     for (int i = 0; i < restaurants.size(); i++) { 
      rb_status.add(false); 
     } 

    } 

    public Restaurant getRestaurant() { 
     return restaurants.get(positionNowSelected); 
    } 

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

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

    @Override 
    public long getItemId(int position) { 
     return restaurants.get(position).getID(); 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     if (vi == null) 
      vi = inflater.inflate(R.layout.profile_list_item_radiobutton, null); 
     TextView tv_name = (TextView) vi 
       .findViewById(R.id.tv_profile_list_item_radioButton_title); 
     ImageView iv_image = (ImageView) vi 
       .findViewById(R.id.iv_profile_list_item_radiobutton_image); 
     RadioButton rb_selected = (RadioButton) vi 
       .findViewById(R.id.cb_profile_list_item_radioButton_radioButton); 

     rb_selected.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
       rb_status.set(position, isChecked); 
       boolean status = false; 
       int i = 0; 
       for (Boolean b : rb_status) { 
        if (b) { 
         status = true; 
         break; 
        } 
        i++; 
       } 
       if (status) { 
        Basket.setRestaurant(restaurants.get(i)); 
        positionNowSelected = i; 
        ll_CancelDone.setVisibility(View.VISIBLE); 
       } else { 
        Basket.setRestaurant(null); 
        positionNowSelected = -1; 
        ll_CancelDone.setVisibility(View.GONE); 
       } 
      } 
     }); 
     rb_selected.setChecked(rb_status.get(position)); 
     // rb_selected.setChecked(position == positionNowSelected); 
     tv_name.setText(restaurants.get(position).getName()); 
     iv_image.setImageResource(restaurants.get(position).getImage()); 
     return vi; 
+1

Вы не добавляете радио кнопки внутри Radio Group –

+0

я должен делать это? Я так не думаю, я прочитал много тем здесь, никто не сказал, что мне нужно использовать Radio Group – user2387331

+0

http://stackoverflow.com/questions/10565989/how-to-add-radio-buttons-to- радиогруппа, http://stackoverflow.com/questions/4669104/dynamic-radio-button-control, http://www.mkyong.com/android/android-radio-buttons-example/ –

ответ

0

Я нашел решение моей самоуправления, это так;

lv_allRestaurants.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> parent, View vuew, 
      int position, long id) { 
     // TODO Auto-generated method stub 
     if (adapterAll2.getPositionNowSelected() == position) { 
      adapterAll2.setPositionNowSelected(-1); 
      adapterAll2.ll_CancelDone.setVisibility(View.GONE); 
     } else { 
      adapterAll2.setPositionNowSelected(position); 
      adapterAll2.ll_CancelDone.setVisibility(View.VISIBLE); 
     } 
     adapterAll2.notifyDataSetChanged(); 
    } 
}); 

Затем адаптер:

class AdapterRestaurantSelectOne extends BaseAdapter { 

    private ArrayList<Boolean> rb_status = new ArrayList<Boolean>(); 
    private static LayoutInflater inflater = null; 
    private List<Restaurant> restaurants = null; 
    Context context; 
    LinearLayout ll_CancelDone; 
    public int positionNowSelected; 

    public AdapterRestaurantSelectOne(Context context, 
      List<Restaurant> restaurants, LinearLayout ll_CancelDone) { 
     positionNowSelected = -1; 
     this.context = context; 
     this.restaurants = restaurants; 
     this.ll_CancelDone = ll_CancelDone; 
     inflater = (LayoutInflater) this.context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     for (int i = 0; i < restaurants.size(); i++) { 
      rb_status.add(false); 
     } 

    } 

    public Restaurant getRestaurant() { 
     return restaurants.get(positionNowSelected); 
    } 

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

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

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

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     if (vi == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      vi = inflater.inflate(R.layout.profile_list_item_radiobutton, null); 
     } 
     TextView tv_name = (TextView) vi 
       .findViewById(R.id.tv_profile_list_item_radioButton_title); 
     ImageView iv_image = (ImageView) vi 
       .findViewById(R.id.iv_profile_list_item_radiobutton_image); 
     RadioButton rb_selected = (RadioButton) vi 
       .findViewById(R.id.cb_profile_list_item_radioButton_radioButton); 
     tv_name.setText(restaurants.get(position).getName()); 
     iv_image.setImageResource(restaurants.get(position).getImage()); 
     rb_selected.setChecked(position == positionNowSelected); 
     return vi; 
    } 

    public int getPositionNowSelected() { 
     return this.positionNowSelected; 
    } 

    public void setPositionNowSelected(int now) { 
     this.positionNowSelected = now; 
    } 
} 
Смежные вопросы