2012-01-24 1 views
3

Listview 내에 체크 박스에 대한 많은 게시물이 있으며, 대다수의 내용을 읽은 후 나에게 두터운 뇌와 혼동 스러울 것이라고 솔직히 말할 수 있습니다. .코드는 ListViewActivity를 확장하지 않지만 목록보기가 있습니다.

내 코드는 ListViewActivity를 확장하지 않지만 tabhost 내에있는 상대적 레이아웃의 일부로 listview를가집니다.

textview 및 checkbox 모두에 대해 focusable = false를 시도했지만 textview는 동작하지만 checkbox는 여전히 listview 항목의 포커스를 무시합니다.

텍스트 뷰가 필요한 경우 레이아웃에 빌드 된 androids가 있어야하지만 각 행마다 checkox가있는 텍스트 뷰가 6 개 이상 필요합니다.

내 main.xml에는 이것이다 :

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tabhost_master" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TabWidget 
    android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="65px" 
    /> 

<FrameLayout 
    android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingTop="65px"> 
<RelativeLayout 
    android:id="@+id/tab1" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    android:weightSum="1" android:gravity="top" android:layout_width="fill_parent"> 
    <TextView android:text="Morning Set Up" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView> 
    <Button android:text="Set Time On" android:layout_height="wrap_content" android:id="@+id/startb" android:layout_width="wrap_content" android:layout_below="@+id/textView1" android:layout_alignParentLeft="true"></Button> 
    <Button android:text="Set Time Off" android:layout_height="wrap_content" android:id="@+id/stopb" android:layout_width="wrap_content" android:layout_alignTop="@+id/startb" android:layout_alignParentRight="true"></Button> 
    <Button android:layout_width="wrap_content" android:text="Set Days" android:id="@+id/mset_days" android:layout_height="wrap_content" android:layout_below="@+id/startb" android:layout_centerHorizontal="true"></Button> 
    <Button android:id="@+id/confirm_m" android:layout_height="wrap_content" android:text="Confirm" android:layout_width="wrap_content" android:layout_alignTop="@+id/mset_days" android:layout_alignParentLeft="true"></Button> 
    <Button android:id="@+id/cancel_m" android:layout_height="wrap_content" android:text="Cancel" android:layout_width="wrap_content" android:layout_alignTop="@+id/mset_days" android:layout_alignParentRight="true"></Button> 
    <ListView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/listView1" android:layout_below="@+id/mset_days" android:layout_alignParentLeft="true" android:layout_marginTop="34dp"></ListView> 
</RelativeLayout> 
<RelativeLayout 
    android:id="@+id/tab2" 
    android:orientation="vertical" 
    android:layout_height="fill_parent" 
    android:weightSum="1" android:gravity="top" android:layout_width="fill_parent"> 
    <TextView android:text="A Set Up" android:textAppearance="?android:attr/textAppearanceMedium" android:id="@+id/textView1_a" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"></TextView> 
    <Button android:text="Set Time On" android:layout_height="wrap_content" android:id="@+id/startb_a" android:layout_width="wrap_content" android:layout_below="@+id/textView1_a" android:layout_alignParentLeft="true"></Button> 
    <Button android:text="Set Time Off" android:layout_height="wrap_content" android:id="@+id/stopb_a" android:layout_width="wrap_content" android:layout_alignTop="@+id/startb_a" android:layout_alignParentRight="true"></Button> 
    <Button android:layout_width="wrap_content" android:text="Set Days" android:id="@+id/mset_days_a" android:layout_height="wrap_content" android:layout_below="@+id/startb_a" android:layout_centerHorizontal="true"></Button> 
    <TextView android:layout_height="wrap_content" android:text="Date/Time : \n Day \n Time" android:id="@+id/dandt_a" android:layout_width="wrap_content" android:layout_below="@+id/mset_days_a" android:layout_alignRight="@+id/mset_days_a" android:layout_marginTop="30dp"></TextView> 
    <TextView android:id="@+id/dandt2_a" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Along with this one as well" android:layout_below="@+id/dandt_a" android:layout_alignParentLeft="true" android:layout_marginLeft="14dp" android:layout_marginTop="44dp"></TextView> 
    <TextView android:layout_height="wrap_content" android:text="To see if these text boxes clash" android:id="@+id/dandt3_a" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_alignParentLeft="true"></TextView> 
    <Button android:id="@+id/confirm_after_a" android:layout_height="wrap_content" android:text="Confirm" android:layout_width="wrap_content" android:layout_alignTop="@+id/mset_days_a" android:layout_alignParentLeft="true"></Button> 
    <Button android:id="@+id/cancel_after_a" android:layout_height="wrap_content" android:text="Cancel" android:layout_width="wrap_content" android:layout_alignTop="@+id/mset_days_a" android:layout_alignParentRight="true"></Button> 

</RelativeLayout> 


</FrameLayout> 
</TabHost> 

목록보기에 대한 나의 레이아웃은 이것이다 :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <CheckBox android:text="Cancel ?" android:focusable="false" android:checked="false" android:layout_width="wrap_content" android:id="@+id/checkBox1" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></CheckBox> 
    <TextView android:layout_width="wrap_content" android:focusable="false" android:id="@+id/time_disp" android:layout_height="wrap_content" android:text="TextView" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/checkBox1" android:layout_marginLeft="16dp" android:layout_marginTop="15dp"></TextView> 
    <CheckedTextView android:layout_width="wrap_content" android:focusable="false" android:layout_height="wrap_content" android:text="CheckedTextView" android:id="@+id/checkedTextView1" android:layout_alignTop="@+id/time_disp" android:layout_alignParentRight="true" android:layout_marginRight="18dp"></CheckedTextView> 

</RelativeLayout> 

내 활동 코드는 이것이다 :

package co.uk.sheildsatmax.layout_tester; 



import android.app.Activity; 
import android.app.ListActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 
import android.widget.Toast; 
import android.widget.CheckBox; 
public class Layout_testerActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private String[] times = {"09:10","10:10","11:20","09:10","12:20","15:30","10:12"}; 
    ListView lv; 
    CheckBox ticked; 
    @Override 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost tabhost=(TabHost)findViewById(R.id.tabhost_master); 
     tabhost.setup(); 

     TabSpec tab1 = tabhost.newTabSpec("Tab1"); 
     morning_tab.setContent(R.id.tab1); 
     morning_tab.setIndicator("Tab1 Setup"); 

     TabSpec tab2 = tabhost.newTabSpec("tab2"); 
     after_tab.setContent(R.id.tab2); 
     after_tab.setIndicator("Tab2 Setup"); 

     tabhost.addTab(tab1b); 
     tabhost.addTab(tab2); 
     lv = (ListView)findViewById(R.id.listView1); 
     // ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,R.layout.,R.id.time_disp,times); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.list_lay,R.id.time_disp,times); 
     lv.setItemsCanFocus(false);  // this was another test attempt 
     lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // tried this and then realised it needed androids own layout to work 

     lv.setAdapter(adapter);  

     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       // TODO Auto-generated method stub 
       Toast.makeText(getApplication().getApplicationContext(), "ID :"+Long.toString(arg3), Toast.LENGTH_LONG).show(); 


      } 
     }); 




    } 


} 

지금 내가왔다 체크 박스가 제대로 작동하도록 어댑터에 대한 별도의 클래스를 만드는 방법에 대한 정보는 필요하지만이 경우 필요합니다. 누군가가 나를 내가 읽은이 모든 링크로 사용하기위한 기본 예제를 게시하시기 바랍니다 정말 나에게 혼동 한 :

당신은 내가 꽤 읽었지만, 그들은 심지어 더 아무도 나를 의아해했다 볼 수 있듯이 listactivies가없는 예제가있는 것 같습니다. 나는 이것을 안드로이드에 쓰고있다. 1.6

+0

anddev.org 링크에서 예제를 다시 시도해보고 사용해 보겠습니다. 확실하지 않은 경우에도 그들이 내 작업 내에서 ListActivity를 사용하지 않는 것으로 작동한다면 Activity는 tabhost도 관리하기 때문에 확실합니다. – TimCS

+0

나는 내가 여기있는 것의 더 나은 예를 발견했다고 생각한다. http://www.anddev.org/other-coding-problems-f5/multiple-checkboxes-in-a-listview-row-t51678.html 그래서 나는 이걸 시도해보고 난 후 – TimCS

+0

위의 링크에서 코드를 시도했지만 체크 박스가 listview의 포커스를 차지하는 문제는 여전히 있습니다. hmmm 여기에 더 많은 생각이 필요합니다. – TimCS

답변

0

체크 박스가 listview와 함께 작동하는 방법에 대한 나의 오해와 listview의 행동을 또한 이해한다.이 포럼 => [리스트 뷰 라인을 클릭하면 체크 박스를 똑딱] [2]

에이 스레드의 도움으로> managing multiple checkboxes

-

나는이 포럼에서이 스레드에 주어진 예를 사용하여 종료

마지막으로 목록보기에서 어떤 체크 상자가 틱인지 기억하고 =>Keeping the checkbox state on listview recycle

관련 문제