gpt4 book ai didi

java - 带有 Array ListView 的异步任务

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

我在让 AsyncTask 与我的应用程序配合使用时遇到一些问题。我遵循了本教程的部分内容:http://samir-mangroliya.blogspot.in/p/android-asynctask-example.html 。但是,我的列表从未加载。

我的原始代码:

public class MainActivity extends ListActivity {  

private File file;
private List<String> myList;

String dirNameSlash = "/Videos";
String path = Environment.getExternalStorageDirectory() + dirNameSlash;
public static final String PREFS_NAME = "MyApp";

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

ListView lv = getListView();

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {

try {
String root_sd = Environment.getExternalStorageDirectory().toString();
String theFile = root_sd + "/Videos/" + (String) getListAdapter().getItem(row);
File file = new File(theFile);
FileInputStream source= new FileInputStream(file);
String targetDirectory = Environment.getExternalStorageDirectory().toString();
FileOutputStream destination = new FileOutputStream(targetDirectory + "/Saved/" + file.getName() + ".mp4");
FileChannel sourceFileChannel = source.getChannel();
FileChannel destinationFileChannel = destination.getChannel();
long size = sourceFileChannel.size();

sourceFileChannel.transferTo(0, size, destinationFileChannel);

source.close();
destination.close();

Toast.makeText(getApplicationContext(), "Video was copied successfully to '/sdcard/Saved'", Toast.LENGTH_LONG).show();

} catch (Exception ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
Toast.makeText(getApplicationContext(), "Error when copying", Toast.LENGTH_LONG).show();
}

return true;
}
});

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d("MyApp", "No SDCARD");
} else {
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"Saved");
directory.mkdirs();

}

myList = new ArrayList<String>();

String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/Videos" ) ;
File list[] = file.listFiles();

for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}

setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, myList ));


protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
intentToPlayVideo.setDataAndType(Uri.parse(path + item), "video/*");
startActivity(intentToPlayVideo);
}



}

我的 AsyncTask 代码:

public class MainActivity extends ListActivity {  

private File file;
private List<String> myList;
Load objMyTask;

String dirNameSlash = "/Videos";
String path = Environment.getExternalStorageDirectory() + dirNameSlash;
public static final String PREFS_NAME = "MyPref";

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

ListView lv = getListView();

lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int row, long arg3) {

try {
String root_sd = Environment.getExternalStorageDirectory().toString();
String theFile = root_sd + "/Videos" + (String) getListAdapter().getItem(row);
File file = new File(theFile);
FileInputStream source= new FileInputStream(file);
String targetDirectory = Environment.getExternalStorageDirectory().toString();
FileOutputStream destination = new FileOutputStream(targetDirectory + "/Saved" + file.getName() + ".mp4");
FileChannel sourceFileChannel = source.getChannel();
FileChannel destinationFileChannel = destination.getChannel();
long size = sourceFileChannel.size();

sourceFileChannel.transferTo(0, size, destinationFileChannel);

source.close();
destination.close();

Toast.makeText(getApplicationContext(), "Video was copied successfully to '/sdcard/Saved'", Toast.LENGTH_LONG).show();

} catch (Exception ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
Toast.makeText(getApplicationContext(), "Error when copying", Toast.LENGTH_LONG).show();
}

return true;
}
});

if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d("MyApp", "No SDCARD");
} else {
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"Saved");
directory.mkdirs();


}


setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, myList ));


objMyTask = new Load();

objMyTask.execute();

}

protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String item = (String) getListAdapter().getItem(position);
Intent intentToPlayVideo = new Intent(Intent.ACTION_VIEW);
intentToPlayVideo.setDataAndType(Uri.parse(path + item), "video/*");
startActivity(intentToPlayVideo);
}



}


private class Load extends AsyncTask<String, Integer, String> {

@Override
protected String doInBackground(String... params) {
myList = new ArrayList<String>();

String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( root_sd + "/Videos" ) ;
File list[] = file.listFiles();

for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}

return null;
}

}

任何帮助将不胜感激。谢谢!

最佳答案

从 asynctask 获取列表后,不要在适配器上调用 notifyDatasetChanged

您需要使用onPostExecute()方法并在那里调用它

不要这样做

setListAdapter(new ArrayAdapter<String>(this,
R.layout.row, myList ));

取出适配器并保留它的一个实例,以便稍后在获得列表时使用

ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.row, myList )
setListAdapter(adapter);

然后只需执行adapter.notifyDatasetChanged()

关于java - 带有 Array ListView 的异步任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18857469/

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