简体   繁体   中英

Custom listview implementation using checkboxes and buttons. Android

I have implemented a custom adapter class for controlling my listview. I'm implementing a button, a textbox and a checkbox against each row in the listview. I have a couple of buttons that are not part of the listview. They are below the listview. Now when I check any number of boxes and press the button that is not part of the listview, I want to be able to delete the checked boxes (if there are any). In other words, I want to b able to delete the checked items with the click of a button that is not part of the listview.

Here's my main class...

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


    }

}
 }

Here's my Adapter class...

     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;

}
  }

The rows are basically populated with videos, so all in all, I want to be able to delete the selected videos against which the check boxes are checked and the delete button is pressed. Help required. Thanks in advance.

Maintain a Boolean Array which keeps track of the position of the rows of checked CheckBoxes.

Then in your Button onClickListener remove those items from the ArrayList and pass this updated ArrayList to your adapter.

Finally call notifyDataSetChanged()

Here , i have a solution for you. I have added your adapter class in the main activity itself. Basically i have created two array list of type string. When you check a checkbox, the corresponding textview value is stored in it the list CHECKEDLIST . In the other list ALLVALUES , all the values are stored.

On button click i match both the lists and if a match occurs, then i remove that value from ALLVALUES and call 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;

}
  }


 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM