我和我有3个片段,其中包括#34; Home"," Shop"和" Collection"。在Collection片段中执行DownloadTask后,进度条和textview正在更新。但是,一旦我选择了“主页”选项卡(第一个选项卡)并选择了“收藏”选项卡(第三个选项卡),将重新创建视图而不是回收,这意味着进度和文本视图将恢复为其默认状态。
原因可能是转换视图,因为它会一直返回null值并创建新视图。但我不知道为什么它一直返回null。希望有人可以提供帮助
package com.GempakStarz.GempakMagazine
public class CollectionListAdapter extends BaseAdapter implements
OnClickListener {
private Context context;
private Activity parentActivity;
private String[] books;
private Map<String, Button> downloadbuttons = new HashMap<String, Button>();
private Map<String, Button> deletebuttons = new HashMap<String, Button>();
private Map<String, ProgressBar> progressbars = new HashMap<String, ProgressBar>();
private Map<String, TextView> progress_texts = new HashMap<String, TextView>();
public CollectionListAdapter(Context context, Activity parentActivity,
String[] books) {
this.context = context;
this.books = books;
this.parentActivity = parentActivity;
}
@Override
public int getCount() {
return books.length;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
Log.v("Check", "ConvertViewCreated!");
String bookname = books[position];
convertView = new View(context);
convertView = inflater.inflate(R.layout.collection_list_item, null);
TextView textView = (TextView) convertView.findViewById(R.id.title);
textView.setText(bookname.replace("gempak", "G"));
// set image based on selected text
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
imageView.setImageResource(R.drawable.ic_launcher);
Button download_button = (Button) convertView
.findViewById(R.id.download);
download_button.setTag(bookname);
download_button.setOnClickListener(this);
Button delete_button = (Button) convertView.findViewById(R.id.delete);
delete_button.setTag(bookname);
delete_button.setOnClickListener(this);
RelativeLayout view = (RelativeLayout) convertView
.findViewById(R.id.background);
view.setTag(bookname);
view.setOnClickListener(this);
ProgressBar progressbar = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
TextView progress_text = (TextView) convertView
.findViewById(R.id.text_progress);
downloadbuttons.put(bookname, download_button);
deletebuttons.put(bookname, delete_button);
progressbars.put(bookname, progressbar);
progress_texts.put(bookname, progress_text);
if (DownloadCompleted(bookname)) {
downloadbuttons.get(bookname).setEnabled(false);
download_button.setText("Download");
progressbars.get(bookname).setProgress(100);
progress_texts.get(bookname).setText("Download Complete");
} else {
Log.v("LOL", "Not in Storage");
}
} else {
Log.v("Check", "ConvertViewReused!!!");
}
return convertView;
}
public boolean DownloadCompleted(String bookname) {
if (Book.getInstance() != null) {
int pagenumber = Book.getInstance().GetBookLibrary().get(bookname).max_page;
int saved_files = 0;
for (int i = 0; i < pagenumber; i++) {
String foldername = bookname;
String filename = "page" + i;
String filepath = Environment.getExternalStorageDirectory()
.getPath() + "/" + foldername + "/" + filename + ".jpg";
File f = new File(Environment.getExternalStorageDirectory(),
foldername);
if (!f.exists()) {
f.mkdirs();// create new directory
}
File fpath = new File(filepath);
if (fpath.exists()) {
saved_files += 1;
}
}
if (saved_files == pagenumber) {
return true;
} else {
return false;
}
}
return false;
}
String checkInternetStatus() {
final ConnectivityManager connMgr = (ConnectivityManager) parentActivity
.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isAvailable()) {
return "Wifi";
} else if (mobile.isAvailable()) {
return "Data";
} else {
return "None";
}
}
@Override
public void onClick(View v) {
final String bookname = v.getTag().toString();
final DownloadTask task = new DownloadTask(context, v.getTag()
.toString(), progressbars.get(bookname),
downloadbuttons.get(bookname), deletebuttons.get(bookname),
progress_texts.get(bookname));
switch (v.getId()) {
case R.id.download:
if (checkInternetStatus() == "Wifi") {
if (Book.getInstance() != null) {
Toast.makeText(
context,
"Download : "
+ Book.getInstance().GetBookLibrary()
.get(bookname).name,
Toast.LENGTH_SHORT).show();
task.execute(Book.getInstance().GetDownloadLinks(bookname));
} else {
Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
}
} else if (checkInternetStatus() == "Data") {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
parentActivity);
alertDialogBuilder
.setMessage("You are using mobile data right now, continue download with mobile data? ");
alertDialogBuilder.setTitle("" + bookname);
alertDialogBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
if (Book.getInstance() != null) {
Toast.makeText(
context,
"Download : "
+ Book.getInstance()
.GetBookLibrary()
.get(bookname).name,
Toast.LENGTH_SHORT).show();
task.execute(Book.getInstance()
.GetDownloadLinks(bookname));
} else {
Toast.makeText(context, "Error",
Toast.LENGTH_SHORT).show();
}
}
});
alertDialogBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
} else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
parentActivity);
alertDialogBuilder.setMessage("No Internet Access ");
alertDialogBuilder.setTitle("System Message");
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
break;
case R.id.delete:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
parentActivity);
alertDialogBuilder.setMessage("Delete " + bookname + "?");
alertDialogBuilder.setTitle("" + bookname);
alertDialogBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(context, bookname + " Deleted",
Toast.LENGTH_SHORT).show();
task.cancel(true);
task.DeleteAll(bookname);
}
});
alertDialogBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
break;
case R.id.background:
if (DownloadCompleted(v.getTag().toString())) {
Intent read_view = new Intent(context, ReadViewPager.class);
read_view.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
read_view.putExtra("Bookname", v.getTag().toString());
read_view.putExtra("PageNumber", 153);
context.startActivity(read_view);
}
break;
}
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
答案 0 :(得分:0)
只需更新一下:
convertView = inflater.inflate(R.layout.collection_list_item, parent, false);
并删除此内容:
convertView = new View(context);
并致电:
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
在if(convertView==null){...
答案 1 :(得分:0)
你错了。你总是返回相同的convertView,第一次初始化的那个,当convertView为null时,不改变值。必须在但是一旦我选择了Home选项卡(第一个选项卡)并选择了返回 收集选项卡(第三个选项卡)重新创建视图而不是回收 这意味着进度和textview恢复到其默认状态。
if/else
后卫之外执行值的分配。 E.g。
if (convertView == null) {
// inflate convertView
}
// convertView.findViewById
// retrieve it the item at position in your dataset
// assign values to the views
答案 2 :(得分:0)
对于您的情况,您需要将getView
更改为:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
Log.v("Check", "ConvertViewCreated!");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.collection_list_item, null);
} else {
Log.v("Check", "ConvertViewReused!!!");
}
String bookname = books[position];
TextView textView = (TextView) convertView.findViewById(R.id.title);
textView.setText(bookname.replace("gempak", "G"));
// set image based on selected text
ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
imageView.setImageResource(R.drawable.ic_launcher);
Button download_button = (Button) convertView
.findViewById(R.id.download);
download_button.setTag(bookname);
download_button.setOnClickListener(this);
Button delete_button = (Button) convertView.findViewById(R.id.delete);
delete_button.setTag(bookname);
delete_button.setOnClickListener(this);
RelativeLayout view = (RelativeLayout) convertView
.findViewById(R.id.background);
view.setTag(bookname);
view.setOnClickListener(this);
ProgressBar progressbar = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
TextView progress_text = (TextView) convertView
.findViewById(R.id.text_progress);
downloadbuttons.put(bookname, download_button);
deletebuttons.put(bookname, delete_button);
progressbars.put(bookname, progressbar);
progress_texts.put(bookname, progress_text);
if (DownloadCompleted(bookname)) {
downloadbuttons.get(bookname).setEnabled(false);
download_button.setText("Download");
progressbars.get(bookname).setProgress(100);
progress_texts.get(bookname).setText("Download Complete");
} else {
Log.v("LOL", "Not in Storage");
}
return convertView;
}
然后,我不认为这些代码是最好的编码规则。我向您推荐ViewHolder
方式。