gpt4 book ai didi

android - 复选框未显示在 CheckedTextView Android 中

转载 作者:行者123 更新时间:2023-11-29 01:41:30 33 4
gpt4 key购买 nike

我正在尝试创建一个类似这样的列表:

<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>

过滤列表.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();
}
});
}

最佳答案

您尝试做的事情有一个更简单的实现。你可以试试这个。为此,您不需要适配器并放弃 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);

关于android - 复选框未显示在 CheckedTextView Android 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24331099/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com