我正在尝试根据项目的值更改片段中ListView
中列表项的颜色。任何建议都会很棒。我已经尝试了getView()
的一些选项,但没有运气。
String[] validvalues = new String[] {personalList, doctorsList,carerList,
myMedList,mymedproblist,kinList,relyList,allergiesList
};
ArrayAdapter<String> files2 = new ArrayAdapter<String>(
getActivity(), android.R.layout.simple_list_item_1,validvalues);
View view2 = inflater.inflate(R.layout.verify, container, false);
list.setAdapter(files2);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view2,int position, long id) {
String str = list.getItemAtPosition(position).toString();
Toast.makeText(getActivity(), position + " " + str,Toast.LENGTH_SHORT).show();
}
});
像string.contains(“!!”)&gt;将背景颜色设置为红色。
答案 0 :(得分:1)
创建自定义Adapter类(可能从它派生),而不是使用ArrayAdapter。然后,在getView()方法中,设置返回视图的背景颜色。例如,只需使用以下代码更改代码中的ArrayAdapter创建:
ArrayAdapter<String> files2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,validvalues)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View itemView = super.getView(position, convertView, parent);
if (getItem(position).contains("!!"))
itemView.setBackgroundColor(Color.RED);
return itemView;
}
};
答案 1 :(得分:0)
您需要创建自己的Adapter
。
我会扩展ArrayAdapter
并覆盖getView()
,使其看起来像这样:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Note: you could get a LayoutInflater and inflate your own layout here instead
// of calling through to super.
View row = super.getView(position, convertView, parent);
String content = getItem(position);
if (content.equals("StringToLookFor") {
row.setBackgroundColor(yourRowColor);
} else {
row.setBackgroundColor(yourOtherRowColor);
}
}
答案 2 :(得分:0)
在这种情况下,您必须定义自己的自定义适配器
public class myadapter extends ArrayAdapter<String>
{
public myadapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return validvalues.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// here you can change color
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.yourcustomlayout, null);
}
if (validvalues[position].equals( ""))
v.setBackgroundColor(color)
return super.getView(position, convertView, parent);
}
}
答案 3 :(得分:0)
使用这段代码,我给了你一个偶数和奇数位置的例子(你可以像你一样改变位置上的条件)
ArrayAdapter<String> adapter = new MyListAdapter();
listView = (ListView) findViewById(R.id.myListView);
listView.setAdapter(adapter);
这是MyListAdapter的实现
public class MyListAdapter extends ArrayAdapter<IArtVO> {
private class MyListAdapter extends ArrayAdapter<INoteVO> {
public MyListAdapter() {
super(context, R.layout.ifact1_help2, R.layout.ifact1_help1, liste);
}
@Override
public int getCount() {
return yourListe.size();
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return 1;
}
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (position % 2 == 0) {
itemView = getLayoutInflater().inflate(R.layout.textViewRed, parent, false);
itemView.setText(" your text");
} else {
itemView = getLayoutInflater().inflate(R.layout.textViewGreen, parent, false);
itemView.setText(" your text");
}
return itemView;
}
}
这里是textViewRed.xml和green,你必须在目录布局上定义
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:paddingLeft="10dp"
android:textColor="@color/black"
android:background="@color/red"
/>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:paddingLeft="10dp"
android:textColor="@color/black"
android:background="@color/green"
/>