Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

ListView et fragment_layout

Featured Replies

Posté(e)

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.

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.