2017-03-06 2 views
0

J'ai visité le lien avec le même sujet mais aucun d'entre eux ne répond à ma question. Je ne veux pas créer une vue de liste extensible personnalisée en utilisant simplement la vue de liste extensible simple. J'ai réussi à récupérer des données de la base de données mysql et vérifié toutes les valeurs qui sont récupérées en utilisant le message toast et également testé le script php en utilisant postman.
Maintenant, je veux afficher les valeurs récupérées dans ExpandableListView.
C'est le fichier list_child.xml
Récupérer les données de mysql et afficher les données dans ExpandableListView dans android

<?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="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/lblListItem" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textSize="17sp" 
     android:paddingTop="5dp" 
     android:paddingBottom="5dp" 
     android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"/> 

</LinearLayout> 

J'utilise une fonction pour récupérer les valeurs de base de données et je reçois les valeurs, mais je suis incapable d'imprimer ces valeurs dans ExpandableListView. Cette fonction prépare la vue de la liste extensible

private void ListData() { 
    listDataHeader = new ArrayList<String>(); 
    listDataChild = new HashMap<String,List<String>>(); 

    //Adding header to expandable list view 
    listDataHeader.add("ABC"); 
    listDataHeader.add("DEF"); 

    //Adding child data 
    List<String> abc = new ArrayList<>(); 
    abc.add(fetchAbc()); 
    List<String> def = new ArrayList<>(); 
    def.add(fetchDef()); 


    //Header child data 
    listDataChild.put(listDataHeader.get(0),abc); 
    listDataChild.put(listDataHeader.get(1),def); 
} 
    private String fetchAbc() { 

    return ("first name : \t"+Name+"\nEmail : \t"+email); 
} 

private String fetchDef() { 

    return ("Id : \t"+id); 
} 

Dans la vue liste extensible Je reçois la vue enfant comme premier nom, e-mail et retour Id des fonctions ci-dessus. Mais pas les valeurs. Ce fichier est classe ExpandableListAdapter

public class ExpandableListAdapter extends BaseExpandableListAdapter { 
    private Context _context; 
    private List<String> _listDataHeader; 
    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.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.header); 
     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; 
    } 
} 

Merci.

+0

double possible de [Extensible Listview avec des données dynamiques android] (http://stackoverflow.com/questions/17523257/expandable-listview-with-dynamic-data-in-android) –

+0

J'ai vérifié le lien et aussi la réponse, vous devriez remarquer que le PO n'a pas accepté la réponse. Et je ne veux pas créer une vue de liste extensible personnalisée Je veux juste réaliser ceci avec le simple –

Répondre

0
private void prepareListData() { 
    listDataHeader = new ArrayList<String>(); 
    listDataChild = new HashMap<String, List<String>>(); 
    url = ip.ip+"loadcourse.php"; 
    StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 



        try { 
         JSONArray data = new JSONArray(response); 
         for (int i = 0; i < data.length(); i++) { 


          JSONObject c = data.getJSONObject(i); 
          one = c.getString("dname").split(","); 
          two = c.getString("course_name"); 
          listDataHeader.add(String.valueOf(one[0])); 


          JSONArray dt= new JSONArray(two.toString()); 
          List<String> nowShowing = new ArrayList<String>(); 
          for (int j = 0; j < dt.length(); j++) { 


           JSONObject d = dt.getJSONObject(j); 

           three = d.getString("course").split(","); 

           nowShowing.add(String.valueOf(three[0])); 


          } 
          listDataChild.put(listDataHeader.get(i), nowShowing); 










         } 




        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 


        // response 
        Log.d("Response", response); 
       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        // error 
        Log.d("Error.Response", error.toString()); 
       } 
      } 
    ) { 
     @Override 
     protected Map<String, String> getParams() { 
      Map<String, String> params = new HashMap<String, String>(); 

      params.put("cid",ip.id); 
      return params; 
     } 
    }; 
    queue.add(postRequest);