2014-06-20 87 views
2

不顯示我試圖創建一個列表是這樣的:複選框在CheckedTextView的Android

<image><text>  <checkbox>. 

到目前爲止,文本和圖像顯示,但該複選框沒有。大部分代碼都適合在這裏: http://www.learn2crack.com/2013/10/android-custom-listview-images-text-example.html

activity_filter_restaurants.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

<ListView 
    android:id="@+id/listFilter" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
</ListView> 
</RelativeLayout> 

list_filter_restaurants.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TableRow> 
     <ImageView 
      android:id="@+id/imgfilter" 
      android:layout_height="100dp" 
      android:layout_width="100dp" /> 

    <CheckedTextView 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:id="@+id/checkedTextView" 
     android:layout_column="2" 
     android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
     android:checked="false"/> 
</TableRow> 
</TableLayout> 

Filterlist.java

public class FilterList extends ArrayAdapter<String> { 
    private final Activity context; 
    private final String[] options; 
    private final Integer[] optionImageId; 

    CheckedTextView chktxtTitle; 

    public FilterList(Activity Context, String [] options, Integer [] optionImageId){ 
     super(Context, R.layout.list_filter_restaurants,options); 
     this.context = Context; 
     this.options = options; 
     this.optionImageId = optionImageId; 
    } 

    @Override 
    public View getView (int position, View view, ViewGroup parent){ 
     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView= inflater.inflate(R.layout.list_filter_restaurants, null, true); 

     chktxtTitle = (CheckedTextView) rowView.findViewById(R.id.checkedTextView); 

     chktxtTitle.setOnClickListener(new View.OnClickListener(){ 
      @Override 
      public void onClick (View v){ 
       if(chktxtTitle.isChecked()) 
        chktxtTitle.setChecked(false); 
       else 
        chktxtTitle.setChecked(true); 
      } 
     }); 


     ImageView imageView = (ImageView) rowView.findViewById(R.id.imgfilter); 
     chktxtTitle.setText(options[position]); 
     //chktxtTitle.setCheckMarkDrawable(android.R.attr.listChoiceIndicatorMultiple); 
     imageView.setImageResource(optionImageId[position]); 
     return rowView; 
    } 
} 

FilterRestaurants.java

public class FilterRestaurants extends Activity { 

    ListView filterlist; 
    String [] options = { 
      " Vegetarian"," Seafood"," Western food"," Indian/South Asian"," Halal/Islamic"," Chinese"," Sandwiches", 
      " Deli", " Coffee" 
      }; 

    Integer [] optionsId = { 
      R.drawable.vegan, 
      R.drawable.seafood, 
      R.drawable.westernfood, 
      R.drawable.indian, 
      R.drawable.halal, 
      R.drawable.chinese, 
      R.drawable.sandwiches, 
      R.drawable.deli, 
      R.drawable.coffee 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header_food); 

     setContentView(R.layout.activity_filter_restaurants); 

     CustomList adaptor = new CustomList(FilterRestaurants.this,options,optionsId); 

     filterlist = (ListView) findViewById(R.id.listFilter); 
     filterlist.setAdapter(adaptor); 
     // filterlist.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
     filterlist.setChoiceMode(ListView.CHOICE_MODE_NONE); 
     filterlist.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
       Toast.makeText(FilterRestaurants.this,"Yoy clicked at "+options[+ position],Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 
+0

此代碼應位於setContentView()的上方getWindow()。setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.header_food); –

+0

@IllegalArgument你指的是哪個代碼? – user3760741

+0

我已經回答了你的問題,請看看 –

回答

1

有一個更簡單的實現你正在嘗試做什麼。你可以試試這個。您不需要適配器,可以放開filterlist.java類和list_filter_restaurants.xml文件。只需在您的xml文件中有一個CheckedTextView對象,下面的代碼將在CheckedTextView的左側設置一個圖像。

Drawable imageDrawable = getBaseContext().getResources.getDrawable(R.drawable.your_image_name); 

((CheckedTextView) chktxtTitle).setCompoundDrawables(imageDrawable,null,null,null); 
+0

是..謝謝這個工作 – user3760741

0

看你的代碼,我發現以下不一致:

  1. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.header_food);filterresturants.java應高於setContentView()
  2. 裏面你FilterList.java您提供錯誤的佈局資源ID在構造函數的super()和01中方法。將其更改爲R.layout.list_filter_restaurants xml
  3. 最後從docs開始,您需要在CheckedTextView內部適配器中將setChoiceMode()設置爲none
+0

我試過了你的方法。但複選框仍然不會出現 – user3760741

+0

用我提出的修復程序更新您的問題。我會再看看。 –

+0

我已經更新了我的代碼。請檢查 – user3760741