Images dissapearing while scrolling Listiew (Android Studio) - java

guys.
I'm totally new to programming and Android programming and I have a problem. I fetch images from MySQL database as InputStream. Then I have Adapter where I load data, that I got from database, to corresponding objects. Hooray, listView is created and everything looks good! But... When I scroll down/up my images dissapear. I've read that listView recycles objects, but the problem is that I don't know/understand how can I fix this problem :///
Please help me guys !!
SHORT: images dissapear in listView on scroll. Too much newbie to understand how to fix it :/
Event class
public class Event {
private int id;
private String title;
private Date date;
private String place;
private String genre;
private String description;
private Double price;
public Event(int id, String title, Date date, String place, String genre, String description, Double price, InputStream image) {
this.id = id;
this.title = title;
this.date = date;
this.place = place;
this.genre = genre;
this.description = description;
this.price = price;
this.image = image;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public InputStream getImage() {
return image;
}
public void setImage(InputStream image) {
this.image = image;
}
private InputStream image;
}
Adapter Class
public class EventAdapter extends BaseAdapter {
LayoutInflater mInflater;
ArrayList<Event> data;
public EventAdapter(Context c, ArrayList<Event> d){
mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.data = d;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
// This is where data is assigned to coresponding fields in event_layout
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Hooks
View view = mInflater.inflate(R.layout.event_layout, null);
TextView eventName = (TextView) view.findViewById(R.id.eventName);
TextView dateYear = (TextView) view.findViewById(R.id.dateYear);
TextView dateDay = (TextView) view.findViewById(R.id.dateDay);
ImageView image = (ImageView) view.findViewById(R.id.eventImage);
// Changing date format
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
String date = sdf.format(data.get(position).getDate());
// Extracting date into seprate vars
String year = date.substring(8);
String day = date.substring(0, 1).toUpperCase() + date.substring(1, 3) + "." + date.substring(4, 7) + "d.";
// making Bitmap from InputStream
Bitmap bmp = BitmapFactory.decodeStream(data.get(position).getImage());
eventName.setText(data.get(position).getTitle());
dateYear.setText(year);
dateDay.setText(day);
image.setImageBitmap(bmp);
return view;
}
}
EventsPage class
public class EventsPage extends AppCompatActivity {
private EventAdapter eventAdapter;
private Context thisContext;
private ListView eventsListView;
private TextView progressBar;
private ArrayList<Event> events = new ArrayList<Event>();
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(Bundle savedInstanceState) {
// Set status bar color to main_green
if(Build.VERSION.SDK_INT >= 21){
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
window.setStatusBarColor(this.getResources().getColor(R.color.main_green_color , this.getTheme()));
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events_page);
Resources res = getResources();
eventsListView = (ListView) findViewById(R.id.eventsListView);
progressBar = (TextView) findViewById(R.id.progressBar);
thisContext = this;
progressBar.setText("");
GetData data = new GetData();
data.execute("");
}
public class GetData extends AsyncTask<String, String, String> {
private String msg;
private ArrayList<Event> bkc_events = new ArrayList<>();
private ArrayList<Event> tic_events = new ArrayList<>();
#Override
protected void onPreExecute(){
progressBar.setText("Connecting to database...");
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
protected String doInBackground(String... strings) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName(DBstrings.JDBC_DRIVER);
conn = DriverManager.getConnection(DBstrings.URL, DBstrings.USERNAME, DBstrings.PASSWORD);
// Get BKC events
stmt = conn.createStatement();
String query = "SELECT * from `bkc_event` ORDER by `date` ASC";
ResultSet res = stmt.executeQuery(query);
while(res.next()){
Event temp = new Event(res.getInt("ID"), res.getString("title"),
res.getDate("date"), res.getString("place"),
res.getString("genre"), res.getString("description"),
res.getDouble("price"), res.getBinaryStream("image"));
this.bkc_events.add(temp);
}
res.close();
// Get TIC events
query = "SELECT * from `tic_event` ORDER by `date` ASC";
res = stmt.executeQuery(query);
while(res.next()){
Event temp = new Event(res.getInt("ID"), res.getString("title"),
res.getDate("date"), res.getString("place"),
res.getString("genre"), res.getString("description"),
res.getDouble("price"), res.getBinaryStream("image"));
tic_events.add(temp);
}
res.close();
stmt.close();
conn.close();
msg = "Process Completed";
PutDataTogether(bkc_events, tic_events);
} catch (ClassNotFoundException ex) {
msg = "Class not found ERROR";
Log.e("ClassNotFound", ex.getMessage());
} catch (SQLException ex) {
msg = "SQL exceltion";
Log.e("SQL:", ex.getMessage());
} finally {
try {
if(stmt != null)
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
// Combine data 2 sets into 1 set
#RequiresApi(api = Build.VERSION_CODES.N)
private void PutDataTogether(ArrayList<Event> bkc, ArrayList<Event> tic){
events.addAll(bkc);
for (Event t: tic) {
// counter for showing redundancy
int counter = 0;
for(Event r: events){
// Check if title and date is the same if so - increase counter by 1
if(t.getTitle().toLowerCase().equals(r.getTitle().toLowerCase()) &&
t.getDate().equals(r.getDate()))
counter++;
}
// if counter == 0, then we can add event to final result set
if(counter == 0)
events.add(t);
}
events.sort(Comparator.comparing(Event::getDate));
}
// To show the data
#Override
protected void onPostExecute(String msg){
progressBar.setText(this.msg);
/*new Handler().postDelayed(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.INVISIBLE);
}
}, 1500); */
// We have stuff to display
if(events.size() > 0){
eventAdapter = new EventAdapter(thisContext, events);
eventsListView.setAdapter(eventAdapter);
}
}
} // END of GetDataEvents class
} // END of EventsPage
The problem:
Picture before scrolling
Picture after scrolling down and then back up

The issue is within the getView() method of your adapter. Recycling in a ListView means, that there is already an existing View which will be used to display your values, so you must not inflate a new one. Change your code like:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Hooks
View view = convertView;
if (view == null) {
view = mInflater.inflate(R.layout.event_layout, null);
}
TextView eventName = (TextView) view.findViewById(R.id.eventName);
TextView dateYear = (TextView) view.findViewById(R.id.dateYear);
TextView dateDay = (TextView) view.findViewById(R.id.dateDay);
ImageView image = (ImageView) view.findViewById(R.id.eventImage);
// Changing date format
SimpleDateFormat sdf = new SimpleDateFormat("MMM dd yyyy");
String date = sdf.format(data.get(position).getDate());
// Extracting date into seprate vars
String year = date.substring(8);
String day = date.substring(0, 1).toUpperCase() + date.substring(1, 3) + "." + date.substring(4, 7) + "d.";
// making Bitmap from InputStream
Bitmap bmp = BitmapFactory.decodeStream(data.get(position).getImage());
eventName.setText(data.get(position).getTitle());
dateYear.setText(year);
dateDay.setText(day);
image.setImageBitmap(bmp);
return view;
}
Note I only changed the first 4 lines of the method to use the provided View which comes from the ListView and only inflate a new one when there is no existing to reuse.

Related

I have problem in transferring data from activity to another activity

public class Memo extends AppCompatActivity {
DBHandler dbh;
Notes items;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memo);
getSupportActionBar().hide();
try{
dbh = new DBHandler(this);
}
catch (Exception ex){
ex.printStackTrace();
}
display();
}
public void display( ){ //method to display all items in the database
List<Notes> books_list = dbh.getNotes(); ////here i get the list fromm the database
///// i used a custom adapter because i needed it
final myAdapter adapter = new myAdapter(this, books_list); ///creating adapter from
myadapter to link it with the list
ListView _note_ = findViewById(R.id.list_txt);
_note_.setAdapter(adapter);
_note_.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
items = (Notes) adapter.getItem(position);
String Note_content = items.getNote();
String title = items.getTitle();
String Date = items.getDate();
Intent i = new Intent(getApplicationContext() ,Note_content.class);
i.putExtra("Note ", Note_content);
i.putExtra("title", title);
i.putExtra("Date", Date);
startActivity(i);
}
});
i have a problem when I try to to get data( only Note) from this activity to another activity, other values(Date and Title) are working, I tried to use toString method in Notes class but it gives null for note
public class Note_content extends AppCompatActivity {
Notes item;
TextView txt_note ,txt_date , txt_title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_content);
txt_title = findViewById(R.id.txt_ntitle);
txt_note = findViewById(R.id.txt_Nnote);
txt_date = findViewById(R.id.txt_Ndate);
Intent i = getIntent();
String note = i.getStringExtra("Note");
String title = i.getStringExtra("title");
String date = i.getStringExtra("Date");
item = new Notes(title,note,date);
txt_note.setText(item.getNote());
//txt_note.setText(note)
txt_date.setText(date);
txt_title.setText(title);
}
this is the second activit, it is not giving any errors but null instead of the note
and this Notes class
public class Notes {
private int id;
private String title;
private String note ;
private String date;
public Notes() {
}
public Notes(String title, String note, String date) {
this.title = title;
this.note = note;
this.date = date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
#Override
public String toString() {
return this.note+"\n"+title+"\n"+date;
}
}
i.putExtra("Note ", Note_content);
In the above code line, "Note " has an extra space at the end.

TodoList application android

Please I need your help .I'am trying to take the date from an activity and then put it in an array list then print it in an ListView .
The problem is the data that I should take it from "Adding Todo" is not showing in the ListView . it do take me to the List Activity but without showing the data .
This is How the app going to be "Adding todo"
And this is the ListView where the data should be in it
My code :-
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
arrayList = new ArrayList<>();
todoAdapter = new TodoAdapter(this , arrayList);
listView.setAdapter(todoAdapter);
}
public void onClick(View view) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, AddTodo.class);
startActivityForResult(intent, Intent_Constants.INTENT_REQUEST_CODE);
}
protected void onActivityResult(int reqCode ,int resultCode , Intent data ){
// here Iam trying to get the data from ADDING TO DO class
if(resultCode == Intent_Constants.INTENT_REQUEST_CODE){
titleText = data.getStringExtra(Intent_Constants.INTENT_TITLE);
priorityText = data.getStringExtra(Intent_Constants.INTENT_PRIORITY);
statusText = data.getStringExtra(Intent_Constants.INTENT_STATUES);
dateText = data.getStringExtra(Intent_Constants.INTENT_DATE);
timeText = data.getStringExtra(Intent_Constants.INTENT_TIME);
todoAdapter.todo.add(new Todo (titleText , statusText ,priorityText ,dateText ,timeText));
}
}
Intent_Constants
public class Intent_Constants {
public final static int INTENT_REQUEST_CODE = 1;
public final static int INTENT_RESULT_CODE = 1 ;
public final static String INTENT_TITLE = "Title";
public final static String INTENT_PRIORITY = "Priority";
public final static String INTENT_TIME = "Time";
public final static String INTENT_DATE = "Date";
public final static String INTENT_STATUES = "Statues";
}
AddTodo class
In this class I find the id then I converted to String
public void saveButton (View view){
Intent intent = new Intent();
intent.putExtra(Intent_Constants.INTENT_TITLE , title);
intent.putExtra(Intent_Constants.INTENT_DATE , date);
intent.putExtra(Intent_Constants.INTENT_TIME , time);
intent.putExtra(Intent_Constants.INTENT_PRIORITY , priority);
intent.putExtra(Intent_Constants.INTENT_STATUES , completed);
setResult(INTENT_RESULT_CODE, intent);
finish();
}
TodoAdapter class
ArrayList<Todo> todo ;
public TodoAdapter(#NonNull Context context, ArrayList<Todo> todo) {
super(context, R.layout.todo_list,todo);
this.todo = todo;
}
public static class ViewHolder {
TextView titleText;
TextView priorityText;
TextView dateText;
TextView timeText;
CheckBox statusBox;
ImageButton edit_image;
ImageButton open_image;
ImageButton delet_image;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = LayoutInflater.from(getContext()) ;
View customeView =inflater.inflate(R.layout.todo_list,parent ,false);
holder.titleText.setText(getItem(position).getTitle());
holder.priorityText.setText(getItem(position).getPriority());
holder.dateText.setText(getItem(position).getDate());
holder.timeText.setText(getItem(position).getTime());
holder.statusBox.setChecked(false);
holder.edit_image = customeView.findViewById(R.id.edit_imageView);
holder.open_image = customeView.findViewById(R.id.open_imageButton);
holder.delet_image = customeView.findViewById(R.id.delete_imageView);
holder.edit_image.setImageResource(R.drawable.ic_edit_black_24dp);
holder.open_image.setImageResource(R.drawable.ic_refresh_black_24dp);
holder.delet_image.setImageResource(R.drawable.ic_delete_black_24dp);
return customeView;
}
Todo class
public class Todo {
private String title ;
private String status ;
private String priority ;
private String date ;
private String time ;
public Todo() {
}
public Todo(String title, String status, String priority, String date, String time) {
this.title = title;
this.status = status;
this.priority = priority;
this.date = date;
this.time = time;
}
public String getTitle() {
return title;
}
public String getStatus() {
return status;
}
public String getPriority() {
return priority;
}
public String getDate() {
return date;
}
public String getTime() {
return time;
}
public void setTitle(String title) {
this.title = title;
}
public void setStatus(String status) {
this.status = status;
}
public void setPriority(String priority) {
this.priority = priority;
}
public void setDate(String date) {
this.date = date;
}
public void setTime(String time) {
this.time = time;
}
}
You can just set a new adapter, I know it isn't best way, but should do the work. Don't create new one, but nulify yours, and initialize it with new data.
#Noura change this
arrayList.add(new Todo(titleText , statusText ,priorityText ,dateText ,timeText));
to
todoAdapter.todo.add(new Todo(titleText , statusText ,priorityText ,dateText ,timeText));
todoAdapter.notifyDataSetChanged();
and at the constructor u need to update your code like below
ArrayList<Todo> todo ;
public TodoAdapter(#NonNull Context context, ArrayList<Todo> todo) {
this.todo = todo
super(context, R.layout.todo_list,todo);
}

Using getShortClassName() method in Android

I'm trying to modify an application to use the getShortClassName() method instead of displaying the full class name (e.g. "com.google.app.MainActivity" to simply ".MainActivity"). The current setup gives me no errors but doesn't display the text.
I presume the solution is not complicated, although I just don't have the experience to make it work. Help?
#Override
public View getChildView (int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
MyActivityInfo activity = (MyActivityInfo)getChild(groupPosition, childPosition);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.all_activities_child_item, null);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
text1.setText(activity.getName());
//TextView text2 = (TextView) view.findViewById(android.R.id.text2);
//text2.setText(activity.getComponentName().getClassName());
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text2.setText(activity.getShortClassName());
ImageView icon = (ImageView) view.findViewById(android.R.id.icon);
icon.setImageDrawable(activity.getIcon());
return view;
}
public class MyActivityInfo implements Comparable<MyActivityInfo> {
public MyActivityInfo(ComponentName activity, PackageManager pm) {
this.component_name = activity;
ActivityInfo act;
try {
act = pm.getActivityInfo(activity, 0);
this.name = act.loadLabel(pm).toString();
try {
this.icon = (BitmapDrawable)act.loadIcon(pm);
}
catch(ClassCastException e) {
this.icon = (BitmapDrawable)pm.getDefaultActivityIcon();
}
this.icon_resource = act.getIconResource();
} catch (NameNotFoundException e) {
this.name = activity.getShortClassName();
this.icon = (BitmapDrawable)pm.getDefaultActivityIcon();
this.icon_resource = 0;
}
this.icon_resource_name = null;
if(this.icon_resource != 0) {
try {
this.icon_resource_name = pm.getResourcesForActivity(activity).getResourceName(this.icon_resource);
} catch (Exception e) {}
}
}
public ComponentName getComponentName() {
return component_name;
}
public BitmapDrawable getIcon() {
return icon;
}
public String getName() {
return name;
}
public String getIconResourceName() {
return icon_resource_name;
}
public String getShortClassName() {
return class_name;
}
protected ComponentName component_name;
protected BitmapDrawable icon;
protected int icon_resource;
protected String icon_resource_name;
protected String name;
protected String class_name;
#Override
public int compareTo(MyActivityInfo another) {
int cmp_name = this.name.compareTo(another.name);
if (cmp_name != 0) return cmp_name;
int cmp_component = this.component_name.compareTo(another.component_name);
return cmp_component;
}
#Override
public boolean equals(Object other) {
if(!other.getClass().equals(MyPackageInfo.class)) {
return false;
}
MyActivityInfo other_info = (MyActivityInfo)other;
return this.component_name.equals(other_info.component_name);
}
}
You could just use getSimpleName() method of class object:
String a = "Hello";
System.out.println(a.getClass().getSimpleName());
System.out.println(a.getClass().getName());
Output:
String
java.lang.String

Can't set the data that is coming out as the response from the server to the CustomListView Adapter

This is the Java code where I am parsing the data-
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...Please Wait...");
pDialog.show();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://sikkimexpress.itstunner.com/api/homenewslist/topnews", new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("HomeNews");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject homenews = jsonArray.getJSONObject(i);
Movie movie = new Movie();
String newsId = homenews.getString("NewsId");
String dateTime = homenews.getString("DateTime");
String newsType = homenews.getString("NewsType");
String title = homenews.getString("Title");
String description = homenews.getString("Description");
String mainImageURL = homenews.getString("MainImageThumbnail");
movieList.add(movie);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
System.out.println("Result:- " + newsId + " " + dateTime + " " + newsType + " " + title + " " + description + " " + mainImageURL);
}
} catch (JSONException e) {
e.printStackTrace();
}
// pDialog.hide();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.getMessage());
// pDialog.hide();
}
});
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
This is the Model Class:-
public class Movie {
private String newsId;
private String dateTime;
private String newsType;
private String title;
private String description;
private String thumbnailUrl;
public Movie() {
}
public Movie(String news_id, String date_time, String news_type, String news_title, String news_description, String news_thumbnailUrl) {
this.title = news_title;
this.thumbnailUrl = news_thumbnailUrl;
this.newsId = news_id;
this.dateTime = date_time;
this.newsType = news_type;
this.description = news_description;
}
public String getNewsId() {
return newsId;
}
public void setNewsId(String newsId) {
this.newsId = newsId;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime = dateTime;
}
public String getNewsType() {
return newsType;
}
public void setNewsType(String newsType) {
this.newsType = newsType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
}
The CustomListView Adapter:-
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
#Override
public int getCount() {
return movieItems.size();
}
#Override
public Object getItem(int location) {
return movieItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView desciption = (TextView) convertView.findViewById(R.id.desciption);
Movie m = movieItems.get(position);
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
title.setText(m.getTitle());
desciption.setText(m.getDescription());
return convertView;
}
}
There is no error while Parsing the data from server. I am getting the actual result. But the Progress Dialog is running after getting the data from the server. The data are not getting set in the CustomListView Adapter. I have already attached the code. Please Help me. I got stuck in it.
You are not closing your Dialog when you have your data.
You should not load data on the "main thread" - use a AsyncTask or something similiar to load your data. There you can show a progress dialog, before you start downloading your data:
From the docs:
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
//Once created, a task is executed very simply:
new DownloadFilesTask().execute(url1, url2, url3);
Also, don't set the adapter multiple times to your ListView (unless you use a different adapter), and call notifyDataSetChanged() everytime your underlying data changes. r data, show progress and stop the dialog when you are finished.
But the Progress Dialog is running after getting the data from the server.?
ans: you are not closing the dialog in onResponse method
For listview you are not setting adapter with updated data. Please create a new adapter or follow this How to update listview when back pressed from another activity android?
You need to dismiss progressdialog in this two response method to hide.
#Override public void onResponse(JSONObject response) { pDialog.dismiss(); }
#Override public void onErrorResponse(VolleyError error) { pDialog.dismiss(); }
In onResponse() method after parsing json you need to notify adapter to display data in list.

Loading images in listview asynchronously with callback

I'm using Parse in my app, and in order to load my 'profile' images, I need to retrieve a so called Parsefile. When the Parsefile is downloaded it uses a callback to notify when it's done. Now this is generally a nice way to do things but I encountered a problem with this when using a Listview and downloading the images with an Asynctask.
The problem is as follows:
In my ListView adapter in the getView method, I create an AsyncTask and execute it, this AsyncTask starts the retrieveProfileImage(callBack) function. In my callback I simply start a Runnable on the UI thread to update the ImageView in the View with the new (retrieved Image). The problem however as it seems, is the fact that as soon as I start my AsyncTask, the View is returned. So I can't set the other images to the correct row. I hope my code demonstrates my problem more clearly.
The ListAdapter:
public class FriendListAdapter extends ArrayAdapter<Profile> {
private int resource;
private Context context;
private List<Profile> friends;
private Profile fProfile;
private Bitmap profileImageBitmap;
private ProgressBar friendImageProgressBar;
//ui
private ImageView friendImage;
public FriendListAdapter(Context context, int resource,
List<Profile> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.friends = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView friendName = null;
friendImage = null;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.friendslist_row, null);
friendName = (TextView) rowView.findViewById(R.id.fName);
friendImage = (ImageView) rowView
.findViewById(R.id.fImage);
friendImageProgressBar = (ProgressBar) rowView.findViewById(R.id.friendImageProgressBar);
} else {
friendName = (TextView) convertView.findViewById(R.id.fName);
friendImage = (ImageView) convertView.findViewById(R.id.fImage);
friendImageProgressBar = (ProgressBar) convertView.findViewById(R.id.friendImageProgressBar);
}
fProfile = friends.get(position);
DownloadProfileImage dImg = new DownloadProfileImage();
dImg.execute();
friendName.setText(fProfile.getName());
return rowView;
}
private class DownloadProfileImage extends AsyncTask<Void, Integer, String> {
#Override
protected String doInBackground(Void... arg0) {
Log.d("logpp", "Starting download image for " + fProfile.getName());
fProfile.retrieveProfileImage(new ProfileImageCallback());
return null;
}
}
private class ProfileImageCallback extends GetDataCallback {
#Override
public void done(byte[] bytearray, ParseException e) {
if (e == null) {
Log.d("logpp", "Done downloading image for " + fProfile.getName() + ". Setting bitmap to:" +
" " + friendImage.getId());
profileImageBitmap = BitmapManager
.getBitmapFromByteArray(bytearray);
((Activity) context).runOnUiThread(new UpdateUi());
}
}
}
private class UpdateUi implements Runnable {
#Override
public void run() {
friendImage.setImageBitmap(profileImageBitmap);
friendImage.setVisibility(View.VISIBLE);
friendImageProgressBar.setVisibility(View.INVISIBLE);
}
}
}
The retrieveProfileImage method:
public void retrieveProfileImage(GetDataCallback callBack) {
this.image.getDataInBackground(callBack);
}
I hope someone can help me with this one.
Regards,
Tim
i solved this problem by following code
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (inflater == null)
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.answer_item, null);
TextView name = (TextView) convertView.findViewById(R.id.textView_ans_user_name);
TextView body = (TextView) convertView.findViewById(R.id.textView_ans_user_body);
TextView timestamp = (TextView) convertView.findViewById(R.id.textView_ans_user_timestamp);
final CircularImageView thumbnail = (CircularImageView) convertView.findViewById(R.id.imageView_ans_user);
Parse_answer_model ans = answers.get(position);
name.setText(ans.getAns_by());
body.setText(ans.getAns_body());
SimpleDateFormat sdfAmerica = new SimpleDateFormat("dd-M-yyyy hh:mm:ss a");
sdfAmerica.setTimeZone(TimeZone.getDefault());
String sDateInAmerica = sdfAmerica.format(ans.getCreatedAt());
timestamp.setText(sDateInAmerica);
ParseQuery<User> query = ParseQuery.getQuery("_User");
query.whereEqualTo("username", ans.getAns_by());
query.getFirstInBackground(new GetCallback<User>() {
public void done(User user, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
img.DisplayImage(user.getprofile_pic_url(), thumbnail, false);
} else {
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
put your imageview as final dont make it global and you get image url from geturl() method, it is as defined by parse you can use below example
ParseFile fileObject = (ParseFile) object.get("image_file");
User user = new User();
user = (User) ParseUser.getCurrentUser();
user.setProfile_pic_url(fileObject.getUrl().toString());
user.saveInBackground();
update
last day i found new solution you can get user's data which related to parse object by following code and made some changes in model class,too.
void getchats() {
pd.show();
ParseQuery<Parse_chat_dialogs> query = ParseQuery.getQuery("chat_dialogs");
query.addDescendingOrder("updatedAt");
query.whereContains("users", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<Parse_chat_dialogs>() {
public void done(List<Parse_chat_dialogs> dilogs, ParseException e) {
if (e == null) {
pd.hide();
dialoglist = (ArrayList<Parse_chat_dialogs>) dilogs;
adp = new ChatDialogAdapter(Chat_list.this, dialoglist);
list.setAdapter(adp);
for (int i = 0; i < dialoglist.size(); i++) {
ParseQuery<User> query = ParseQuery.getQuery("_User");
query.whereEqualTo("username", dialoglist.get(i).getUsers().trim()
.replace(ParseUser.getCurrentUser().getUsername(), "").replace(",", ""));
User user = new User();
try {
user = query.getFirst();
dialoglist.get(i).setFirstname(user.getFirstname());
dialoglist.get(i).setLastname(user.getLastname());
dialoglist.get(i).setProfileurl(user.getprofile_pic_url());
adp.notifyDataSetChanged();
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
} else {
Toast.makeText(Chat_list.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
as in above example i added three new param in parseobejct model class for storing values of user's firstname ,lastname and profile url.
i am also sharing model class for getting more idea
#ParseClassName("chat_dialogs")
public class Parse_chat_dialogs extends ParseObject {
private String firstname;
private String lastname;
private String profileurl;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getProfileurl() {
return profileurl;
}
public void setProfileurl(String profileurl) {
this.profileurl = profileurl;
}
/////////////////////////////////////////////////////////////////////////////
public String getLast_message() {
return getString("last_message");
}
public void setLast_message(String value) {
put("last_message", value);
}
public String getUsers() {
return getString("users");
}
public void setUsers(String value) {
put("users", value);
}
}
How about this!
Instead of using AsyncTask in the adapter class, use it in the MainActivity where you set the adapter for the listview. And in your done method in the Callback or the postExecute update the object/objects and call notifyDataSetChanged().
So, essentially you could have an update method in your adapter class, say, like this,
public void updateObject(int pos, byte[] byteArray){
//Assuming your Profile Object has some member to store this image data
friends.get(pos).setImageData(byteArray); //friends is the list in adapter class and setImageData may be the setter in your Profile object class
notifyDataSetChanged();
}
and in the getView(), you could do something like this
profileImageBitmap = BitmapManager
.getBitmapFromByteArray(friends.get(pos).getImageData);
friendImage.setImageBitmap(profileImageBitmap);

Categories