I have 3 fragments with me, which is "Home", "Shop", and "Collection". After I executed the DownloadTask in Collection fragment, the progress bar and textviews are updating. But once I selected the Home tab(1st tab) and select back the Collection tab(3rd tab) the views is recreated instead of recycling which means the progress and textview restore to its default state.
The cause might be the convert view because it keep returning null value and create the new view. But I do not know why it keep returning null. Hope anyone can help.
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;
}
}
But once I selected the Home tab(1st tab) and select back the Collection tab(3rd tab) the views is recreated instead of recycling which means the progress and textview restore to its default state.
you are wrong. You are returning always the same convertView, the one you initialize the first time, when convertView is null, without changing is value. The assignment of the values to your views must be performed outside the if/else
guard. E.g.
if (convertView == null) {
// inflate convertView
}
// convertView.findViewById
// retrieve it the item at position in your dataset
// assign values to the views