Android 复选框未显示在CheckedTextView中

Android 复选框未显示在CheckedTextView中,android,xml,Android,Xml,我正在尝试创建一个类似以下内容的列表: <image><text> <checkbox>. 。 到目前为止,文本和图像显示,但复选框未显示。大部分代码都是从这里改编的: activity\u filter\u restaurants.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://

我正在尝试创建一个类似以下内容的列表:

<image><text>       <checkbox>.
到目前为止,文本和图像显示,但复选框未显示。大部分代码都是从这里改编的:

activity\u filter\u 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>
<?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>

list\u filter\u 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>
<?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;
    }
}
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();
            }
        });
    }
公共类过滤器列表扩展了ArrayAdapter{
私人最终活动背景;
私有最终字符串[]选项;
私有最终整数[]optionImageId;
CheckedTextView chktxtTitle;
公共筛选器列表(活动上下文、字符串[]选项、整数[]选项图像ID){
超级(上下文、右布局、列表、筛选、餐厅、选项);
this.context=上下文;
this.options=选项;
this.optionImageId=optionImageId;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组父视图){
LayoutInflater充气器=上下文。getLayoutInflater();
视图行视图=充气机。充气(R.layout.list\u filter\u,null,true);
chktxtTitle=(CheckedTextView)rowView.findViewById(R.id.CheckedTextView);
chktxtTitle.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
if(chktxtTitle.isChecked())
chktxtTitle.setChecked(false);
其他的
chktxtTitle.setChecked(true);
}
});
ImageView ImageView=(ImageView)rowView.findViewById(R.id.imgfilter);
chktxtTitle.setText(选项[位置]);
//chktxtTitle.setCheckMarkDrawable(android.R.attr.listChoiceIndicatorMultiple);
setImageResource(optionImageId[position]);
返回行视图;
}
}
filterRestarants.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;
    }
}
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();
            }
        });
    }
公共类筛选器Restarants扩展活动{
ListView过滤器列表;
字符串[]选项={
“素食”、“海鲜”、“西餐”、“印度/南亚”、“清真/伊斯兰”、“中国”、“三明治”,
“熟食店”、“咖啡店”
};
整数[]选项ID={
R.drawable.素食主义者,
海产,
R.drawable.westernfood,
R.drawable.印第安人,
R.drawable.halal,
R.drawable.chinese,
拉丝三明治,
R.drawable.deli,
拉丝咖啡
};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE\u自定义\u标题);
getWindow().setFeatureInt(Window.FEATURE\u CUSTOM\u TITLE,R.layout.header\u food);
setContentView(R.layout.activity\u filter\u餐厅);
CustomList Adapter=新的CustomList(FilterRestarants.this,options,optionId);
filterlist=(ListView)findViewById(R.id.listFilter);
过滤器列表。设置适配器(适配器);
//filterlist.setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
filterlist.setChoiceMode(ListView.CHOICE\u MODE\u NONE);
filterlist.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Toast.makeText(filterRestarants.this),点击“+选项[+位置],Toast.LENGTH_SHORT.show();
}
});
}

查看您的代码,我发现以下不一致之处:

  • getWindow().setFeatureInt(Window.FEATURE\u CUSTOM\u TITLE,R.layout.header\u food);
    filterresturants.java
    中的
  • FilterList.java
    中,您在构造函数的
    super()
    getView()
    方法中提供了错误的布局资源id。将其更改为
    R.layout.list\u filter\u
    xml
  • 最后,从中,您需要在
    CheckedTextView
    内部适配器中将
    setChoiceMode()
    设置为
    none

  • 有一个更简单的实现您正在尝试做的事情。您可以尝试这个。您不需要适配器来实现它,并且放弃
    filterlist.java
    类和
    list\u filter\u restaurants.xml
    文件。只需在xml文件中有一个
    CheckedTextView
    对象,下面的代码就会将图像设置到选中文本视图

    Drawable imageDrawable = getBaseContext().getResources.getDrawable(R.drawable.your_image_name);
    
    ((CheckedTextView) chktxtTitle).setCompoundDrawables(imageDrawable,null,null,null);
    

    这段代码应该在setContentView()getWindow().setFeatureInt(Window.FEATURE\u CUSTOM\u TITLE,R.layout.header\u food);@IllegalArgument你指的是哪段代码?我已经回答了你的问题,请看一看。我尝试了你的方法。但是复选框仍然没有出现。用我建议的修复更新你的问题。我会再看一次。