简单的ListView选择题的Android [英] simple ListView Multiple choice Android
问题描述
我开发板球的应用程序。我的要求是这样的,如果我选择团队1个可用的国名的列表中有显示,如果我从印度选择国名印度选手的名单显示有和,我有选择的多的球员。我所做的一切。但我的问题是我使用android.R.layout.simple_list_item_multiple_choice选择的球员。我使用的简单列表视图和列表的背景是黑色图像。而我的列表视图就是这样
< ListView控件
机器人:ID =@ + ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =0dp
机器人:layout_weight =8.5
机器人:cacheColorHint =#00000000 />
现在的问题是,在ListView值呈现黑色。我已经有黑色的背景图像。和的值也黑色。所以它不是看起来很不错。如何列表视图值的颜色更改为白色无改变O自定义的适配器。
这是我的适配器类
适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_multiple_choice,playersName);
lvview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lvview.setAdapter(适配器);
您必须创建Custome 的TextView
来改变所有的ListView项目颜色
android.R.layout.simple_list_item_multiple_choice 到 ArrayAdapter
你应该通过自定义>列表项的XML,其具有不同TEXTCOLOR属性。
例如,在文件夹中创建布局custom_list_item.xml:
<?XML版本=1.0编码=UTF-8&GT?;
< CheckedTextView的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / TextView的
机器人:layout_width =FILL_PARENT
机器人:layout_height =机器人:ATTR /列表preferredItemHeight
机器人:textAppearance =机器人:ATTR / textAppearanceLarge
机器人:重力=center_vertical
安卓:勾选=机器人:ATTR / listChoiceIndicatorSingle
机器人:paddingLeft =6DIP
机器人:paddingRight =6DIP
机器人:文字颜色=#FF00FF
/>
然后传递它像适配器如下:
新ArrayAdapter<串GT;(这一点,R.layout.custom_list_item,playersName);
编辑:
下面是code这是工作的罚款我已经测试了。
lv.setAdapter(新ArrayAdapter<串GT;(这一点,R.layout.custom_list_item,playersName));
lv.setBackgroundColor(Color.BLACK);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(新OnItemClickListener()
{
@覆盖
公共无效onItemClick(适配器视图<> p_arg0,查看p_arg1,INT p_arg2,长p_arg3)
{
my_sel_items =新的String(选定项目);
SparseBooleanArray一个= lv.getCheckedItemPositions();
的for(int i = 0; I< a.size();我++){
如果(a.valueAt(ⅰ)){
my_sel_items = my_sel_items +,
+(字符串)lv.getAdapter()的getItem(ⅰ)。
}
}
Log.v(价值观,my_sel_items);
}
});
块引用>列表视图的布局
< ListView控件
机器人:ID =@ + ID /安卓名单
机器人:layout_marginTop =60dip
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:文字颜色=#000000
/>
块引用>I am developing an app for cricket. My requirement is like this, if i select team 1 the list of available country name has to display and if i select a country name as India the list of player from India has to displayed and in that i have select a multiple players from that. I have done everything. But my problem is i am using android.R.layout.simple_list_item_multiple_choice for selecting players. I am using simple list view and the background of that list is black image. And my listview is like that
<ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="8.5" android:cacheColorHint="#00000000" />
Now the problem is the listview value is showing black in color. Already i have black background image. And the value also black in color. So its not looking good. How to change the color of listview values to white without changing o custom adapter.
And this is my adapter class
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,playersName); lvview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lvview.setAdapter(adapter);
解决方案You have to create Custome
TextView
to change color of allListView items
, instead of passing defaultandroid.R.layout.simple_list_item_multiple_choice
toArrayAdapter
you should pass custom list item XML, which has different TextColor attribute.For example, created custom_list_item.xml under folder Layout:
<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="center_vertical" android:checkMark="?android:attr/listChoiceIndicatorSingle" android:paddingLeft="6dip" android:paddingRight="6dip" android:textColor="#FF00FF" />
Then passed it to adapter like as below:
new ArrayAdapter<String>(this, R.layout.custom_list_item, playersName);
EDITED:
Here is the code which is working fine i have tested.
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_list_item, playersName)); lv.setBackgroundColor(Color.BLACK); lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> p_arg0, View p_arg1, int p_arg2, long p_arg3) { my_sel_items = new String("Selected Items"); SparseBooleanArray a = lv.getCheckedItemPositions(); for (int i = 0; i < a.size(); i++) { if (a.valueAt(i)) { my_sel_items = my_sel_items + "," + (String) lv.getAdapter().getItem(i); } } Log.v("values", my_sel_items); } });
Layout of listview
<ListView android:id="@+id/android:list" android:layout_marginTop="60dip" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="#000000" />
这篇关于简单的ListView选择题的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!