2011-12-15 2 views
0

하나의 목록보기를 만들었습니다.이 각 행에는 확인란과 텍스트가 들어 있습니다. 확인란에있는 텍스트를 목록에서 가져와야합니다. 그건 내가 확인란을 사용하여 목록에서 선택한 텍스트 값을 전달해야합니다. 리스트는 내가 모든 값을 통과해야이 경우안드로이드에서 목록의 확인란을 어떻게 선택 하시겠습니까?

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> 

하고 온 클릭 방법의 하나에 다음 코드에 의해 선택된 항목을 얻을 수 있습니다 :

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가 필요합니다. 또한 확인란을 사용자 정의해야합니다. 그래서 위의 코드 만 사용해야합니다. 위의 코드를 시도했지만 목록에서 아무 것도 선택할 수 없으므로 확인란을 선택할 수 없습니다. – Lakshmanan

관련 문제