【问题标题】:Custom listview implementation using checkboxes and buttons. Android使用复选框和按钮的自定义列表视图实现。安卓
【发布时间】:2013-12-13 11:18:58
【问题描述】:

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

这是我的主要课程...

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;

}
  }

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

【问题讨论】:

  • 删除按钮单击上的复选框是什么意思?您是否希望在单击按钮时取消选中复选框?
  • 不,对不起,我的意思是我想删除选中复选框并按下删除按钮的整行。
  • 我已经发布了答案。它工作正常

标签: android listview button checkbox


【解决方案1】:

维护一个布尔数组,用于跟踪选中复选框的行位置。

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

终于致电notifyDataSetChanged()

【讨论】:

    【解决方案2】:

    在这里,我有一个解决方案。我已在主要活动本身中添加了您的适配器类。基本上我已经创建了两个字符串类型的数组列表。当您选中一个复选框时,相应的 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;
    
    }
      }
    
    
     }
    

    【讨论】:

    • 如果适配器类和主类位于不同的 java 文件中,比如问题中发布的文件,这会起作用吗?
    • 是的。他们将@MohammadSohaib
    • 我现在终于有时间尝试了,但它不起作用。问题是在 AScustomadapter 类中无法访问某些变量,例如“checkedIndices”。
    猜你喜欢
    • 1970-01-01
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    相关资源
    最近更新 更多