2013-04-09 95 views
0

我有一個列表視圖,由複選框列表填充..我想添加一個按鈕,當我按下它時,它會爲每個項目在列表視圖中選擇哪個用戶..我知道我可以使用setOnCheckedChangeListener但是我如何爲我的列表中的每個複選框調用?如果我創建一個數組字符串,並且每次我的複選框選中它時,都會將其文本添加到數組列表可以爲每個選定的項目做循環嗎? 這裏是我的代碼pupulate列表視圖什麼是做listview選擇循環的最好方法

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_database_list); 
    File path=new File(ClubCP.SDcardPath); 
    List<String> file_lists = main.directoryPath(path); 
    ListView Database_list=(ListView)findViewById(R.id.database_list); 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      R.layout.database_list_item,R.id.chk_database_list_item,file_lists); 
    ArrayAdapter<String> adapter1 = new InteractiveArrayAdapter(this,file_lists); 
    Database_list.setAdapter(adapter1); 

} 

我搜索,我想我必須使用一個ArrayAdapter ..我正在寫的class..this是我的課 包co.tosca.persianpoem;

import java.util.List; 

import android.app.Activity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 

public class InteractiveArrayAdapter extends ArrayAdapter<String> { 

     private final List<String> list; 
     private final Activity context; 

     public InteractiveArrayAdapter(Activity context, List<String> list) { 
     super(context, R.layout.database_list_item, list); 
     this.context = context; 
     this.list = list; 
     } 

     static class ViewHolder { 
     protected CheckBox checkbox; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
     View view = null; 
     if (convertView == null) { 
      LayoutInflater inflator = context.getLayoutInflater(); 
      view = inflator.inflate(R.layout.database_list_item, null); 
      final ViewHolder viewHolder = new ViewHolder(); 
      viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chk_database_list_item); 
      viewHolder.checkbox 
       .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
        ///do sth 

       } 
       }); 
      view.setTag(viewHolder); 
      viewHolder.checkbox.setTag(list.get(position)); 
     } else { 
      view = convertView; 
      ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position)); 
     } 


     return view; 
     } 
    } 

感謝您的幫助

+0

「do sth」是什麼意思? – asloob 2013-04-09 05:56:25

+0

檢查此:http://dj-android.blogspot.in/2013/02/multi-selection-listview-android-with.html – 2013-04-09 05:57:22

+0

感謝您的幫助,@ user603125我的意思是在這裏我想添加選定的項目到arrey list or sth like that ..我正在處理它.. @ Dhaval Sodha Parmar我已經找到了這個例子。謝謝.. – Majid 2013-04-09 06:02:13

回答

1

您需要繼承ArrayAdapter和覆蓋getView方法,所以你可以在每個視圖基礎打電話setOnClickListener。請參閱ArrayAdapter文檔:here

public View getView (int position, View convertView, ViewGroup parent) { 
    View itemView = convertView; 
    if (itemView == null) { 
     LayoutInflater li = LayoutInflater.from(getContext()); 
     itemView = li.inflate(R.layout.database_list_item); 
    } 

    CheckBox cb = (CheckBox)itemView.findViewById(R.id.checkbox); // whatever the id of checkbox is in layout 
    cb.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      CheckBox myCb = (CheckBox)v; 
      if (myCb.isChecked()) { 
       // add position to array representing selected items 
      } else { 
       // remove position from array 
      } 
     } 
    }); 

    return itemView; 
} 
+0

非常感謝,我用你的代碼,但現在我在'View itemView = convertView? getLayoutInflater()。inflate(R.layout.database_list_item);'和不能編譯它說'語法錯誤,插入':表達式「來完成表達式」 – Majid 2013-04-09 06:38:18

+0

對不起,這是一個來自c#的運算符(確切地說是空合併運算符)。我會更新代碼片段來修復它。 – 2013-04-09 07:04:11

+0

我更新了有問題的代碼。 – 2013-04-09 07:14:37

相關問題