简体   繁体   English

使用复选框和按钮的自定义列表视图实现。 安卓系统

[英]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. 维护一个布尔数组,该数组跟踪已检查CheckBoxes行的位置。

Then in your Button onClickListener remove those items from the ArrayList and pass this updated ArrayList to your adapter. 然后在您的Button onClickListener中,从ArrayList中删除这些项目,并将此更新的ArrayList传递给您的适配器。

Finally call notifyDataSetChanged() 最后调用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 . 选中复选框时,相应的textview值将存储在列表CHECKEDLIST中 In the other list ALLVALUES , all the values are stored. 在另一个列表ALLVALUES中 ,存储所有值。

On button click i match both the lists and if a match occurs, then i remove that value from ALLVALUES and call notifydatasetchanged 在按钮上单击我同时匹配列表和匹配项,然后从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;

}
  }


 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 自定义列表视图和复选框/按钮的问题 - Problem with custom Listview and Checkboxes/Buttons 具有单选按钮的自定义ListViewAdapter。 - Custom ListViewAdapter with Radio Buttons. 使用按钮(Android)过滤自定义Listview - Filter a custom Listview using Buttons (Android) 1个列表视图和3个按钮。 1个按钮不可单击,为什么? - 1 listview and 3 buttons. 1 button is not clickable, why? 我正在尝试使用listview按钮从cutomlistadapter打开Android Activity。 我尽力了山,但没有结果? - i am trying to open Android Activity from cutomlistadapter with listview buttons. i tried at mt best but no result? 带有3个按钮的Android小部件。 和不同的意图 - Android widget with 3 buttons. And different intents Android-自定义列表视图适配器中的CheckBoxes怪异问题? - Android - weird problem with CheckBoxes in custom listview adapters? android如何动态绑定复选框与自定义listview - android how to dynamically bind checkboxes with custom listview 带有CheckBoxes的Android自定义ListView搜索,过滤器不起作用 - Android Custom ListView search with CheckBoxes , Filter is not Working Android ListView选中所有复选框(自定义ResourceCursorAdapter) - Android ListView check all checkboxes (custom ResourceCursorAdapter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM