我的基本适配器中有分段单选按钮。我试图在用户点击这些按钮时设置文本,但它不起作用。我有空指针异常,但我使用Toast消息来显示值,它正在工作。
完整的基础适配器
class MyAdapter extends BaseAdapter implements
RadioGroup.OnCheckedChangeListener, View.OnClickListener {
// Declare Variables
Context context;
LayoutInflater inflater;
private boolean[] expandedView;
private List<AnywallPost> AnywallPostlist2 = null;
private ArrayList<AnywallPost> arraylist;
int mSelectedPos = -1;
public MyAdapter(Context context, List<AnywallPost> AnywallPostlist2) {
this.context = context;
this.AnywallPostlist2 = AnywallPostlist2;
inflater = LayoutInflater.from(context);
this.arraylist = new ArrayList<AnywallPost>();
this.arraylist.addAll(AnywallPostlist2);
this.expandedView = new boolean[AnywallPostlist2.size()];
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
public class ViewHolder {
TextView Name;
ImageView falg;
TextView TVal;
SegmentedGroup segmented;
// TextView book1;
RadioButton b1;
RadioButton b2;
RadioButton b3;
TextView lt1;
TextView lt2;
TextView lt3;
}
@Override
public int getCount() {
return AnywallPostlist2.size();
}
@Override
public Object getItem(int position) {
return AnywallPostlist2.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getViewTypeCount() {
return 2; // brief & expanded
}
@Override
public int getItemViewType(int position) {
return expandedView[position] ? 1 : 0;
}
public View getView(final int position, View view, ViewGroup parent) {
// final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
int layout = 0;
switch (getItemViewType(position)) {
case 0:
layout = R.layout.custum_alertitem;
break;
case 1:
layout = R.layout.custum_alertitem2;
break;
}
view = inflater.inflate(layout, parent, false);
switch (getItemViewType(position)) {
case 0:
holder.Name = (TextView) view.findViewById(R.id.textViewserviceName);
holder.falg = (ImageView) view.findViewById(R.id.imageViewserviceImg);
break;
case 1:
holder.Name = (TextView) view.findViewById(R.id.textViewserviceName);
holder.falg = (ImageView) view.findViewById(R.id.imageViewserviceImg);
holder.TVal = (TextView) view.findViewById(R.id.textViewValue);
holder.segmented = (SegmentedGroup) view.findViewById(R.id.segmentedmode);
holder.lt1 = (TextView) view.findViewById(R.id.textViewlate1);
holder.lt2 = (TextView) view.findViewById(R.id.textViewlate2);
holder.lt3 = (TextView) view.findViewById(R.id.textViewlate3);
break;
}
view.setTag(holder);
} else {
// because the view type is determined by expandedView[position],
// the correct layout will be recycled here
holder = (ViewHolder) view.getTag();
}
switch (getItemViewType(position)) {
case 0:
holder.Name.setText(AnywallPostlist2.get(position).get("name").toString());
String st = AnywallPostlist2.get(position).get("status").toString().trim();
if (st.equals("true"))
{
holder.falg.setImageResource(R.drawable.green);
} else {
holder.falg.setImageResource(R.drawable.red);
}
break;
case 1:
holder.Name.setText(AnywallPostlist2.get(position).get("name").toString());
String st = AnywallPostlist2.get(position).get("status").toString().trim();
if (st.equals("true"))
{
holder.falg.setImageResource(R.drawable.green);
} else {
holder.falg.setImageResource(R.drawable.red);
}
break;
holder.segmented.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.button31:
if (mSelectedPos != position) {
// holder.tvMsg.setColor(Color.RED);
holder.TVal.setText("1");
}
break;
case R.id.button32:
if (mSelectedPos != position) {
holder.TVal.setText("2");
}
break;
case R.id.button33:
if (mSelectedPos != position) {
holder.TVal.setText("3");
}
break;
}
}
});
break;
}
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (expandedView[position]) {
// this is expanded view so toggle it back to brief view
expandedView[position] = false;
} else {
// clear any expanded views elsewhere
expandedView = new boolean[AnywallPostlist2.size()];
// toggle brief view to expanded
expandedView[position] = true;
}
MyAdapter.this.notifyDataSetChanged();
}
});
return view;
}
}