2012-07-11 70 views
2

我試圖創建一個包含目錄名稱的ListViewListView包含一個TextView以顯示文件夾名稱和一個checkbox。 列表已成功創建,但當我向上/向下滾動列表時,我正面臨複選框狀態的問題。複選框的狀態隨機更改。 要保存複選框的狀態,我正在使用HashMap<String, Boolean>,字符串將目錄名稱和布爾值複選框狀態。包含複選框滾動問題的ListView

下面是代碼(很抱歉的長度): MainActivity.java

public class MainActivity extends ListActivity implements OnClickListener { 

    Button done, cancel, selectAll; 

    ArrayList<String> m_list; 
    HashMap<String, Boolean> m_checkedMap; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     m_checkedMap = new HashMap<String, Boolean>(); 

     done = (Button) findViewById(R.id.bDone); 
     cancel = (Button) findViewById(R.id.bCancel); 
     selectAll = (Button) findViewById(R.id.bSelAll); 
     done.setOnClickListener(this); 
     cancel.setOnClickListener(this); 

     new AsyncHandler(this).execute(); 
    } 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     Toast.makeText(this, m_list.get(position), Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
     case R.id.bDone: 
      //Update CheckedMap in DB 
      Toast.makeText(this, m_checkedMap.values().toString(), 
        Toast.LENGTH_LONG).show(); 
      break; 
     case R.id.bCancel: 
      this.finish(); 
      break; 
     } 
    } 

    public class AsyncHandler extends AsyncTask { 

     Context context; 
     public AsyncHandler(Context c) { 
      context = c; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      Toast.makeText(context, "In onPreExecute()", Toast.LENGTH_SHORT) 
        .show(); 
     } 

     @Override 
     protected Object doInBackground(Object... arg0) { 
      getList(); 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Object result) { 
      // super.onPostExecute(result); 
      setListAdapter(new ElementAdapter(context, m_list, m_checkedMap)); 
     } 

     private void getList() { 
      m_list = new ArrayList<String>(); 
      File root = new File("/"); 
      File[] files = root.listFiles(); 
      for (File f : files) { 
       if (f.isDirectory() && !f.isHidden()) { 
        m_list.add(f.getName()); 
        m_checkedMap.put(f.getName(), false); 
       } 
      } 
      Collections.sort(m_list); 
     } 
    } 
} 

適配器類

public class ElementAdapter extends ArrayAdapter { 

    LinearLayout rowLayout; 
    ArrayList<String> items = new ArrayList<String>(); 
    TextView tvElement; 
    HashMap<String, Boolean> checkedMap; 
    CheckBox cBox; 
    Context context; 

    public ElementAdapter(Context c, ArrayList<String> elements, 
      HashMap<String, Boolean> m_checkedMap) { 
     super(c, R.layout.row, elements); 
     this.items = elements; 
     this.checkedMap = m_checkedMap; 
     this.context = c; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     if (view == null) { 
      LayoutInflater vi = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = vi.inflate(R.layout.row, null); 
     } 

     rowLayout = (LinearLayout) view.findViewById(R.id.row); 
     tvElement = (TextView) view.findViewById(R.id.tvElement); 
     tvElement.setText(items.get(position)); 
     cBox = (CheckBox) view.findViewById(R.id.checkBox1); 
     cBox.setOnClickListener(new OnClickListener() { 
      public void onClick(View paramView) { 
       if (cBox.isChecked()) { 
        //checkedMap.put(tvElement.getText().toString(), false); 
        checkedMap.put(items.get(position), false); 
       } else if (!cBox.isChecked()) { 
        //checkedMap.put(tvElement.getText().toString(), true); 
        checkedMap.put(items.get(position), true); 
       } 
      } 
     }); 
     cBox.setChecked(checkedMap.get(items.get(position))); 
     return view; 
    } 
} 

回答

3

我創造了一個博客帖子出於同樣的原因CheckBox的滾動發行與ListView, ListView with CheckBox Scrolling Issue

+0

它的工作,但如何。我沒有完全理解它。你可以解釋一下setTag()和getTag()(特別是使用'viewHolder.checkbox.setTag(position);'] – reiley 2012-07-11 13:38:26

+0

setTag(position)將會把這個位置加上可以進一步使用的視圖。你可以看到我使用int pos =(Integer)v.getTag();來獲取相同的位置。並使用它。 – 2012-07-11 13:41:47

+0

嗨,我遵循你的代碼,它運行良好......但似乎現在創建其他相關的問題。 ArrayList現在只保留替代值,即保持第一和第三並忘記第二和第四。 [這裏](http://stackoverflow.com/questions/11486128/android-after-button-press-checkbox-oncheckedchange-not-working) – reiley 2012-07-15 13:05:18