2013-11-21 47 views
0

好吧,我堅持整天嘗試在列表視圖中添加複選框。我有兩部分Android - 在ListView中的多個選擇

  1. 如果我點擊列表框中的任何項目,它會加載一些東西(這部分已經實現)。
  2. 每個項目都有一個複選框。我希望能夠檢查任何10個或任意數量的項目,以便即使滾動瀏覽列表,選擇也不會丟失。

任何人都可以請幫助我嗎?我檢查的一個解決方案是this solution,但我不想重構整個代碼,除非有必要。

MAINACTIVITY:

public class ShowList extends Activity{ 

    static final String LIST_KEY_ID = "bookid"; 
    static final String LIST_KEY_NAME = "bookname"; 
    static final String LIST_KEY_WRITER = "writer"; 

    ListView list; 
    MylibmanList adapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_show_list); 
     setupActionBar(); 

     mylibmandbhandler db = new mylibmandbhandler(this); 
     List<mylibman> allfld = db.getAllRecord(); 
     db.close(); 
     ArrayList<HashMap<String, String>> allbookList = new ArrayList<HashMap<String, String>>(); 
     for (mylibman cn : allfld) { 
      HashMap<String, String> map = new HashMap<String, String>(); 
       map.put(LIST_KEY_ID, Integer.toString(cn.getBookid())); 
      map.put(LIST_KEY_NAME, cn.getBookname()); 
       map.put(LIST_KEY_WRITER, cn.getWriter()); 

       allbookList.add(map); 
     } 
     list=(ListView)findViewById(R.id.list); 
     adapter=new MylibmanList(this, allbookList); 
     list.setAdapter(adapter); 

     list.setOnItemClickListener(new OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
       batchid.clear(); 
       HashMap<String, String> list_hashMap = (HashMap<String, String>) parent.getItemAtPosition(position); 
       String currlist = list_hashMap.get(LIST_KEY_ID); 
       Intent returnIntent = new Intent(); 
       returnIntent.putExtra("bookid",currlist); 
       setResult(RESULT_OK,returnIntent); 
       finish(); 
      } 
     }); 

適配器類:

public class MylibmanList extends BaseAdapter { 
     private Activity activity; 
     private ArrayList<HashMap<String, String>> data; 
     private static LayoutInflater inflater=null; 

     public MylibmanList(Activity a, ArrayList<HashMap<String, String>> d) { 
      activity = a; 
      data=d; 
      inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     static class ViewHolder { 
      TextView title; 
      TextView artist; 
      TextView duration; 
      CheckBox check; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      View vi=convertView; 
      if(convertView==null) 
       vi = inflater.inflate(R.layout.listrow_row, null); 

      ViewHolder holder = new ViewHolder(); 
      holder.title = (TextView)vi.findViewById(R.id.title); 
      holder.artist = (TextView)vi.findViewById(R.id.artist); 
      holder.duration = (TextView)vi.findViewById(R.id.duration); 
      holder.check = (CheckBox)vi.findViewById(R.id.check); 

      holder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        Log.d("test","t3"); // Don't know what to do 
       } 
      }); 
      vi.setTag(holder); 
      holder.check.setTag(data.get(position)); 

      HashMap<String, String> book = new HashMap<String, String>(); 
      book = data.get(position); 

      ViewHolder holderfin = (ViewHolder) vi.getTag(); 
      holderfin.title.setText(book.get(ShowList.LIST_KEY_NAME)); 
      holderfin.artist.setText(book.get(ShowList.LIST_KEY_WRITER)); 
      holderfin.duration.setText(book.get(ShowList.LIST_KEY_ID)); 
      holderfin.check.setChecked(false); 
      return vi; 
     } 
    } 

XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="5dip" > 

    <LinearLayout android:id="@+id/thumbnail" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="3dip" 
     android:layout_alignParentLeft="true" 
     android:background="@drawable/image_bg" 
     android:layout_marginRight="5dip"> 
<!-- 
     <ImageView 
      android:id="@+id/list_image" 
      android:contentDescription="@string/bookimage" 
      android:layout_width="50dip" 
      android:layout_height="50dip"/> 
--> 
    </LinearLayout> 


    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@+id/thumbnail" 
     android:layout_toRightOf="@+id/thumbnail" 
     android:focusableInTouchMode="false" 
     android:clickable="false" 
     android:focusable="false" 
     android:textColor="#040404" 
     android:typeface="sans" 
     android:textSize="18sp" 
     android:textStyle="bold"/> 


    <TextView 
     android:id="@+id/artist" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/title" 
     android:focusableInTouchMode="false" 
     android:clickable="false" 
     android:focusable="false" 
     android:textColor="#343434" 
     android:textSize="13sp" 
     android:layout_marginTop="1dip" 
     android:layout_toRightOf="@+id/thumbnail" /> 


    <TextView 
     android:id="@+id/duration" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@id/title" 
     android:gravity="right" 
     android:focusableInTouchMode="false" 
     android:clickable="false" 
     android:focusable="false" 
     android:layout_marginRight="5dip" 
     android:textSize="12sp" 
     android:textColor="#10bcc9" 
     android:textStyle="bold"/> 

    <CheckBox 
     android:id="@+id/check" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:focusable="false" 
     android:layout_marginRight="10dp" /> 
</RelativeLayout> 
+0

東西可以添加listrow_row的XML做可以幫助你和你的第一個問題也是,要檢查上的複選框上點擊複選框時,或者當你點擊'onItemClick'或者你可以考慮'onItemLongClick'?請更精確地說你想要什麼 – Coderji

+0

對不起,我忘了..加入 – abdfahim

+0

我希望能夠從列表視圖中選擇10或任何隨機行(比如說我有100行),每次我檢查一個checbox時,有一個「持續時間」ArrayList ..現在,如果我檢查一些複選框,並向下滾動,從最後一個視圖中的所有選擇都會丟失(我猜listview是這樣的) – abdfahim

回答

1

基本上,喲你可以有一個只包含選定項目的哈希集合。代碼看起來像這樣

public class MylibmanList extends BaseAdapter { 
    private Activity activity; 
    private ArrayList<HashMap<String, String>> data; 
    private static LayoutInflater inflater=null; 

    HashSet<String> selectedBooks = new HashSet<String>(); 

    //This listener will be used on all your checkboxes, there's no need to 
    //create a listener for every checkbox. 
    CompoundButton.OnCheckedChangeListener checkChangedListener = new CompoundButton.OnCheckedChangeListener{ 
      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       String bookDuration = (String) buttonView.getTag(); 
       if(isChecked){ 
        selectedBooks.add(bookDuration); 
       }else{ 
        selectedBooks.remove(bookDuration); 
       } 
      } 
    } 


    public MylibmanList(Activity a, ArrayList<HashMap<String, String>> d) { 
     activity = a; 
     data=d; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    static class ViewHolder { 
     TextView title; 
     TextView artist; 
     TextView duration; 
     CheckBox check; 
    } 


    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if(convertView==null){ 
      convertView = inflater.inflate(R.layout.listrow_row, null); 

      holder = new ViewHolder(); 
      holder.title = (TextView) convertView.findViewById(R.id.title); 
      holder.artist = (TextView) convertView.findViewById(R.id.artist); 
      holder.duration = (TextView) convertView.findViewById(R.id.duration); 
      holder.check = (CheckBox) convertView.findViewById(R.id.check); 


      holder.check.setOnCheckedChangeListener(checkChangedListener); 

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


     HashMap<String, String> book = new HashMap<String, String>(); 
     book = (HashMap<String, String>) getItem(position); 

     holder.check.setTag(book.get(ShowList.LIST_KEY_ID)); 

     holder.title.setText(book.get(ShowList.LIST_KEY_NAME)); 
     holder.artist.setText(book.get(ShowList.LIST_KEY_WRITER)); 
     holder.duration.setText(book.get(ShowList.LIST_KEY_ID)); 

     boolean bookSelected = false; 
     if(selectedBooks.contains(book.get(ShowList.LIST_KEY_ID))){ 
      bookSelected = true; 
     } 

     holder.check.setChecked(bookSelected); 

     return convertView; 
    } 

我改變了你的getView abit。現在,每個視圖只能創建一次viewHolder(它應該)。另外,如果它沒有太多的喧囂,你應該爲你的書創建一個類。像

Class Book{ 
    String title; 
    String artist; 
    Long duration; 
} 
+0

讓我知道是否有任何你還沒有理解的部分。 – vikki

+0

可否請複製粘貼整個適配器類..因爲我剛剛開始學習,有時我迷失在哪裏插入什麼。如果你把你的代碼和我的代碼放在一起,對我的理解有幫助。 – abdfahim

+0

我現在還沒有接近正確的鍵盤。然而,你所需要做的就是將你的'getView'替換爲我發佈的'getView',然後將其餘部分(selectedBooks和checkChangedListener)放在你的類的頂部,它們是類成員。你可以把它們放在線的下方'private static LayoutInflater inflater = null;'。 – vikki