ListFragment toujours vide
Salut, je souhaite réaliser une liste personnalisée dans une ListFragment mais ma liste est toujours vide.
J'ai lu et appliqué plus tuto mais en vain, j'ai vraiment besoin d'aide.
Voici le code de mon fragment CVList.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package tdl.project.jobmanager.fragment;
import java.io.File;
import java.util.ArrayList;
import tdl.project.jobmanager.R;
import tdl.project.jobmanager.adapter.ListAdapter;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class CVList extends ListFragment {
private ListView mList = null;
private ListAdapter mAdapter = null;
private File mCurrentFile = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, container, false);
}
public void onActivtyCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Toast.makeText(getActivity(), "List ready", Toast.LENGTH_LONG).show();
mList = (ListView) getListView();
if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Toast.makeText(getActivity(), "Carte mémoire absente", Toast.LENGTH_LONG).show();
return;
} else {
mCurrentFile = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Job Manager/pdf/");
}
File[] fichiers = mCurrentFile.listFiles();
ArrayList<File> list = new ArrayList<File>();
for(File f : fichiers)
list.add(f);
mAdapter = new ListAdapter(getActivity(), android.R.layout.simple_list_item_1, list);
mList.setAdapter(mAdapter);
registerForContextMenu(mList);
mList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
File fichier = mAdapter.getItem(position);
seeItem(fichier);
}
private void seeItem(File mFile) {
String cv = mFile.getName() + ".pdf";
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Job Manager/pdf/" + cv);
if (file.exists()){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "No Application Available to View PDF", Toast.LENGTH_SHORT).show();
}
}
else
Toast.makeText(getActivity(), "No CV found", Toast.LENGTH_SHORT).show();
}
});
}
} |
Le layout du fragment list.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/text_empty" />
</RelativeLayout> |
Mon adapter ListAdapter.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| package tdl.project.jobmanager.adapter;
import java.io.File;
import java.util.Comparator;
import java.util.List;
import tdl.project.jobmanager.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ListAdapter extends ArrayAdapter<File> {
private LayoutInflater mInflater = null;
public ListAdapter(Context context, int textViewResourceId, List<File> objects) {
super(context, textViewResourceId, objects);
mInflater = LayoutInflater.from(context);
}
private class FileComparator implements Comparator<File> {
public int compare(File lhs, File rhs) {
// si lhs est un répertoire et pas l'autre, il est plus petit
if(lhs.isDirectory() && rhs.isFile())
return -1;
// dans le cas inverse, il est plus grand
if(lhs.isFile() && rhs.isDirectory())
return 1;
//Enfin on ordonne en fonction de l'ordre alphabétique sans tenir compte de la casse
return lhs.getName().compareToIgnoreCase(rhs.getName());
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layoutItem;
if(convertView != null)
layoutItem = (LinearLayout) convertView;
else
layoutItem = (LinearLayout) mInflater.inflate(R.layout.cv_item, parent, false);
File item = getItem(position);
ImageView logo = (ImageView) layoutItem.findViewById(R.id.cvLogo);
TextView name = (TextView) layoutItem.findViewById(R.id.cvName);
logo.setImageResource(R.drawable.file_icon);
name.setText(item.getName());
return layoutItem;
}
public void sort () {
super.sort(new FileComparator());
}
} |
Le layout de personnalisation cv_item.xml
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?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="horizontal"
android:paddingTop="@dimen/space5"
android:paddingBottom="@dimen/space5" >
<ImageView
android:id="@+id/cvLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name" />
<TextView
android:id="@+id/cvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/space5"
style="@style/choosePhotoText" />
</LinearLayout> |
Merci d'avance pour vos réponses, cordialement.