gpt4 book ai didi

java - 如何从 ListView 中选择特定文件

转载 作者:行者123 更新时间:2023-12-01 12:04:19 25 4
gpt4 key购买 nike

我制作了一个 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 );
}

我在“simplerow.xml”中每个加载的文件附近添加了一个“打开”按钮:

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

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

最佳答案

为您的 ListView 添加点击监听器,例如:

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

并且您还必须将文件路径存储在某处,以便您可以轻松地将选定的文件路径传递给ItemClickListener

关于java - 如何从 ListView 中选择特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27740977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com