2

我试图将图像图标添加到expandablelistview子列表。我无法正确执行此操作。 在我的应用程序中,子列表从API(JSON)加载数据。如何将图像图标添加到expandablelistview子列表

我想添加图像到不同的数据。例如address.png +地址就像智者一样。

任何人都可以帮助我创建它吗?

这是我的主要活动代码。

public void ListDrwaer() { 

     listDataHeader = new ArrayList<String>(); 
     listDataChild = new HashMap<String, List<String>>(); 
     List<String> restData; 

     try { 
      JSONObject jsonResponse = new JSONObject(strJson1); 
      JSONArray jsonMainNode = jsonResponse.optJSONArray("restaurants"); 



      for (int i = 0; i < jsonMainNode.length(); i++) { 

       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
       String restName = jsonChildNode.optString("name"); 
       String address = "Address: "+jsonChildNode.optString("address"); 
       String mobile = "Contact No: "+jsonChildNode.optString("mobile"); 
       String direction = "Direction: "+jsonChildNode.optString("direction"); 
       String bestTime = "BestTime to visite: "+jsonChildNode.optString("bestTime"); 
       String food = "Food: "+jsonChildNode.optString("food"); 
       String dress = "Dress: "+jsonChildNode.optString("dress"); 
       String priceRange = "Price Range: "+jsonChildNode.optString("priceRange"); 
       //String url = jsonChildNode.optString("priceRange"); 

       listDataHeader.add(restName); 
       restData = new ArrayList<String>(); 
       restData.add(address); 
       restData.add(mobile); 
       restData.add(direction); 
       restData.add(bestTime); 
       restData.add(food); 
       restData.add(dress); 
       restData.add(priceRange); 

       listDataChild.put(restName, restData); 

      } 


     } catch (JSONException e) { 
      Toast.makeText(getApplicationContext(), "Error..." + e.toString(), 
        Toast.LENGTH_LONG).show(); 
     } 

     listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild); 

     // setting list adapter 
     expListView.setAdapter(listAdapter); 


    } 


} 

这是Adapter类

public class ExpandableListAdapter extends BaseExpandableListAdapter { 

    private Context _context; 
    private List<String> _listDataHeader; // header titles 
    // child data in format of header title, child title 
    private HashMap<String, List<String>> _listDataChild; 

    ImageView image; 

    public ExpandableListAdapter(Context context, List<String> listDataHeader, 
      HashMap<String, List<String>> listChildData) { 
     this._context = context; 
     this._listDataHeader = listDataHeader; 
     this._listDataChild = listChildData; 
    } 

    Integer[] images = { R.drawable.address, 
      R.drawable.clock, R.drawable.location }; 

    @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.list_item, null); 
     } 

     TextView txtListChild = (TextView) convertView 
       .findViewById(R.id.lblListItem); 


     txtListChild.setText(childText); 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int 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); 
     } 

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



} 

这是孩子列表XML文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 


    <TextView 
     android:id="@+id/lblListItem" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="14sp" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:textColor="#daac56" 
     android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" /> 
    <ImageView 
     android:id="@+id/img" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_alignParentLeft="true"/> 

</LinearLayout> 
+0

您已经将地址添加到了“TextView”中。这与'ImageView'是一样的。只需找到视图并设置你的图像。我真的不明白你的问题。你能解释一下你的意思吗?“我无法正确地做到这一点”? – owe

+0

是的。我想添加图像视图然后文本视图。我无法理解如何将imageview添加到此子列表中。 – anuruddhika

+0

@anuruddhika你有没有找到解决方案?我也有同样的问题。 –

回答

0

你已经在你的BaseExpandlableListAdapter通过以下行添加文本到您的TextView

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

对您的图像做同样的处理:

ImageView imgViewChild = (ImageView) convertView 
       .findViewById(R.id.img);  
imgViewChild.setImageResource(R.drawable.address); 
+0

我应该添加7张图片。那么我该怎么做?例如地址图片+地址,然后联系图标+联系号码等等。 – anuruddhika

+0

只需在设置图像之前制作一条if语句...如何确定应该显示哪7个图像? – owe