如何从列表视图中选择特定文件

时间:2015-01-02 10:52:44

标签: java android file listview button

我制作了一个Android应用,您可以通过" ListView"将一组文件夹中的文件加载到外部存储设备中。 Java的。这是我的代码:

public class MainActivity extends Activity {

    final StringBuffer sb = new StringBuffer();

    private ListView mainListView ;  
    private ArrayAdapter<String> listAdapter ;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainListView = (ListView) findViewById( R.id.mainListView );  
        ArrayList<String> List = new ArrayList<String>();  

        File storage = Environment.getExternalStorageDirectory();
        File file = new File(storage,"/myfolder/");
        if (file.exists() && file.isDirectory()) {
            for (String s : file.list()) {
                sb.append(s + " ");
                List.addAll( Arrays.asList(s) );
            }
        }

        listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, R.id.rowTextView,List); 
        mainListView.setAdapter( listAdapter );  
    }

我添加了一个按钮&#34; Open&#34;接近&#34; simplerow.xml&#34;中的每个已加载文件:

  <Button
            android:id="@+id/button1"
            android:layout_width="75dp"
            android:layout_height="75dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:text="Open" />

        <TextView
            android:id="@+id/rowTextView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/button1"
            android:padding="10dp"
            android:textSize="16sp" >
        </TextView>

我添加了此按钮,因为我想通过单击按钮打开所选文件。 我该怎么做?如何选择特定文件?如何检索所选文件的路径?

2 个答案:

答案 0 :(得分:1)

为您的列表视图添加Click Listener,如:

mainListView .setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            File myFile = new File(List.get(position));
            FileOpen.openFile(mContext, myFile);
    }

});

public class FileOpen {

    public static void openFile(Context context, File url) throws IOException {
        // Create URI
        File file=url;
        Uri uri = Uri.fromFile(file);

        Intent intent = new Intent(Intent.ACTION_VIEW);
        // Check what kind of file you are trying to open, by comparing the url with extensions.
        // When the if condition is matched, plugin sets the correct intent (mime) type, 
        // so Android knew what application to use to open the file
        if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
            // Word document
            intent.setDataAndType(uri, "application/msword");
        } else if(url.toString().contains(".pdf")) {
            // PDF file
            intent.setDataAndType(uri, "application/pdf");
        } else if(url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
            // Powerpoint file
            intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
        } else if(url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
            // Excel file
            intent.setDataAndType(uri, "application/vnd.ms-excel");
        } else if(url.toString().contains(".zip") || url.toString().contains(".rar")) {
            // WAV audio file
            intent.setDataAndType(uri, "application/x-wav");
        } else if(url.toString().contains(".rtf")) {
            // RTF file
            intent.setDataAndType(uri, "application/rtf");
        } else if(url.toString().contains(".wav") || url.toString().contains(".mp3")) {
            // WAV audio file
            intent.setDataAndType(uri, "audio/x-wav");
        } else if(url.toString().contains(".gif")) {
            // GIF file
            intent.setDataAndType(uri, "image/gif");
        } else if(url.toString().contains(".jpg") || url.toString().contains(".jpeg") || url.toString().contains(".png")) {
            // JPG file
            intent.setDataAndType(uri, "image/jpeg");
        } else if(url.toString().contains(".txt")) {
            // Text file
            intent.setDataAndType(uri, "text/plain");
        } else if(url.toString().contains(".3gp") || url.toString().contains(".mpg") || url.toString().contains(".mpeg") || url.toString().contains(".mpe") || url.toString().contains(".mp4") || url.toString().contains(".avi")) {
            // Video files
            intent.setDataAndType(uri, "video/*");
        } else {
            //if you want you can also define the intent type for any other file

            //additionally use else clause below, to manage other unknown extensions
            //in this case, Android will show all applications installed on the device
            //so you can choose which application to use
            intent.setDataAndType(uri, "*/*");
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        context.startActivity(intent);
    }
}

您还必须将文件路径存储在某处,以便轻松传递选定的文件路径onItemClickListener

答案 1 :(得分:0)

使用ListView的setOnItemSelectedListener(AdapterView.OnItemSelectedListener listener)方法

这将注册ItemSelectedListener,然后在ItemSelectedListener中打开所选项目。