我已經實現了一個自定義適配器類來控制我的列表視圖。我正在實現一個按鈕,一個文本框和一個複選框列表視圖中的每一行。我有幾個不屬於listview的按鈕。他們在列表視圖下面。現在,當我檢查任意數量的框並按下不屬於列表視圖的按鈕時,我希望能夠刪除選中的框(如果有的話)。換句話說,我希望能夠通過點擊不屬於列表視圖的按鈕來刪除選中的項目。使用複選框和按鈕的自定義列表視圖實現。 Android
這裏是我的主類...
public class ASvideofiles extends Activity implements OnClickListener {
int count = 0;
private String[] vidNames;
private String[] vidPaths;
private ListView myList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asvideofiles);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
Button b2 = (Button) findViewById(R.id.button2);
b2.setOnClickListener(this);
File f = new File(Environment.getExternalStorageDirectory(),
"ABC/XYZ/");
File[] files = f.listFiles();
vidNames = new String[files.length];
vidPaths = new String[files.length];
if(files != null) {
for(int i=0; i < files.length; i++) {
vidNames[i] = files[i].getName();
vidPaths[i] = files[i].getPath();
count ++;
}
}
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < files.length; i++) {
list.add(vidNames[i]);
}
myList = (ListView)findViewById(R.id.listView1);
myList.setAdapter(new AScustomadapter(ASvideofiles.this, list));
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.button1) {
} else if(v.getId() == R.id.button2) {
}
}
}
這裏是我的適配器類...
public class AScustomadapter extends BaseAdapter {
private ArrayList<String> mListItems;
private LayoutInflater mLayoutInflater;
int i = 0;
private ArrayList<Integer> checkedIndices = new ArrayList<Integer>();
public AScustomadapter(Context context, ArrayList<String> arrayList) {
mListItems = arrayList;
//get the layout inflater
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mListItems.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
public int getTotalCheckedCount() {
return checkedIndices.size();
}
@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = mLayoutInflater.inflate(R.layout.list_item, null);
holder.itemName = (TextView) view.findViewById(R.id.list_item_text_view);
holder.cb1 = (CheckBox) view.findViewById(R.id.checkBox1);
view.setTag(holder);
} else {
holder = (ViewHolder)view.getTag();
}
final String stringItem = mListItems.get(position);
if (stringItem != null) {
if (holder.itemName != null) {
holder.itemName.setText(stringItem);
}
}
holder.cb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if(checkedIndices.contains(position)){
}else {
checkedIndices.add(position);
}
}else {
checkedIndices.remove((Integer)position);
}
}
});
if(checkedIndices.contains((Integer)position)) {
holder.cb1.setChecked(true);
} else {
holder.cb1.setChecked(false);
}
//this method must return the view corresponding to the data at the specified position.
return view;
}
private static class ViewHolder {
protected TextView itemName;
protected CheckBox cb1;
}
}
各行基本上填充視頻,所以一切的一切,我希望能夠刪除選中的複選框,並按下刪除按鈕。需要幫助。提前致謝。
你什麼意思通過刪除按鈕,複選框單擊匹配嗎?您是否希望在點擊按鈕時取消選中複選框? –
不,對不起,我的意思是我想刪除複選框被選中的整行,並按下刪除按鈕。 –
我已經發布了一個答案。它工作正常 –