gpt4 book ai didi

android - ExpandableListView 组 View 在 Android 中选中时发生变化

转载 作者:行者123 更新时间:2023-11-29 17:36:11 26 4
gpt4 key购买 nike

我正在尝试创建一个我以前做过的 ExpandableListView,我知道它会回收 View ,但我正在尝试让 View 成为最终 View 而不是更改。当我选择组 View 展开 View 时,组 View 有时会更改为另一个组 View 的文本。

请有人看看我的代码,看看我做错了什么。

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;
}

}

最佳答案

实际上我只是设法修复了错误。我必须做的是将 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;
}

关于android - ExpandableListView 组 View 在 Android 中选中时发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30459037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com