2016-03-04 57 views
0

我有一個ListView,其中一個CheckBox和一個EditText。我想在每行中檢查CheckBox時看到EditText。目前,我設置的EditText知名度,GONE當點擊同一行中的另一項時,更改列表查看項目visibilty

這是我的適配器類:

private class MyCustomAdapter extends ArrayAdapter<Cheese> { 

     private ArrayList<Cheese> cheeseList; 

     public MyCustomAdapter(Context context, int textViewResourceId, 
       ArrayList<Cheese> cheeseList) { 
      super(context, textViewResourceId, cheeseList); 
      this.cheeseList = new ArrayList<Cheese>(); 
      this.cheeseList.addAll(cheeseList); 
     } 

     private class ViewHolder { 
      EditText code; 
      CheckBox name; 
     } 

    @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 

       ViewHolder holder = null; 
       Log.v("ConvertView", String.valueOf(position)); 

       if (convertView == null) { 
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = vi.inflate(R.layout.cheese_info, null); 

        holder = new ViewHolder(); 
        holder.code = (EditText) convertView 
          .findViewById(R.id.quantity); 
        holder.name = (CheckBox) convertView 
          .findViewById(R.id.checkBox1); 
        convertView.setTag(holder); 

        holder.name.setOnClickListener(new View.OnClickListener() { 
         public void onClick(View v) { 
          CheckBox cb = (CheckBox) v; 
          Cheese country = (Cheese) cb.getTag(); 
          Toast.makeText(
            getApplicationContext(), 
            "Clicked on Checkbox: " + cb.getText() + " is " 
              + cb.isChecked(), Toast.LENGTH_LONG) 
            .show(); 
          country.setSelected(cb.isChecked()); 
          ViewHolder holder1 = (ViewHolder)v.getTag(); 
          holder1.code.setVisibility(View.VISIBLE); 

         } 
        }); 
       } else { 
        holder = (ViewHolder) convertView.getTag(); 
       } 

       Cheese country = cheeseList.get(position); 
       holder.name.setText(country.getName()); 
       holder.name.setChecked(country.isSelected()); 
       holder.name.setTag(country); 

       return convertView; 

      } 

     } 

我嘗試添加

ViewHolder holder1 = (ViewHolder)v.getTag(); 
holder1.code.setVisibility(View.VISIBLE); 

CheckBox點擊。但是,應用程序越來越應聲給錯誤

java.lang.ClassCastException:com.example.viewpagersample.Cheese 不能轉換到 com.example.viewpagersample.MainActivity $ MyCustomAdapter $ ViewHolder

+0

嘗試通過您的奶酪對象操作的知名度和使用setOnItemClickListener您的適配器之外! –

+0

我認爲問題是與此行holder.name.setTag(國家); – Vishwa

+0

你正在嘗試在標記而不是視圖中分配集合Cheese對象 – Vishwa

回答

0

我找出解決方案。我得父ViewGroup中,找到 的EditText和能見度的變化如下圖所示:

ViewGroup viewGroup=(ViewGroup)v.getParent(); 
EditText quantity=(EditText)viewGroup.findViewById(R.id.quantity); 
quantity.setVisibility(View.VISIBLE); 
0
private class MyCustomAdapter extends ArrayAdapter<Cheese> { 

     private ArrayList<Cheese> cheeseList; 

     public MyCustomAdapter(Context context, int textViewResourceId, 
       ArrayList<Cheese> cheeseList) { 
      super(context, textViewResourceId, cheeseList); 
      this.cheeseList = new ArrayList<Cheese>(); 
      this.cheeseList.addAll(cheeseList); 
     } 

     private class ViewHolder { 
      EditText code; 
      CheckBox name; 
     } 

    @Override 
      public View getView(int position, View convertView, ViewGroup parent) { 

       ViewHolder holder = null; 
       Log.v("ConvertView", String.valueOf(position)); 

       if (convertView == null) { 
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        convertView = vi.inflate(R.layout.cheese_info, null); 

        holder = new ViewHolder(); 
        holder.code = (EditText) convertView 
          .findViewById(R.id.quantity); 
        holder.name = (CheckBox) convertView 
          .findViewById(R.id.checkBox1); 
        convertView.setTag(holder); 


       } else { 
        holder = (ViewHolder) convertView.getTag(); 
       } 

       Cheese country = cheeseList.get(position); 
       holder.name.setText(country.getName()); 
       holder.name.setChecked(country.isSelected()); 
       holder.name.setTag(country); 
holder.name.setOnClickListener(new Listener(country,holder.name,holder.code)); 
       return convertView; 

      } 
class Listener implements View.OnClickListener 
    { 
    Cheese country; 
    CheckBox cb; 
    EditText view; 
    Listener(Cheese country, CheckBox cb,EditText view) 
    { 
    this.country = country; 
    this.cb = cb; 
    this.view = view; 
    } 

    @Override 
     public void onClick(View v) { 

          Toast.makeText(
            getApplicationContext(), 
            "Clicked on Checkbox: " + cb.getText() + " is " 
              + cb.isChecked(), Toast.LENGTH_LONG) 
            .show(); 
          country.setSelected(cb.isChecked()); 
          view.setVisibility(View.VISIBLE) 

     } 
    } 

     } 
相關問題