如何从Android中的自定义ListView获取已检查项目的列表?

时间:2013-08-14 15:48:46

标签: android android-listview android-arrayadapter

我正在尝试在Android中使用自定义ListView。我已经将ArrayAdpater子类化为包含对象的ArrayList。放在ArrayAdapter中的对象来自SQLite数据库。 在我的ArrayAdapter类中,我覆盖了getView()方法,并为ListView的每一行放置了一个自定义视图,该视图放置了ImageView,3 TextViews和Checkbox视图。

我希望能够让我列表中的所有已检查行对它们执行某些操作,即。从列表和基础数据库中删除它们或编辑该行表示的基础数据对象。

我的问题是我似乎找不到让OnItemClickListener工作的方法。 ListView显示正常,但单击该行时没有任何反应,或者如果我选中该复选框并尝试使用我的removeUnit()方法删除选中的项目。我已经没想完了。

我在这里做错了什么?谢谢你的帮助!

这是我的活动

package com.mack.mylogger;

import android.os.Environment;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.*;
import android.database.Cursor;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.CheckBox;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.util.SparseBooleanArray;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.util.StringTokenizer;
import java.io.*;
import java.util.*;

public class ReportActivity extends Activity {

    private Context context = null;

    DBAdapter db = new DBAdapter(this); //my database adapter
    ListView reportLV;
    ArrayList<Integer> checkedPositions = new ArrayList<Integer>();

    String TAG = "ReportActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_report);

        // set up the list of finished units
        reportLV = (ListView) findViewById(R.id.listview);
        reportLV.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        ArrayList<RUnit> units = new ArrayList<RUnit>(); //init adapter with empty list
        reportLV.setAdapter(new ReportLVAdapter(this, units));
        reportLV.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Log.d(TAG, "Item is clicked at position: " + position);
                CheckBox cb = (CheckBox) view.findViewById(R.id.checkbox);
                cb.toggle();

                if (cb.isChecked()) {
                    checkedPositions.add(position);
                    Log.d(TAG, "Item is checked at position: " + position);
                } else if (!cb.isChecked()) {
                    checkedPositions.remove(position);
                    Log.d(TAG, "Item is unchecked at position: " + position);
                }


            }
        });

        // context
        context = this.getBaseContext();
    }


    @Override
    public void onResume() {
        super.onResume();
        updateListView();

    }

    private void updateListView() {

        // get list of units
        db.read();
        RUnit[] allUnits = db.getAllUnits();
        ArrayList<RUnit> finishedUnits = new ArrayList<RUnit>();
        System.out.println("Units in database: " + allUnits.length);
        RUnit unit;
        for (int i=0; i<allUnits.length; i++) {
            unit = allUnits[i];

                          // check status
            if (unit.getStatus() == "Finished") {
                finishedUnits.add(unit);
            } 

        }
        // update adapter with updated list of finished units
        reportLV.setAdapter(new ReportLVAdapter(this, finishedUnits));
      }

    public void addUnit(View view) {
        // method to run when "Add" button is clicked
        Intent intent = new Intent(this, AddActivity.class);
        startActivity(intent);

    }

    public void removeUnit(View view) {
        // method to run when remove button is clicked
        // loop thru the items in the ListView and remove checked items from the db 
        for (int i=0; i<checkedPositions.size(); i++) {
            String unitStr = reportLV.getItemAtPosition(i).toString();
            Log.d(TAG, "Removing unit: " + unitStr);

            // remove unit from the database
            db.deleteUnit(unitStr);
        }

        //update the LV
        updateListView();

    }

    public void submit(View view) {
                // do something here
      }


} 

这是我的ArrayAdapter

package com.mack.mylogger;

import android.app.Activity;
import android.util.*;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CheckBox;
import android.view.View.OnClickListener;
import java.util.*;

public class ReportLVAdapter extends ArrayAdapter<RUnit> {

    private final Activity context;
    private final ArrayList<RUnit> units;

    String TAG = new String("ReportLVAdapter");

    static class ViewHolder {
        public TextView serial;
        public TextView status;
        public TextView date;
        public ImageView model;
        public ImageView flag;
        public CheckBox checkbox;
    }


    public ReportLVAdapter(Activity context, ArrayList<RUnit> units) {
        super(context, R.layout.adapterlv_report, units);
        this.context = context;
        this.units = units;

        Log.d(TAG, "Size of adapter array: " + units.size());
      }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder viewHolder = new ViewHolder();

        if (rowView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            rowView = inflater.inflate(R.layout.adapterlv_report, null);
            viewHolder.serial = (TextView) rowView.findViewById(R.id.serial);
            viewHolder.model = (ImageView) rowView.findViewById(R.id.model);
            viewHolder.status = (TextView) rowView.findViewById(R.id.status);
            viewHolder.date = (TextView) rowView.findViewById(R.id.date);
            viewHolder.flag = (ImageView) rowView.findViewById(R.id.flag);
            viewHolder.checkbox = (CheckBox) rowView.findViewById(R.id.checkbox);
            rowView.setTag(viewHolder);
                } else {
            viewHolder = (ViewHolder) rowView.getTag();
        }

        // Need to handle empty database situation
        // get info from RUnit obj
        RUnit unit = units.get(position);
        String sn = unit.getSerialNumAsString();
        String mdl = unit.getModel();
        String stat = unit.getStatus();

        // date
        String d;
        if (stat == "Finished") { d = unit.getFinishDate(); }
        else if (stat == "Submitted") { d = unit.getSubmitDate(); }
        else if (stat == "Shipped") { d = unit.getShipDate(); }
        else { d = "NA"; }

        // flags
        String[] flags = unit.getFlags();

        ///// Set view values

        // text values
        viewHolder.serial.setText(sn);
        viewHolder.status.setText(stat);
        viewHolder.date.setText(d);

        // set model image
        if (mdl == "TK") {
            viewHolder.model.setImageResource(R.drawable.tk_icon);
        } else {
            // must be carrier
            viewHolder.model.setImageResource(R.drawable.carrier_icon);
        }

        // set flag image - only look for hold
        for (int i=0; i<flags.length; i++) {
            if (flags[i] == "hold") {
                // set hold image
                viewHolder.flag.setImageResource(R.drawable.hold_flag);
            } else {
                // no image
            }
        }

        return rowView;
    }

我的活动XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:text="@string/main_title"
        android:textAlignment="gravity"
        android:textAllCaps="true"
        android:textSize="36sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal"
        android:text="@string/report_subtitle"
        android:textAlignment="gravity"
        android:textAllCaps="true"
        android:textSize="24sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/button_submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_horizontal"
        android:text="@string/button_submit"
        android:onClick="submit"
        android:textSize="26sp" />


    <LinearLayout
        android:id="@+id/button_group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/button_submit"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal" > 

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.0"
            android:onClick="addUnit"
            android:text="@string/button_add_fg" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.0"
            android:text="@string/button_modify_fg" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.0"
            android:onClick="removeUnit"
            android:text="@string/button_remove_fg" />

    </LinearLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/button_group"
        android:layout_below="@id/subtitle"
        android:footerDividersEnabled="true"
        android:headerDividersEnabled="true" />

</RelativeLayout>

我对行XML的自定义视图

<?xml version="1.0" encoding="utf-8"?>

<GridLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:columnCount="5"
    android:rowCount="2"   
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/model"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_margin="5dip"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 

    <TextView
        android:id="@+id/serial"
        android:layout_row="0"
        android:layout_column="1"
        android:layout_columnSpan="2"
        android:textSize="35dp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/status"
        android:layout_row="1"
        android:layout_column="0"
        android:layout_columnSpan="2"
        android:textSize="20dp"
        android:textStyle="italic"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="5dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/date"
        android:layout_row="1"
        android:layout_column="2"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/flag"
        android:layout_row="0"
        android:layout_column="3"
        android:layout_rowSpan="2"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_row="0"
        android:layout_column="4"
        android:layout_rowSpan="2"
        android:layout_gravity="right"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" /> 

</GridLayout>   

2 个答案:

答案 0 :(得分:0)

您可以在列表视图中调用getCheckedItemPositions()。这将返回包含列表中所有项目的SparseBooleanArray以及是否检查它们。完成后,您可以遍历数组并使用valueAt(i)来确定是否选择了某个项目。

Here's a good article on this topic

答案 1 :(得分:0)

你可以试试这个

SparseBooleanArray checkedPositions = lv.getCheckedItemPositions ();int size = checkedPositions.size ();for (int i=0 ; i<size ; i++) {
 // We get the key stored at the index 'i'
 int key = checkedPositions.keyAt (i);
 // We get the boolean value with the key
 Log.i (Tag, "checkedPositions(" + key + ")=" + checkedPositions.get (key));}