2013-04-12 3 views
0

Я хочу сделать сборщик файлов с помощью AlertDialog на основе ListView.
Моя проблема в том, что мой onClickListener ничего не делает. Поэтому, когда я нажимаю на строку в моем списке, ничего не происходит.
Вот мой FilePicker класс:Почему OnclickListener не работает с моим ViewList в AlertDialog?

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); 
    } 

} 

и вот мои два XML-файлы:
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" 
    /> 

и 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> 

В моей деятельности Мне просто нужно позвонить в свой FilePicker следующим образом:

FilePicker fp = new FilePicker(this); 
fp.show(); 
+0

Вы можете попробовать установить адаптер в listView вместо самого AlertDialog. – yahya

+0

Да, это была проблема, спасибо! – nonozor

ответ

2

вместо установки адаптера в диалоговом объекта

this.setAdapter(adapter, null); 

вы, вероятно, нужно установите его в свой вид списка

v.setAdapter(adapter); 
+0

Спасибо, что решает проблему. – nonozor

0

изменение final ListView v = (ListView) li.inflate(R.layout.file_list, null, false); построчно

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

, потому что здесь вы раздуть макет в виде списка и теперь

final ListView v = (ListView)vi.findViewById(your listviewid); 

 Смежные вопросы

  • Нет связанных вопросов^_^