我正在尝试使用ArrayList填充listview,而ArrayList必须根据此处https://stackoverflow.com/a/22326209/8520030的内容从Asynctask返回。我试图将zip文件显示为listview。我使用asynctask通常需要花费太多时间在listview中加载项目。 但是使用Asynctask时listview中没有显示任何内容。就像它没有填充listview项目。我这样做到现在为止。
public interface IAsyncTask {
ArrayList<String> IAmFinished(ArrayList<File> arrayList);
}
public class MainActivity2 extends AppCompatActivity implements IAsyncTask {
public static ArrayList<File> fileList = new ArrayList<>();
public static ArrayList<String> name = new ArrayList<>();
Longoperation longoperation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
File dir1 = new File(Environment.getExternalStorageDirectory().getPath());
longoperation = new Longoperation(dir1, this);
longoperation.execute();
JazzyListView lv = (JazzyListView) findViewById(R.id.list);
lv.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, name));
}
@Override
public ArrayList<String> IAmFinished(ArrayList<File> list) {
for(int i=0;i< list.size();i++){
name.add(list.get(i).getName());
}
return name;
}
private class Long operation extends AsyncTask<File, Void, ArrayList<File>>{
File dir;
private static final String TAG = "com.example.dell_1.myapp3.Zip";
private IAsyncTask asyncTaskListener;
public Longoperation(File dir,IAsyncTask asyncTaskListener) {
this.dir = dir;
this.asyncTaskListener = asyncTaskListener;
}
@Override
protected ArrayList<File> doInBackground(File... params) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
doInBackground(listFile[i]);
} else {
boolean booleanpdf = false;
if (listFile[i].getName().endsWith(".zip") || listFile[i].getName().endsWith(".jar")) {
for (int j = 0; j < fileList.size(); j++) {
if (fileList.get(j).getName().equals(listFile[i].getName())) {
booleanpdf = true;
} else {
}
}
if (booleanpdf) {
booleanpdf = false;
} else {
fileList.add(listFile[i]);
}
}
}
}
}
return fileList;
}
public void onPostExecute(ArrayList<File> list) {
asyncTaskListener.IAmFinished(list);
}
}
}
但我无法填充列表视图。 列表视图中没有显示任何内容。
我怎样才能解决这个问题?我究竟做错了什么?
答案 0 :(得分:0)
从onCreate中删除以下行。
IAmFinished(fileList);
从onCreate剪切以下行并在for循环后粘贴IamFinished。
lv.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, name));
并且不要忘记声明JazzyListView全球。
修改代码的结尾。
public interface IAsyncTask {
ArrayList<String> IAmFinished(ArrayList<File> arrayList);
}
public class MainActivity2 extends AppCompatActivity implements IAsyncTask {
public static ArrayList<File> fileList = new ArrayList<>();
public static ArrayList<String> name = new ArrayList<>();
Longoperation longoperation;
JazzyListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
File dir1 = new File(Environment.getExternalStorageDirectory().getPath());
longoperation = new Longoperation(dir1, this);
longoperation.execute();
lv = (JazzyListView) findViewById(R.id.list);
}
@Override
public ArrayList<String> IAmFinished(ArrayList<File> list) {
for(int i=0;i< list.size();i++){
name.add(list.get(i).getName());
}
lv.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, name));
return name;
}
private class Longoperation extends AsyncTask<File, Void, ArrayList<File>>{
File dir;
private static final String TAG = "com.example.dell_1.myapp3.Zip";
private IAsyncTask asyncTaskListener;
public Longoperation(File dir,IAsyncTask asyncTaskListener) {
this.dir = dir;
this.asyncTaskListener = asyncTaskListener;
}
@Override
protected ArrayList<File> doInBackground(File... params) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
doInBackground(listFile[i]);
} else {
boolean booleanpdf = false;
if (listFile[i].getName().endsWith(".zip") || listFile[i].getName().endsWith(".jar")) {
for (int j = 0; j < fileList.size(); j++) {
if (fileList.get(j).getName().equals(listFile[i].getName())) {
booleanpdf = true;
} else {
}
}
if (booleanpdf) {
booleanpdf = false;
} else {
fileList.add(listFile[i]);
}
}
}
}
}
return fileList;
}
public void onPostExecute(ArrayList<File> list) {
asyncTaskListener.IAmFinished(list);
}
}
}
答案 1 :(得分:0)
您需要通过调用notifyDataSetChanged()来刷新适配器。像这样:
lv = (JazzyListView) findViewById(R.id.list);
..
lv.getAdapter().notifyDataSetChanged();
或者您可以使用以下内容:
// need to make the class scope variable.
ArrayAdapter mAdapter;
// create the adapter
mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, name)
lv.setAdapter(adapter);
...
// refresh the adapter in your listener.
mAdapter.notifyDataSetChanged()