如果在此自定义listView中检查了行,则获取索引?

时间:2011-08-05 11:56:19

标签: android android-listview

您好我正在使用以下代码生成一个带有checkbox.how的自定义列表视图。当用户点击按钮时,我可以获得已检查行的索引吗?

package com.CustomListView;

import android.app.Activity;
import android.os.Bundle;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class CustomListViewActivity extends Activity {
    ListView mListview;

    Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mListview=(ListView)findViewById(R.id.listview);
        mListview.setAdapter(new mCustomList(this));


        btn =(Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


            }
        });


    }
    public class mCustomList extends BaseAdapter{
        private Context mContext;

        public mCustomList(Context c){
            mContext = c;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return COUNTRIES.length;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View converView, ViewGroup parent) {
            View List;
            if(converView==null){
                List=new View(mContext);
                LayoutInflater mLayoutinflater=getLayoutInflater();
                List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
            }
            else{
                List = (View)converView;
            }
            ImageView imageView = (ImageView)List.findViewById(R.id.imgview);
            TextView textView = (TextView)List.findViewById(R.id.text);
            CheckBox chkbox=(CheckBox)List.findViewById(R.id.chkbox);
            textView.setText(COUNTRIES[position]);

            // TODO Auto-generated method stub
            return List;
        }

    }

    static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
        "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",

    };
}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:scrollbars="none"
></ListView>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>


</LinearLayout>

mylist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#099900" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scaleType="center"
android:id="@+id/imgview" android:src="@drawable/icon" android:layout_centerInParent="true" />
<CheckBox android:id="@+id/chkbox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:checked="true" android:layout_alignParentRight="true" android:layout_marginRight="10dip" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

首先更改你的getView,因为为每个错误的视图调用了findViewById。 你有一个布尔数组,列表中的每个项目都设置了false。与复选框的已检查状态的每次更改相比,您将相应的布尔值更改为true / false(已选中/未选中)。比

 if(converView==null){
            List=new View(mContext);
            LayoutInflater mLayoutinflater=getLayoutInflater();
            List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
            ImageView imageView = (ImageView)List.findViewById(R.id.imgview);
            TextView textView = (TextView)List.findViewById(R.id.text);
            CheckBox chkbox=(CheckBox)List.findViewById(R.id.chkbox);
            chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                 @Override
                 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   checkedArray[position] = isChecked;
                   //process your array. do something with indexes that correspond to a true value. 
                   } 
             }

         }
         });

} else{
    List = (View)converView;
}