gpt4 book ai didi

android - 如何在 fragment 中使用带有自定义类对象的可扩展 ListView ?

转载 作者:行者123 更新时间:2023-11-29 21:24:43 24 4
gpt4 key购买 nike

我有一个 fragment ,其中放置了一个可扩展 ListView 。我有一个模态类,其中包含产品列表。在可扩展 ListView 中,在标题上我想设置我已成功放置的部分名称,小时候我想放置不同部分名称下的产品。

但是当我应用我的逻辑时,我得到了唯一的一个产品。这就是我得到的......!

enter image description here

这里我得到了唯一一个在所有产品中重复的产品名称。

这是我到目前为止所尝试的::

适配器::

class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private ArrayList<String> _listDataHeader; // header titles
ArrayList<Products> topupProducts;

public ExpandableListAdapter(Context context, ArrayList<String> listDataHeader,ArrayList<Products> topupProducts)
{
this._context = context;
this._listDataHeader = listDataHeader;
this.topupProducts = topupProducts;
}

@Override
public Products getChild(int groupPosition, int childPosititon) {

/*return this.topupProducts.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);*/
return topupProducts.get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {

// child = topupProducts.get(groupPosition).getProductName();
//final String childText = (String) getChild(groupPosition, childPosition);
final String childText = topupProducts.get(groupPosition).getProductName();

if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item_fragment, null);
}

convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(_context, topupProducts.get(childPosition).getProductID(),Toast.LENGTH_SHORT).show();
}
});

TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
return this.topupProducts.size();
//return ((ArrayList<String>) topupProducts.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
{
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}

TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);

return convertView;
}

@Override
public boolean hasStableIds() {
return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

任何帮助将不胜感激..提前致谢...

编辑::

这就是我从本地数据库 (SQlite) 中获取我的产品部分名称的方式

listHeader = new LinkedHashMap<String, String>();
DataHelper dataHelper=new DataHelper(ctx);
topupProSectionsName=new ArrayList<String>();
topupProSectionID=new ArrayList<String>();
listHeader= dataHelper.getSectionSForTopupProduct();

if (listHeader != null)
{
Set entrySet = listHeader.entrySet();
Iterator i = entrySet.iterator();
while (i.hasNext())
{
Map.Entry me = (Map.Entry)i.next();
topupProSectionsName.add((String) me.getKey());
topupProSectionID.add((String) me.getValue());
addProduct((String)me.getKey(),listDataChild);

}
}

MyModal 类::

public class TopupProSectionName 
{
private String sectionName;
private ArrayList<Products> topupProductList = new ArrayList<Products>();

public String getsectionName() {
return sectionName;
}
public void setsectionName(String sectionName) {
this.sectionName = sectionName;
}

public ArrayList<Products> gettopupProductList() {
return topupProductList;
}
public void settopupProductList(ArrayList<Products> topupProductList) {
this.topupProductList = topupProductList;
}
}

AddProduct 方法::

private int addProduct(String department,ArrayList<Products> product)
{
int groupPosition = 0;

TopupProSectionName headerInfo = myDepartments.get(department);
if(headerInfo == null)
{
headerInfo = new TopupProSectionName();
}
headerInfo.setsectionName(department);
myDepartments.put(department, headerInfo);
deptList.add(headerInfo);

ArrayList<Products> productList = headerInfo.gettopupProductList();
int listSize = productList.size();
listSize++;

Products detailInfo = new Products();
detailInfo.setProductName(product.get(groupPosition).getProductName());
productList.add(detailInfo);
headerInfo.settopupProductList((productList));

groupPosition = deptList.indexOf(headerInfo);
return groupPosition;
}

改进的可扩展适配器::

class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
ArrayList<Products> topupProducts;
ArrayList<TopupProSectionName> listDataHeader;
public ExpandableListAdapter(Context context, ArrayList<TopupProSectionName> listDataHeader)
{
this._context = context;
this.listDataHeader = listDataHeader;
this.topupProducts = topupProducts;
}

@Override
public Products getChild(int groupPosition, int childPosititon) {

ArrayList<Products> productList = deptList.get(groupPosition).gettopupProductList();
return productList.get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {


final Products childText = (Products) getChild(groupPosition, childPosition);


if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item_fragment, null);
}

convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
//Toast.makeText(_context, topupProducts.get(childPosition).getProductID(),Toast.LENGTH_SHORT).show();
}
});

TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
txtListChild.setText(childText.getProductName());
return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {

ArrayList<Products> productList = deptList.get(groupPosition).gettopupProductList();
return productList.size();
}

@Override
public Object getGroup(int groupPosition) {

return deptList.get(groupPosition);
}

@Override
public int getGroupCount() {

return deptList.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent)
{

TopupProSectionName headerTitle = (TopupProSectionName) getGroup(groupPosition);
if (convertView == null)
{
LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}

TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle.getsectionName());

return convertView;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}

最佳答案

结构应该是这样的:

List<String> groupList = new ArrayList<String>();
groupList.add("Group 1");
groupList.add("Group 2");

List<List<String>> childList = new ArrayList<List<String>>();
List<String> group1Childs = new ArrayList<String>();
group1Childs.add("Child 1 of Group 1");
group1Childs.add("Child 2 of Group 1");
childList.add(group1Childs);
List<String> group2Child = new ArrayList<String>();
group2Childs.add("Child 1 of Group 2");
group2Childs.add("Child 2 of Group 2");
childLists.add(group2Childs);

在你的适配器中:

public int getChildCount(int groupPosition) {
return childList.get(groupPosition).size();
}

public String getChild(int groupPosition, int childPosition) {
return childList.get(groupPosition).get(childPosition);
}

希望你明白我的意思。这只是可扩展列表的一个简单示例,在您的情况下,您应该更改 StringProducts .

我做了什么

我曾经用过一个List结构如:

public class ExpandableItem {
private String groupName;
private List<String> childs;
}

然后我通过了List<ExpandableItem>Adapter构造函数为 _items

public int getChildCount(int groupPosition) {
return _items.get(groupPosition).getChilds().size();
}

public String getChild(int groupPosition, int childPosition) {
return _items.get(groupPosition).getChilds().get(childPosition);
}

我不知道这是否是一个好的做法,但它确实有效。

关于android - 如何在 fragment 中使用带有自定义类对象的可扩展 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20344941/

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