Jump to content

ListView et fragment_layout


Recommended Posts

Bonjour All,

 

Je fais un projet pour fixer mes connaissances en android mais voila j'ai un soucis, j'ai fait un NavigationDrawer et lorsque je clik sur un item je voudrais afficher une ListView sur le fragment layout correspondant au lieu d'appeler une autre activité et y afficher mon listview. Le problème est que ça n'affiche rien, y'a pas d'erreur dans le log mais juste la liste de mes données. Je sais pas où se trouve le problème donc j'ai besoin d'aide pour avancer.

Les données de ma ListView viennent d'une BD mysql.

 

Voilà mon code:

public class FragmentTel extends Fragment {
    InputStream is = null;
    String result = null;
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    List<String> maListDonnee = new ArrayList<String>();
    ImageView ivIcon;
    TextView tvItemName;
    ListView lv;
    public static final String IMAGE_RESOURCE_ID = "iconResourceID";
    public static final String ITEM_NAME = "itemName";

    public FragmentTel() {
    }

    @[member=override]
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_layout_one, container, false);
        ivIcon = (ImageView) view.findViewById(R.id.frag1_icon);
        tvItemName = (TextView) view.findViewById(R.id.frag1_text);
        lv = (ListView) view.findViewById(R.id.list1);
        tvItemName.setText(getArguments().getString(ITEM_NAME));
        ivIcon.setImageDrawable(view.getResources().getDrawable(getArguments().getInt(IMAGE_RESOURCE_ID)));
        SecondTache task = new SecondTache();
        task.execute();
        lv.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1 ,maListDonnee));
        return view;
    }

    class SecondTache extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
                try {
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://10.0.2.2:82/AndroidWork/affichage_telephone.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
                } catch (Exception e) {
                    Log.e("log_tag", "Erreur de connexion http " + e.toString());
                }
                //Conversion de la réponse en chaine
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();
                } catch (Exception e) {
                    Log.e("log_tag", "Erreur de conversion du resultat " + e.toString());
                }
                // Parsing des données JSON
                try {
                    JSONArray tableJson = new JSONArray(result);
                    for (int i = 0; i < tableJson.length(); i++) {
                        JSONObject json_data = tableJson.getJSONObject(i);
                        //ici on met les attributs q'on veut recupéré (table telephone Designation)
                        Log.i("log_tag", "Designation: " + json_data.getString("Designation"));
                        maListDonnee.add(json_data.getString("Designation"));
                    }
                } catch (JSONException e) {
                    Log.e("log_tag", "Erreur parsing de donnees " + e.toString());
                }
                return result;
            }
        protected void onPostExecute(String result) {
            System.out.println(result);
        }
    }
}

Appel FragmentTel dans ActivityMain.class:

public void SelectItem(int possition) {
		Fragment fragment = null;
		Bundle args = new Bundle();
		switch (possition) {
	      case 2:
	            fragment = new FragmentTel();
	            args.putString(FragmentTel.ITEM_NAME, dataList.get(possition).getItemName());
	            args.putInt(FragmentTel.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
	            break;
...
 }
}

Fragment_Layout_Tel :

<?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="horizontal"
    android:weightSum="1">
  
    <ImageView
        android:id="@+id/frag1_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/frag1_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#ffff"/>

</LinearLayout>

Merci d'avance.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...