我正在尝试创建一个我之前已经完成的ExpandableListView,并且我知道它会回收视图,但我试图让视图成为最终的而不是更改。当我选择组视图以展开视图时,组视图有时会更改为另一个组视图的文本。
请有人看看我的代码,看看我做错了什么。
public class ClientListAdapter extends BaseExpandableListAdapter
{
private Context context;
private List<Client> listOfClients;
public ClientListAdapter(Context context, List<Client> listOfClients)
{
this.context = context;
this.listOfClients = listOfClients;
}
private static class ViewHolder
{
TextView name;
TextView ID;
TextView email;
TextView phoneNumber;
Button writeToNFC;
}
@Override
public int getGroupCount()
{
return listOfClients.size();
}
@Override
public int getChildrenCount(int groupPosition)
{
return 1; // There will only ever be 1 child in the group.
}
@Override
public Object getGroup(int groupPosition)
{
return groupPosition;
}
@Override
public Object getChild(int groupPosition, int childPosition)
{
return null;
}
@Override
public long getGroupId(int groupPosition)
{
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
return groupPosition;
}
@Override
public boolean hasStableIds()
{
return true; // Stops child views from resetting.
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
final ViewHolder viewHolder;
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.unexpanded, parent, false);
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView.findViewById(R.id.ClientName);
viewHolder.name.setText(listOfClients.get(groupPosition).getName());
System.out.println("Group name: " + listOfClients.get(groupPosition).getName());
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)
{
final ViewHolder viewHolder;
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.client_details, parent, false);
viewHolder = new ViewHolder();
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.ID = (TextView) convertView.findViewById(R.id.ClientID);
viewHolder.email = (TextView) convertView.findViewById(R.id.ClientEmail);
viewHolder.phoneNumber = (TextView) convertView.findViewById(R.id.ClientTel);
viewHolder.writeToNFC = (Button) convertView.findViewById(R.id.WriteToNFC);
viewHolder.ID.setText(listOfClients.get(groupPosition).getClientID());
viewHolder.email.setText(listOfClients.get(groupPosition).getEmail());
viewHolder.phoneNumber.setText(listOfClients.get(groupPosition).getPhoneNumber());
viewHolder.writeToNFC.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
MainActivity mainActivity = (MainActivity) context;
String ndefMessage = viewHolder.ID.getText().toString();
mainActivity.setNdefMessage(ndefMessage);
Toast.makeText(context, "Client details selected.", Toast.LENGTH_SHORT).show();
System.out.println("NDEF message: " + ndefMessage);
}
});
System.out.println("Group position: " + groupPosition);
System.out.println("Child position: " + childPosition);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return false;
}
}
答案 0 :(得分:2)
我实际上只是设法修复错误。我必须做的是将viewHolder.name函数放在if语句之外,如下所示:
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
final ViewHolder viewHolder;
if(convertView == null)
{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.unexpanded, parent, false);
viewHolder = new ViewHolder();
System.out.println("Group name: " + listOfClients.get(groupPosition).getName());
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.name = (TextView) convertView.findViewById(R.id.ClientName);
viewHolder.name.setText(listOfClients.get(groupPosition).getName());
return convertView;
}
答案 1 :(得分:1)
使用以下覆盖BaseExpandableListAdapter
中的方法public int getChildType(int groupPosition, int childPosition) {
//define a condition here to differentiate between your child views.
}
public int getChildTypeCount() {
return 2; <-- this is the number of views for your child
}
希望这会有所帮助。