代码之家  ›  专栏  ›  技术社区  ›  nonozor

为什么OnclickListener无法在AlertDialog中使用我的ViewList?

  •  0
  • nonozor  · 技术社区  · 11 年前

    我想在ListView的基础上制作一个带有AlertDialog的文件选择器。
    我的问题是我的onClickListener似乎什么都不做。所以,当我点击列表中的一行时,什么都不会发生。
    这是我的FilePicker类:

    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文件:
    文件列表.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();
    
    2 回复  |  直到 11 年前
        1
  •  2
  •   tralfamadore    11 年前

    而不是将适配器设置为对话框对象

    this.setAdapter(adapter, null);
    

    您可能需要将其设置为列表视图

    v.setAdapter(adapter);
    
        2
  •  0
  •   kyogs    11 年前

    改变 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);