更改数组适配器中列表项的文本颜色

本教程将介绍更改数组适配器中列表项的文本颜色的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

问题描述

我创建了一个列表视图,并在我的自定义对话框中实现了该列表视图。该列表视图使用数组适配器,而在我的数组适配器中,我使用自己的布局和所需的颜色。代码如下所示。

 listView = new ListView(context);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
R.layout.my_spinner_layout, items);
listView.setAdapter(adapter);

在此处,单击”我的列表项目工作正常”的监听程序。

问题现在开始。我需要在我的自定义警报对话框内的列表视图,每一行都包含一个单选按钮。我也用同样的方法。以下是我的代码。

listView = new ListView(context);
  ArrayAdapter<String>adapter = new ArrayAdapter<String>(context,R.layout.my_single_choice_layout, choice);
  listView.setAdapter(adapter);

此处可以同时选中所有单选按钮。我的监听程序工作不正常。

My_SPINER_Layout_XML

<?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:paddingTop="10dp"
  android:paddingBottom="10dp"
  android:paddingLeft="10dp"
  style="@style/ListItemTextColor"
  />

和My_Single_Choose_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:id="@+id/my_choice_radio"
 android:layout_height="match_parent"
 android:button="@null"
 android:drawableRight="@android:drawable/btn_radio"
 android:text="Option"
 style="@style/ListItemTextColor" >


</RadioButton>

推荐答案

试试:

list.setAdapter(new EfficientAdapter(context,R.layout.my_single_choice_layout, choice));

然后创建一个类

 public class EfficientAdapter extends ArrayAdapter {

private LayoutInflater mInflater;

private String[] mStrings;

private int mViewResourceId;

public EfficientAdapter(Context ctx, int viewResourceId,String[] strings) {
 super(ctx, viewResourceId, strings);

 mInflater = (LayoutInflater)ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
 mStrings = strings;

 mViewResourceId = viewResourceId;
}
@Override
public int getCount() {
 return mStrings.length;
}

@Override
public String getItem(int position) {
 return mStrings[position];
}

@Override
public long getItemId(int position) {
 return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
 convertView = mInflater.inflate(mViewResourceId, null);

 convertView.setMinimumHeight(132);
 TextView tv = (TextView)convertView.findViewById(R.id.option_text); //Give Id to your textview
 tv.setText(mStrings[position]);
  tv.setTextColor(Color.RED);
 RadioButtons r=(RadioButtons)convertview.findviewById(Radio button id);
 r.setOnCheckedListener(new ur listener()
{
/////////Do whatever you wanna do overhere
});

 return convertView;
}

 }

希望它能有所帮助。

好了关于更改数组适配器中列表项的文本颜色的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。

0
没有账号?注册  忘记密码?