2011-08-05 1 views
0

こんにちは、次のコードを使用して、カスタムリストビューをcheckbox.howで生成します。ユーザーがボタンをクリックしたときに、チェックされた行のインデックスを取得できますか? findViewByIdが間違っているすべてのビューのために呼び出されるためこのカスタマイズされたlistViewでチェックされた行のインデックスを取得しますか?

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

は、最初にあなたのgetViewメソッドを変更します。 リスト内のすべての項目に対して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; 
} 
+0

@thanks a lot !!!!!!!! :-) – Harinder

関連する問題