使用复选框和按钮自定义列表视图实现。 Android的

时间:2013-11-27 17:13:51

标签: android listview button checkbox

我已经实现了一个自定义适配器类来控制我的listview。我正在列表视图中的每一行实现一个按钮,一个文本框和一个复选框。我有几个不属于listview的按钮。它们低于列表视图。现在,当我检查任意数量的框并按下不属于列表视图的按钮时,我希望能够删除选中的框(如果有的话)。换句话说,我希望能够通过单击不属于列表视图的按钮来删除选中的项目。

这是我的主要课程......

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) {


    }

}
 }

这是我的Adapter类......

     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;

}
  }

这些行基本上都是用视频填充的,所以总而言之,我希望能够删除所选的视频,并选中复选框并按下删除按钮。需要帮助。提前谢谢。

2 个答案:

答案 0 :(得分:0)

维护一个布尔数组,用于跟踪已检查的CheckBoxes行的位置。

然后在Button onClickListener中从ArrayList中删除这些项,并将此更新的ArrayList传递给适配器。

最后致电notifyDataSetChanged()

答案 1 :(得分:0)

在这里,我有一个解决方案。我已在主活动本身中添加了适配器类。基本上我已经创建了两个类型字符串的数组列表。选中复选框后,相应的textview值将存储在列表 CHECKEDLIST 中。在另一个列表 ALLVALUES 中,将存储所有值。

在按钮上单击我匹配两个列表,如果匹配发生,那么我从 ALLVALUES 中删除该值并调用notifydatasetchanged

package com.example.test;

import java.io.File;
import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnClickListener {
    private ArrayList<String> checkedIndices = new ArrayList<String>();
    ArrayList<String> list = new ArrayList<String>();
    AScustomadapter adapter;
int count = 0;
private String[] vidNames = {"a","b","c","d","e","f","g","h","i"};
private ListView myList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(this);



    for (int i = 0; i < vidNames.length; i++) {
        list.add(vidNames[i]);
    }

    myList = (ListView)findViewById(R.id.list);
    adapter = new AScustomadapter(getApplicationContext(), list);
    myList.setAdapter(adapter);
}

@Override
public void onClick(View v) {
    if(v.getId() == R.id.button1) {


        for (int i = 0; i < checkedIndices.size(); i++) {
            for(int j = 0; j <list.size();j++){
            if(list.get(j).contains(checkedIndices.get(i))){
                list.remove(j);
            }
            }
        }
        adapter.notifyDataSetChanged();



    } 

}

public class AScustomadapter extends BaseAdapter {

private ArrayList<String> mListItems;
private LayoutInflater mLayoutInflater;
int i = 0;

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 list.get(i);
}

@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.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {
                //is chkIos checked?
        if (((CheckBox) v).isChecked()) {
            if(!checkedIndices.contains(getItem(index).toString()))
                checkedIndices.add(getItem(index).toString());          }
        else {
            checkedIndices.remove(getItem(index).toString());

        }
          //case 2

      }
    });

    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 class ViewHolder {

    protected TextView itemName;
    protected CheckBox cb1;

}
  }


 }