我的适配器(其风格一直适用于我的应用程序的其余部分)现在给我带来了问题。这是第一个具有可切换按钮的列表。每个 listItem 都有一个 toggleButton,当我切换、向下滚动然后返回时,它会自动变为未选中状态。在过去的两天里,我一直在尝试与 SO 不同的解决方案,从禁用回收到添加视图持有者,但都没有成功。
我知道代码可能看起来有点乱,但希望它仍然可以理解。变量已更改,我很确定我得到了每个实例,但我主要需要逻辑/语法方面的帮助。我没有展示我拥有的一些 ViewHolder 代码,主要是因为这只会使它更加混乱。
我发送的 String[]s 在每个中都发送了等量的字符串值。
public class MyListAdapter extends BaseAdapter
{
Activity activity;
/**
* String[] of sender Data
*/
String[] Id, Name, Alert, Instructions, RedText, dawcb; //Alert, Name, and a toggleButton are to be displayed in list
TextView status, date, prescription;
ImageView AlertIcon;
ToggleButton tb;
/**
* Index of Selected Item
*/
private int _index = -1;
/**
* Inflate Layout in Application Context
*/
private static LayoutInflater inflater = null;
public MultiRenewAdapter(Activity a, String[] Id, String[] Name, String[] Alert, String[] Instructions, String[] RedText, String[] dawcb)
{
activity = a;
this.Id = Id;
this.Name = Name;
this.Alert = Alert;
this.Instructions = Instructions;
this.RedText = RedText;
this.dawcb = dawcb;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//some code to help save selected items
static class ViewHolder {
protected ImageView alertIcon;
protected TextView text;
protected ToggleButton tb;
}
public int getCount()
{
return Name.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
// I thought I could try turning off recycling (this is from another post here):
// @Override
// public int getViewTypeCount() {
// //Count=Size of ArrayList.
// return Name.length;
// }
//
// @Override
// public int getItemViewType(int position) {
//
// return position;
// }
/**
* Override Get View for custom format
*/
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View vi = convertView;//vi and convertView are null
ViewHolder viewHolder = null;
vi = inflater.inflate(R.layout.row_layout, null);
viewHolder = new ViewHolder();
positionInt = position;
//doesn't work since viewHolder is null, and is causing a null pointer exception
//viewHolder.tb = (ToggleButton) vi.findViewById(R.id.toggleButton);
//viewHolder.prescription = (TextView) //vi.findViewById(R.id.prescription);
//viewHolder.AlertIcon = (ImageView) vi.findViewById(R.id.AlertIcon);
if (position == _index)
{
vi.setBackgroundColor(activity.getResources().getColor(R.color.DeepSkyBlue));
}
else
{
vi.setBackgroundColor(activity.getResources().getColor(R.color.transparent));
if (position % 2 == 0)
{
vi.setBackgroundColor(Color.TRANSPARENT);
}
else
{
vi.setBackgroundColor(Color.LTGRAY);
}
}
if(convertView==null)
{
if (position % 2 == 0)
{
vi.setBackgroundColor(Color.TRANSPARENT);
}
else
{
vi.setBackgroundColor(Color.LTGRAY);
}
}
tb = (ToggleButton) vi.findViewById(R.id.toggleButton);
prescription = (TextView) vi.findViewById(R.id.prescription);
AlertIcon = (ImageView) vi.findViewById(R.id.AlertIcon);
if(Alert[position].equals("4")){
AlertIcon.setBackgroundResource(R.drawable.checktemp);
}else if(Alert[position].equals("3")){
AlertIcon.setBackgroundResource(R.drawable.orange_diamond_temp);
}else if(Alert[position].equals("2")){
AlertIcon.setBackgroundResource(R.drawable.exclamation_red_temp);
}else if(Alert[position].equals("1")){
AlertIcon.setBackgroundResource(R.drawable.red_x_temp);
} else {
AlertIcon.setVisibility(View.INVISIBLE);
}
prescription.setText(Name[position]);
return vi;
}
public void setClicked(int index)
{
_index = index;
this.notifyDataSetChanged();
}
}
我期待着任何和所有的帮助。
谢谢!