我使用ExpandableListview
...我可以将从Web服务检索的值设置为子布局的单个Textview
。
现在需要在Web服务的子布局中设置两个不同的Textviews
值
我可以使用单个textviews,但没有足够的想法来使用两个Textview
MainActivity.java
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
cat = jsonObj.getJSONArray(All_link.TAG_NEWS);
String err = jsonObj.getString(All_link.TAG_ERROR);
Log.e("------>Error", String.valueOf(err));
if (err.equals("1")) {
} else {
// Adding child data
listDataHeader.add("Home");
listDataHeader.add("News Category");
listDataHeader.add("");
listDataHeader.add("My Favourites");
listDataHeader.add("Story-Photo-Video");
listDataHeader.add("About Us");
listDataHeader.add("Rate this App");
listDataHeader.add("Share this App");
listDataHeader.add("Settings");
// looping through All Contacts
for (int i = 0; i < cat.length(); i++) {
JSONObject c = cat.getJSONObject(i);
String cat_id = c.getString(All_link.TAG_CAT_ID);
String cat_name = c
.getString(All_link.TAG_CAT_NAME);
String image_icon = c.getString("image_icon");
/*navDrawerItems.add(new NavDrawerItem(cat_name,
image_icon, navMenuIcons.getResourceId(
1, -1), true
,"1",id));*/
list_items.add(cat_name);
Log.e("list_items", "karlist "+list_items);
}
listDataChild.put(listDataHeader.get(0), new ArrayList<String>()); // No child
listDataChild.put(listDataHeader.get(1), list_items); // With child
listDataChild.put(listDataHeader.get(2), new ArrayList<String>()); // No child
listDataChild.put(listDataHeader.get(3), new ArrayList<String>()); // No child
listDataChild.put(listDataHeader.get(4), new ArrayList<String>()); // No child
listDataChild.put(listDataHeader.get(5), new ArrayList<String>()); // No child
listDataChild.put(listDataHeader.get(6), new ArrayList<String>()); // No child
listDataChild.put(listDataHeader.get(7), new ArrayList<String>()); // No child
listDataChild.put(listDataHeader.get(8), new ArrayList<String>()); // No child
ExpandableListAdapter.java
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private static final int[] EMPTY_STATE_SET = {};
private static final int[] GROUP_EXPANDED_STATE_SET =
{android.R.attr.state_expanded};
private static final int[][] GROUP_STATE_SETS = {
EMPTY_STATE_SET, // 0
GROUP_EXPANDED_STATE_SET // 1
};
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.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 String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.drawer_list_item, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.title);
TextView txtListChild_id = (TextView) convertView.findViewById(R.id.txt_id);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
Log.e("groupPosition", "kargr "+groupPosition);
return this._listDataChild.get(this._listDataHeader.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) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
View ind = convertView.findViewById( R.id.explist_indicator);
if( ind != null ) {
ImageView indicator = (ImageView)ind;
if( getChildrenCount( groupPosition ) == 0 ) {
indicator.setVisibility( View.INVISIBLE );
} else {
indicator.setVisibility( View.VISIBLE );
int stateSetIndex = ( isExpanded ? 1 : 0) ;
Drawable drawable = indicator.getDrawable();
drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
}
}
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;
}
}
最佳答案
您需要使用Class来处理您的子数据:
public class YourData {
public String cat_name;
public String cat_id;
public YourData(String id, String name) {
cat_id= id;
cat_name = name;
}
}
现在您的孩子列表将如下所示:
private List<YourData> _listDataHeader; // header titles
您可以将该列表填写为:
_listDataHeader.add(new YourData(id,name));
将适配器签名更改为:
public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<YourData>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
并在GetView中将其用作:
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final YourData childText = (YourData) getChild(groupPosition, childPosition);
....
txtListChild.setText(childText.cat_name);
txtListChild_id.setText(childText.cat_id);
return convertView;
}