2011-12-15 11 views
0

私は1つのリストビューを作成しました。この各行にはチェックボックスとテキストが含まれています。チェックボックスがオン(チェック)のリストのテキストを取得する必要があります。それはチェックボックスを使用して、リストから選択されたテキスト値を渡す必要があります。リストは、私はすべての値を渡す必要があり、この場合アンドロイドのリストのチェックボックスをどのように選択するには?

enter image description here

ように見える

gatwayList = (ListView) findViewById(R.id.gatwaylist); 

     ArrayList<HashMap<String,String>> wholeDetailsList = new ArrayList<HashMap<String,String>>(); 
     for (int i = 0; i < 5; i++) 
     { 


     HashMap<String, String> map = new HashMap<String, String>(); 
     map.put("name", "Test+ i"); 
     //map.put("address", wrap_address); 
     wholeDetailsList.add(map); 
     } 

     SimpleAdapter wholeDetailsAdapter = new SimpleAdapter(this, 
       wholeDetailsList, R.layout.gatway_row, new String[] {"name"}, new int[] {R.id.gatwayId}); 
     gatwayList.setCacheColorHint(0); 
     gatwayList.setAdapter(wholeDetailsAdapter); 

gatway_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    > 
<CheckBox 
     android:id="@+id/check" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="4px" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10px" > 
    </CheckBox> 


    <TextView android:id="@+id/gatwayId" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ffff" 
     android:textSize="18dip" 
     android:layout_marginTop="15dip" 
     android:layout_marginLeft="60dip" 
     android:textStyle="bold" 
     android:textColor="#000000"/> 


</RelativeLayout> 

は、これは私のコードです。それ以外は選択した値のみ。すべてのコメントは高く評価されますか?

答えて

0

私は期待UIと機能性を得るために使用しています次のスニペットを、

TableLayout table; 
final int row = 6; //variable to get the number of row 
     final CheckBox cb[] = new CheckBox[row]; 

        table = (TableLayout) findViewById(R.id.gatwaylist); 

        for (int i=0;i<row;i++) 

        { 
          TableRow row = new TableRow(this); 

          CheckBox gateway = new CheckBox(this); 
          gateway.setGravity(Gravity.CENTER); 
         // gateway.se 
          gateway.setTextColor(Color.BLACK); 
          gateway.setText("Gateway"+i); 

          row.addView(gateway); 

          View view = new View(this); 
          view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 2)); 
          view.setBackgroundColor(Color.BLACK); 
          table.addView(row); 
          if(!(i == numgateways-1)) 
          { 
           table.addView(view); 
          } 



          cb[i] = gateway; 
        } 

と表レイアウトxml要素は次のとおりです。

<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"> 
     <TableLayout android:id="@+id/gatwaylist" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_weight="1" 
     android:transcriptMode="normal" android:headerDividersEnabled="true" android:background="@drawable/framebackground" 
     > 
     </TableLayout></ScrollView> 

、あなたはonclickの方法のいずれかで、次のコードで選択した項目を取得することができます:

for(int i=0;i<row;i++) 
           { 
            if(cb[i].isChecked()) 
              { 
               Toast.makeText(TestVideoActivity.this, "Checked :"+cb[i].getText(), Toast.LENGTH_SHORT).show(); 
              } 
           }   } 
1

使用この代わりに、それは良いでしょう。そして、

listView.setChoiceMode(CHOICE_MODE_MULTIPLE); 
listView.setAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_multiple_choice, fields)); 

このような選択された項目を取得:

listView.getCheckedItemPositions(); 
+0

@AMRI:上記のコードでは「CHOICE_MODE_MULTIPLE」とは何ですか。また、これは私が期待したようにうまく動作しません。上記のUIと同じUIが必要です。また、チェックボックスをカスタマイズする必要があるので、上記のコードを使用してください。上記のコードを試しましたが、リストから何も選択できませんでした。つまり、チェックボックスは選択できません。 – Lakshmanan

関連する問題