2013-04-12 2 views
0

ListView를 기반으로 AlertDialog가있는 파일 선택기를 만들고 싶습니다.
내 문제는 내 onClickListener가 아무것도하지 않는 것입니다. 그래서 목록에서 한 줄을 클릭해도 아무 일도 일어나지 않습니다.
여기 내 FilePicker 클래스입니다 :왜 OnclickListener가 AlertDialog에서 내 ViewList와 함께 작동하지 않습니까?

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

AlertDialog 자체 대신 adapter를 listView로 설정할 수도 있습니다. – yahya

+0

예, 문제였습니다. 고마워요! – nonozor

답변

2

목록보기로 설정하십시오.

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

에 의해 대신 당신은 아마 필요

this.setAdapter(adapter, null); 

대화 개체에 어댑터를 설정 지금

final ListView v = (ListView)vi.findViewById(your listviewid); 
관련 문제