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();
AlertDialog 자체 대신 adapter를 listView로 설정할 수도 있습니다. – yahya
예, 문제였습니다. 고마워요! – nonozor