Вопрос

I want to make a file picker with a AlertDialog based on a ListView.
My problem is that my onClickListener seems to do nothing. So when I click on a line in my list nothing happens.
Here is my FilePicker class :

public class FilePicker extends AlertDialog.Builder {

    public FilePicker(final Context context) {
        super(context);
        LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final ListView v = (ListView) li.inflate(R.layout.file_list, null, false);
        File[] dirList = (new File (Environment.getExternalStorageDirectory().getAbsolutePath() + "/myDir")).listFiles();

        ArrayList<HashMap<String, String>> mylistData =
                new ArrayList<HashMap<String, String>>();
        String[] columnTags = new String[] {"col1", "col2"};

        for (File file: dirList){
            HashMap<String,String> map = new HashMap<String, String>();
            map.put(columnTags[0], file.getName());
            map.put(columnTags[1], DateFormat.format("yyyy-MM-dd",new Date(file.lastModified())).toString());
            mylistData.add(map);
        }


        int[] columnIds = new int[] {R.id.filelistitemview,R.id.datelistitemview};
        SimpleAdapter adapter = new SimpleAdapter(context, mylistData,R.layout.file_list_item,columnTags , columnIds);
        this.setAdapter(adapter, null);
        this.setTitle("Choose midi settings file");
        v.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) {
                String selectedFromList =(String) (v.getItemAtPosition(myItemInt));
                Toast.makeText(context, selectedFromList, Toast.LENGTH_SHORT).show();
              }                 
        });
        this.setView(v);
    }

}

and here is my two xml files :
file_list.xml

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    />

and file_list_item.xml :

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:paddingBottom="6dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">
<TextView 
    android:id="@+id/filelistitemview"
    android:layout_width="120dip"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:textColor="#000"
    android:textSize="23dp" 
    android:layout_weight="1"/>

<TextView 
    android:id="@+id/datelistitemview"
    android:layout_width="60dip"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:textColor="#000"
    android:textSize="18dp" 
    android:layout_weight="1"/>

 </LinearLayout>

In my activity I just have to call my FilePicker like this :

    FilePicker fp = new FilePicker(this);
fp.show();
Это было полезно?

Решение

instead of setting adapter to dialog object

this.setAdapter(adapter, null);

you probably need set it to your list view

v.setAdapter(adapter);

Другие советы

change final ListView v = (ListView) li.inflate(R.layout.file_list, null, false); line by

`View vi = inflater.inflate(R.layout.file_list, null`);

because here you inflate layout in list view and now

 final ListView v = (ListView)vi.findViewById(your listviewid);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top