【发布时间】:2013-12-08 08:00:41
【问题描述】:
![我正在为一个列表视图使用自定义适配器,其中我有三个项目文本、按钮和单选按钮。借助单选按钮,我一次只能选择一行。现在我想要什么,当我选择使用单选按钮的行 所选单选按钮的特定行应设置为某种颜色。这是我的自定义适配器代码,其中所有项目都在那里。
package com.pramod.customlistviewwithradiobutton;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class CustomAdapter extends ArrayAdapter<Item> {
private final Context context;
private boolean userSelected = false;
private RadioButton mCurrentlyCheckedRB;
private final ArrayList<Item> itemList;
public CustomAdapter(Context context, ArrayList<Item> itemList) {
super(context, R.layout.row_item, itemList);
this.context = context;
this.itemList = itemList;
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// Item rowItem = getItem(position);
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.row_item, parent, false);
// 3. Get the two text view from the rowView
Button btn = (Button) rowView.findViewById(R.id.button1);
TextView tv1 = (TextView) rowView.findViewById(R.id.textView1);
TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
RadioButton radio = (RadioButton) rowView.findViewById(R.id.radioButton1);
// 4. Set the text for textView
tv1.setText(itemList.get(position).getName());
tv2.setText(itemList.get(position).getAddress());
System.out.println(""+getCount());
if (position == getCount() - 1 && userSelected == false) {
// radio.setChecked(true);
mCurrentlyCheckedRB = radio;
} else {
radio.setChecked(false);
}
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "b "+position, Toast.LENGTH_LONG).show();
}
});
radio.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mCurrentlyCheckedRB != null) {
if (mCurrentlyCheckedRB == null)
mCurrentlyCheckedRB = (RadioButton) v;
mCurrentlyCheckedRB.setChecked(true);
Toast.makeText(context, ""+position, Toast.LENGTH_LONG).show();
convertView.setBackgroundColor(Color.BLUE);
}
if (mCurrentlyCheckedRB == v)
return;
mCurrentlyCheckedRB.setChecked(false);
((RadioButton) v).setChecked(true);
mCurrentlyCheckedRB = (RadioButton) v;
}
});
return rowView;
}
}
Here ,Note i don't have to put onclickitem on listview ,because i have a click on button and textview.][1]
【问题讨论】:
标签: android colors android-custom-view