开发者

Get index if checked rows in this customized listView?

Hi i am using the following code to generate a custom listview with checkbox.how can i get the index of the checked rows when the user clicks on the button?

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"开发者_C百科
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>


first change your getView because findViewById is called for every view which is wrong. you have a boolean array with false set for every item in the list. Than at every change in checked state of your check box, you change the corresponding boolean to true/false(checked/unchecked). Than

 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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜