我有一个列表视图。列表视图的每一行包含两个文本(名称和地址),一个按钮和一个单选按钮。我正在使用CustomAdapter。现在我有条件;如果text1(name)等于“Pramod”,那么我必须从整行中删除单选按钮。
到底发生了什么?只有该行的单选按钮正在删除。
我必须删除所有行的单选按钮。我该如何解决这个问题?
这是我的customAdapter类代码:
public class CustomAdapter extends ArrayAdapter<Item> {
private final Context context;
// private boolean userSelected = false;
public static RadioButton mCurrentlyCheckedRB;
private final ArrayList<Item> itemList;
int selected_itemindex=-1;
static String abc="";
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
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
final 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);
final TextView tv2 = (TextView) rowView.findViewById(R.id.textView2);
final 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());
// This is the code I am using to delete the radio button from the entire row
if (itemList.get(position).getName().toString().equals("Pramod")) {
radio.setVisibility(View.INVISIBLE);
}
if (itemList.get(position).isSelected()) {
radio.setChecked(true);
rowView.setBackgroundColor(Color.CYAN);
}
else {
rowView.setBackgroundColor(Color.WHITE);
radio.setChecked(false);
}
// The radio button I am using to select a particular row one at a time.
radio.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (itemList.get(position).isSelected()) {
}
else
{
int selected_previousval = -1;
for (int i=0; i<itemList.size(); i++) {
if (itemList.get(i).isSelected()) {
selected_previousval = i;
break;
}
}
String name2=itemList.get(position).getName();
String add2=itemList.get(position).getAddress();
if (selected_previousval==-1) {
itemList.remove(position);
itemList.add(position,new Item(name2, add2, true));
rowView.setBackgroundColor(Color.CYAN);
Toast.makeText(context, "selected a 3 row",2000).show();
radio.setChecked(true);
}
else {
itemList.remove(position);
itemList.add(position, new Item(name2, add2, true));
name2 = itemList.get(selected_previousval).getName();
add2 = itemList.get(selected_previousval).getAddress();
itemList.remove(selected_previousval);
itemList.add(selected_previousval, new Item(name2, add2, false));
MainActivity.lv.setAdapter(new CustomAdapter(context, itemList));
}
}
}
});
return rowView;
}
}
这是我的Arraylist课程:
public class Item {
private String Name;
private String Address;
public boolean selected;
public boolean isSelected() {
return this.selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public Item(String Name, String Address,boolean selected) {
super();
this.Name = Name;
this.Address = Address;
this.selected=selected;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
}
答案 0 :(得分:1)
<强>更新强>
问题:如果text = pramod,我想从listview的every
行中删除radiobutton。
答案:
ArrayList<Item> items;
CustomAdapter adapter;
items = new ArrayList<Item>();
ArrayList<String> temparray;
items.add(new Item("Pramod", "Ballia", false, 0));
items.add(new Item("Pankaj", "Mau", false, 1));
items.add(new Item("Pradeep", "Ranchi", false, 1));
items.add(new Item("Jitendra", "Varansi", false, 1));
items.add(new Item("Amresh", "Sonbhadra", false, 1));
items.add(new Item("Anil", "Sarnath", false, 1));
temparray = new ArrayList<String>();
for(int i=0; i<items.size(); i++)
{
temparray.add(items.get(i).getName());
}
if (temparray.contains("Pramod"))
{
Log.d("temparray", "contains Pramod");
Global.show=0;
}
else
{
Log.d("temparray", "not contain");
Global.show=1;
}
lv = (ListView)dp.findViewById(R.id.listView1);
Spinner sp = (Spinner) dp.findViewById(R.id.spinner1);
adapter = new CustomAdapter(this, items);
lv.setAdapter(adapter);
if (Global.show == 0)
{
radio.setVisibility(View.INVISIBLE);
}
else
{
radio.setVisibility(View.VISIBLE);
if (itemList.get(position).isSelected()) {
radio.setChecked(true);
rowView.setBackgroundColor(Color.CYAN);
}
else {
rowView.setBackgroundColor(Color.WHITE);
radio.setChecked(false);
}
}
public static class Global {
Public static int show;
}