我有一個列表視圖,由複選框列表填充..我想添加一個按鈕,當我按下它時,它會爲每個項目在列表視圖中選擇哪個用戶..我知道我可以使用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;
}
}
感謝您的幫助
「do sth」是什麼意思? – asloob 2013-04-09 05:56:25
檢查此:http://dj-android.blogspot.in/2013/02/multi-selection-listview-android-with.html – 2013-04-09 05:57:22
感謝您的幫助,@ user603125我的意思是在這裏我想添加選定的項目到arrey list or sth like that ..我正在處理它.. @ Dhaval Sodha Parmar我已經找到了這個例子。謝謝.. – Majid 2013-04-09 06:02:13