I am working on TCP socket, I receive some bunch of data for every sec and need to display it in ListView for that I am using a dynamic listView with custom array adapter.
My problem is, when I receive a new set of data from TCP socket, a new object is creating for listItem (findViewById), because of it when I scroll my listview, again it is moving to top when I am updating my listView.
When I consider only one set of data I did not find the above issue.
My code:
doAsynchronousTask = new TimerTask() {
#Override
public void run() {
finalizer = new Runnable() {
public void run() {
try {
new RetriveStock().execute(); // AsyncTask Executes for every 1 sec.
} catch (Exception e) {
// TODO: handle exception
}
}
}
};
handler.post(finalizer);
}
};
timer.schedule(doAsynchronousTask, 0, 1000); // execute in every 1000
// AsyncTask
public class RetriveStock extends AsyncTask<Void, Void, ArrayList<User>> {
#Override
protected ArrayList<User> doInBackground(Void... params) {
message += client.clientReceive(1); // Receive data from socket and put into message (string global variable).
printJson(); // Here I receive data from JSON and put into object
adb = new RSSListAdaptor(DefaultMarketWatch.this,
R.layout.rssitemview, list); // "list" is a global listAdapter.
return null;
}
#Override
protected void onCancelled() {
super.onCancelled();
}
protected void onPostExecute(ArrayList<User> result) {
lv.setAdapter(adb);
adb.notifyDataSetChanged();
super.onPostExecute(result);
}
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
// printJson()
public void printJson() {
String str = "";
/* SOME CODE HERE
FORMATING "message" (STRING VARIABLE) TO A PERFECT JSONARRAY FORMATE, AND PUT INTO VARIABLE "str" */
str = "[" + str + "]";
try {
JSONArray jsonArray = new JSONArray(str);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
User obj = new User();
// MY CODE, EITHER ADDING NEW OBJECT OR UPDATING THE EXISTING USER OBJECT.
list.add(obj);
} catch (JSONException e1) {
e1.printStackTrace();
}
}
// My Custom ArrayAdapter.
public class RSSListAdaptor extends ArrayAdapter<User> {
private List<User> objects = null;
public RSSListAdaptor(Context context, int textviewid,
List<User> objects) {
super(context, textviewid, objects);
this.objects = objects;
}
#Override
public int getCount() {
return ((null != objects) ? objects.size() : 0);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
if (null == view) {
LayoutInflater vi = (LayoutInflater) DefaultMarketWatch.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.rssitemview, null);
}
try {
User u = objects.get(position);
if (null != u) {
TextView title_list = (TextView) view.findViewById(R.id.symbol);
TextView ltp_list = (TextView) view.findViewById(R.id.ltp);
TextView high_list = (TextView) view.findViewById(R.id.high);
TextView low_list = (TextView) view.findViewById(R.id.low);
TextView persend_list = (TextView) view.findViewById(R.id.persent);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) title_list
.getLayoutParams();
params.setMargins(0, 0, 25, 0);
title_list.setText("TITLE");
ltp_list.setText("LTP");
high_list.setText("HIGH");
low_list.setText("LOW");
persend_list.setText("PERSENTAGE");
}
} catch (IllegalStateException is) {
is.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
}
Try this:
// save index and top position
int index = mList.getFirstVisiblePosition();
View v = mList.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();
// update listview with new data
// restore
mList.setSelectionFromTop(index, top);
Or
// Save ListView state
Parcelable state = listView.onSaveInstanceState();
// Set new items
listView.setAdapter(adapter);
// Restore previous state (including selected item index and scroll position)
listView.onRestoreInstanceState(state);
Do not recreate the adapter in doInBackground and do not reset it in onPostExecute
Rather create an empty adapter in your constructor or somewhere sensible, and when data comes in, append that data to your adapter then call notifyDataSetChanged
Related
I have a problem with an application on android. More specifically, I have a problem with lagging ListView. I use class ViewHolder. Actually I do not display a lot of things, but my move Items with large lags.
I do not know what I could change, I tried to found answers in search but found no solution to this problem.
I can scroll my view, but in interval few seconds my view stop for 2 sec. I thought that it is problem with update adapter, but i don't know what can i do change.
Sorry for my English.
Here is the code of the adapter:
public class ListAdapter_obiekty extends BaseAdapter{
Context ctx;
LayoutInflater lInflater;
private int screenHeight;
ArrayList<HashMap<String, String>> products,dane;
ListAdapter_obiekty(Context context, ArrayList<HashMap<String, String>> data) {
ctx = context;
this.products = data;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
screenHeight = metrics.heightPixels;
}
#Override
public int getCount() {
return products == null ? 0 : products.size();
}
#Override
public Object getItem(int position) {
return products.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public void updateItem(final String name, final String item, final Integer pos) {
HashMap<String,String> hm = this.products.get(pos);
hm.put(name, item);
this.products.set(pos,hm);
notifyDataSetChanged();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
HashMap<String, String> hm = this.products.get(position);
String tryb = hm.get("tryb");
ListViewHolder holder = null;
if (view == null) {
view = lInflater.inflate(R.layout.list_item, parent, false);
ViewGroup.LayoutParams params = view.getLayoutParams();
if (params.height != screenHeight) params.height = screenHeight/2;
view.setLayoutParams(params);
holder = new ListViewHolder();
holder.tryb = ((TextView) view.findViewById(R.id.tV_tryb));
view.setTag(holder);
}else{
holder = (ListViewHolder) view.getTag();
}
if (position % 2 == 0) {
view.setBackgroundResource(R.drawable.lista_obiekty_niebieski);
holder.tryb.setTextColor(Color.parseColor("#e3e9e4"));
}else{
view.setBackgroundResource(R.drawable.lista_obiekty_pod_logo);
holder.tryb.setTextColor(Color.parseColor("#007bb3"));
}
holder.tryb.setText(hm.get(ctx.getString(R.string.tryb)));
return view;
}
static class ListViewHolder{
TextView tryb;
}
}
Here is the code to create an adapter and update:
runOnUiThread(new Runnable() {
public void run() {
adapter = new ListAdapter_obiekty(HomeActivity.this, productsListNAZWY,productsListDANE);
ListView lvMain = (ListView) findViewById(R.id.list);
lvMain.setAdapter(adapter);
for(int i =0; i<=ile; i++){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HashMap news = null;
news = (HashMap) productsListNAZWY.get(i);
String ip = (String) news.get("numer_ip");
callAsynchronousTask(i, "http://" + ip + "/config/xml.cgi?A|1|74|I|1|29|D|1|62");
Log.d("TAGS","URUCHOMILEM");
}
}
And Here is my function callAsynchronousTask() :
public void callAsynchronousTask(final int i, final String numerIPx) {
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
try {
obiekt=null;
obiekt = new ListaXML();
String[] tabAnal = obiekt.execute(numerIPx).get();
Integer code = obiekt.code;
String trybPracy = "";
trybPracy = tabAnal[10];
HashMap maps = new HashMap<String,String>();
... more items (5-6)
maps.put("trybPracy", trybPracy);
adapter.updateItem(maps,i);
} catch (Exception e) {
}
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 5000);
}
I suspect the lag is due to this bit of the code
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
You're doing this in the main UI thread.
Reduce this time to 100 and see what happens. If it is the cause you will have to re-work your code, as there doesn't seem to be anything wrong with your list adapter.
Edit: comment out this bit of code to find the lag.
for(int i =0; i<=ile; i++){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
HashMap news = null;
news = (HashMap) productsListNAZWY.get(i);
String ip = (String) news.get("numer_ip");
callAsynchronousTask(i, "http://" + ip + "/config/xml.cgi?A|1|74|I|1|29|D|1|62");
Log.d("TAGS","URUCHOMILEM");
}
In one of my app, I need to use AsyncTask. I need to get an image URL from an HTTP site. I have done this in the doInBackground method. I am getting the URL of that image as a string.
publishProgress(thumb);//thumb is string
then in
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
sample = new ArrayList<String>();
System.out.println("this is on progress..."+values[0]);
sample.add(values[0]);
GridviewAdapter_old go=new GridviewAdapter_old(getActivity(), sample);
gv.setAdapter(go);
// gv.setAdapter(ga);
}
public class GridviewAdapter_old extends BaseAdapter
{
private ArrayList<String> listCountry;
private Activity activity;
public GridviewAdapter_old(Activity activity, ArrayList<String> listCountry) {
super();
// this.listCountry = listCountry;
this.listCountry = new ArrayList<String>();
this.listCountry = listCountry;
// this.listFlag = listFlag;
this.activity = activity;
System.out.println("this is contry name " + this.listCountry);
// System.out.println("this is img name " + this.listFlag);
// System.out.println();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println("len " + listCountry.size());
// return listCountry.size();
return listCountry.size();
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder {
public ImageView imgViewFlag;
// public TextView txtViewTitle;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if (convertView == null) {
view = new ViewHolder();
convertView = inflator.inflate(R.layout.test, null);
view.imgViewFlag = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// view.imgViewFlag.setBackgroundResource(R.drawable.view_default);
convertView.setTag(view);
}
else {
view = (ViewHolder) convertView.getTag();
}
try {
URL myUrl = new URL(listCountry.get(0));
InputStream inputStream = (InputStream) myUrl.getContent();
Drawable drawable = Drawable.createFromStream(inputStream, null);
view.imgViewFlag.setImageDrawable(drawable);
} catch (Exception e) {
System.out.println(e.getMessage());
}
/*
* Bitmap imageBitmap = null; //System.gc(); try { URL imageURL = new
* URL(listCountry.get(0));
* System.out.println("this is in last portion..."
* +listCountry.get(position)); imageBitmap =
* BitmapFactory.decodeStream(imageURL.openStream());
* view.imgViewFlag.setImageBitmap(imageBitmap); //
* view.txtViewTitle.setText(listCountry.get(position)); //
* view.imgViewFlag.setImageResource(listFlag.get(position));
*
* } catch (Exception e) { System.out.println("this is error " +
* e.getMessage()); }
*/
return convertView;
}
}
The problem is:
I am not getting an image from the URL (that is getting from thumb (publishProgress(thumb);)
I am not getting the multiple images.
you are only getting one in your list because every time you call onProgressUpdate you create a new list.
You dont want to use onProgressUpdate to populate your list, that would be an insane amount of overhead. Instead you want to create your list inside your doInBackground then pass that list to your onPostExecute then put the new list to the adapter and call notifyDatasetChanged on the adapter to refresh the list.
in short, create the adapter (class wide variable), create an ArrayList (class wide variable) and everytime a change is made the the list all you have to do is call notifyDatasetCHanged on the adapter
ArrayList<String> list = new ArrayList<String();
ArrayAdapter adapter;
public class AsyncTask.....
#Override
protected Void doInBackground(Void... params){
...
list.add(stuff);
}
#Override
protected Void onPostExecute(Void params){
adapter.notifyDatasetChanged();
}
My listview doesn't show exact placement of images on its individual custom adapters. I am trying to load the images using AsyncTask. Sometimes the images duplicates and I don't have any idea why. Here is my ArrayAdapter code:
public class NewsPromoAdapter extends ArrayAdapter<NewsPromosDTO> {
private final Context context;
List<NewsPromosDTO> npList;
LayoutInflater inflater;
Bitmap src;
public NewsPromoAdapter(Context context, List<NewsPromosDTO> npList) {
super(context, R.layout.news_and_promos, npList);
this.context = context;
this.npList = npList;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public static class ViewHolder {
TextView nameText;
TextView date1Text;
ImageView imageView;
ProgressBar prg;
LoadImage loadImg;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.news_and_promos, parent,
false);
holder = new ViewHolder();
holder.nameText = (TextView) convertView
.findViewById(R.id.newspromo_name);
holder.date1Text = (TextView) convertView
.findViewById(R.id.newspromo_date);
holder.imageView = (ImageView) convertView
.findViewById(R.id.newspromo_imageView);
holder.prg = (ProgressBar) convertView
.findViewById(R.id.progressBar1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.loadImg.cancel(true);
}
if (!(npList.get(position).getName().equals(""))) {
holder.nameText.setVisibility(View.VISIBLE);
holder.nameText.setText(npList.get(position).getName());
} else {
holder.nameText.setVisibility(View.GONE);
}
if (!(npList.get(position).getDate().equals(""))) {
holder.date1Text.setVisibility(View.VISIBLE);
holder.date1Text.setText(npList.get(position).getDate());
} else {
holder.date1Text.setVisibility(View.GONE);
}
if (!(npList.get(position).getImageURL().equals(""))) {
holder.imageView.setVisibility(View.VISIBLE);
holder.loadImg = new LoadImage(holder.imageView, npList.get(
position).getImageURL(), holder);
holder.loadImg.execute();
} else {
holder.imageView.setVisibility(View.GONE);
holder.prg.setVisibility(View.GONE);
}
return convertView;
}
class LoadImage extends AsyncTask<Object, Void, Bitmap> {
ViewHolder viewH;
private ImageView imv;
private String url;
public LoadImage(ImageView imv, String imageURL, ViewHolder viewH) {
this.imv = imv;
this.url = imageURL;
this.viewH = viewH;
}
#Override
protected Bitmap doInBackground(Object... params) {
BitmapFromURL bmpURL = new BitmapFromURL();
src = bmpURL.getBitmapFromURL(url);
return src;
}
#Override
protected void onPostExecute(Bitmap result) {
// imv.setImageBitmap(result);
// viewH.prg.setVisibility(View.GONE);
viewH.prg.setVisibility(View.GONE);
imv.setImageBitmap(result);
}
}
}
Here is the Activity that handles the ADAPTER. I am implementing the Endless listview approach that some big sites uses. Like Facebook, Twitter, etc..New Items loaded into the listview when scrolling into the bottom. I am not sure if I did it well.
public class ListNewsPromoActivity extends Activity {
List<NewsPromosDTO> npList;
String url, user_email;
Bundle extras;
int user_points;
List<Integer> npIDs;
private ProgressDialog progressDialog;
BitmapFromURL bmpURL;
JSONArray users = null;
String TAG_ID = "id";
String TAG_NAME = "name";
String TAG_PHOTO = "photo";
String TAG_DATE = "date";
String TAG_DESC = "description";
ListView list;
NewsPromoAdapter newsAdapter;
boolean loadingMore = false;
private Runnable returnRes;
int itemsPerPage = 3;
int currentIndex = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.news_promo_listview);
list = (ListView) findViewById(R.id.mainView);
extras = getIntent().getExtras();
if (extras != null) {
user_points = extras.getInt("user_points");
user_email = extras.getString("user_email");
}
url = getString(R.string.app_url_news_promos);
npIDs = new ArrayList<Integer>();
npList = new ArrayList<NewsPromosDTO>();
View footerView = ((LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.footer, null, false);
newsAdapter = new NewsPromoAdapter(ListNewsPromoActivity.this, npList);
list.addFooterView(footerView);
list.setAdapter(newsAdapter);
list.setOnScrollListener(new OnScrollListener() {
// useless here, skip!
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// what is the bottom item that is visible
int lastInScreen = firstVisibleItem + visibleItemCount;
// is the bottom item visible & not loading more already ? Load
if ((lastInScreen == totalItemCount) && !(loadingMore)) {
Thread thread = new Thread(null, getRunnable());
thread.start();
}
}
});
// Runnable to load the items
// Since we cant update our UI from a thread this Runnable takes care of
// that!
returnRes = new Runnable() {
#Override
public void run() {
// Loop thru the new items and add them to the adapter
for (NewsPromosDTO np : npList) {
System.out.println(np.getName() + " andito ako!!");
newsAdapter.add(np);
}
newsAdapter.notifyDataSetChanged();
// Done loading more.
loadingMore = false;
}
};
}
protected class loadNewsPromo extends AsyncTask<Void, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
protected String doInBackground(Void... params) {
// Creating JSON Parser instance
loadingMore = true;
JSONParser jParser = new JSONParser();
JSONObject json;
if (jParser.checkServer(url)) {
try {
// Getting Array of UserNews
json = jParser.getJSONFromUrl(url);
users = json.getJSONArray(user_email);
// looping through All UserNews
for (int i = currentIndex; i < itemsPerPage; i++) {
if (itemsPerPage == i) {
// currentIndex--;
break;
} else {
JSONObject c = users.getJSONObject(i);
// Storing each json item in variable
NewsPromosDTO npDTO = new NewsPromosDTO();
npDTO.setImageURL(c.getString(TAG_PHOTO));
npDTO.setId(c.getInt(TAG_ID));
npDTO.setName(c.getString(TAG_NAME));
npDTO.setDate(c.getString(TAG_DATE));
npDTO.setDescription(c.getString(TAG_DESC));
npList.add(npDTO);
currentIndex++;
}
}
} catch (JSONException e) {
e.printStackTrace();
return null;
} finally {
}
}
}
return null;
}
#Override
protected void onPostExecute(String result) { //
// progressDialog.dismiss();
loadingMore = false;
list();
}
}
public void list() {
// add the footer before adding the adapter, else the footer will not
// load!
View footerView = ((LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
R.layout.footer, null, false);
newsAdapter = new NewsPromoAdapter(ListNewsPromoActivity.this, npList);
list.addFooterView(footerView);
list.setAdapter(newsAdapter);
}
public Runnable getRunnable() {
Runnable loadMoreListItems = new Runnable() {
#Override
public void run() {
// Set flag so we cant load new items 2 at the same time
loadingMore = true;
// Reset the array that holds the new items
// Simulate a delay, delete this on a production environment!
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
// Get 15 new listitems
JSONParser jParser = new JSONParser();
JSONObject json;
npList = new ArrayList<NewsPromosDTO>();
if (jParser.checkServer(url)) {
try {
// Getting Array of Contacts
json = jParser.getJSONFromUrl(url);
users = json.getJSONArray(user_email);
// looping through All Contacts
// if (itemsPerPage > users.length() - currentIndex) {
// itemsPerPage = users.length();
// }
npList = new ArrayList<NewsPromosDTO>();
System.out.println(users.length() + " Laman ng users");
int counter = 0;
for (int i = currentIndex; i < users.length(); i++) {
if (itemsPerPage == counter) {
break;
} else {
JSONObject c = users.getJSONObject(i);
// Storing each json item in variable
NewsPromosDTO npDTO = new NewsPromosDTO();
npDTO.setImageURL(c.getString(TAG_PHOTO));
npDTO.setId(c.getInt(TAG_ID));
npDTO.setName(c.getString(TAG_NAME));
npDTO.setDate(c.getString(TAG_DATE));
npDTO.setDescription(c.getString(TAG_DESC));
npList.add(npDTO);
currentIndex++;
}
counter++;
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
}
}
// Done! now continue on the UI thread
runOnUiThread(returnRes);
}
};
return loadMoreListItems;
}
}
This question already has answers here:
BaseAdapter class wont setAdapter inside Asynctask - Android
(4 answers)
Closed 9 years ago.
I have an asynctask that gathers comments, usernames, and numbers by using a JSON method. Then I have a class that extends BaseAdapter that suppose to put the comments and usernames into a listView. The problem is how can I get the comments, usernames and numbers to the BaseAdapter class? Here is my current code
class loadComments extends AsyncTask<JSONObject, String, JSONObject> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
}
protected JSONObject doInBackground(JSONObject... params) {
JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);
return json2;
}
#Override
protected void onPostExecute(JSONObject json2) {
try {
if (json2.getString(KEY_SUCCESS) != null) {
registerErrorMsg.setText("");
String res2 = json2.getString(KEY_SUCCESS);
if(Integer.parseInt(res2) == 1){
l1=(ListView)findViewById(R.id.list);
JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
String comments[] = new String[commentArray.length()];
for ( int i=0; i<commentArray.length(); i++ ) {
comments[i] = commentArray.getString(i);
}
JSONArray numberArray = json2.getJSONArray(KEY_NUMBER);
String numbers[] = new String[numberArray.length()];
for ( int i=0; i<numberArray.length(); i++ ) {
numbers[i] = numberArray.getString(i);
}
JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
String usernames[] = new String[usernameArray.length()];
for ( int i=0; i<usernameArray.length(); i++ ) {
usernames[i] = usernameArray.getString(i);
}
}//end if key is == 1
else{
// Error in registration
registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
}//end else
}//end if
} //end try
catch (JSONException e) {
e.printStackTrace();
}//end catch
}
}
new loadComments().execute();
class CreateCommentLists extends BaseAdapter{
Context ctx_invitation;
String[] listComments;
String[] listNumbers;
String[] listUsernames;
public CreateCommentLists(Context ctx_invitation, String[] comments, String[] Numbers, String[] usernames)
{
super();
this.ctx_invitation = ctx_invitation;
listComments = comments;
listNumbers = Numbers;
listUsernames = usernames;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listComments.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listComments[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = null;
try
{
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)ctx_invitation.getSystemService(inflater);
v = li.inflate(R.layout.list_item, null);
TextView commentView = (TextView)v.findViewById(R.id.listComment);
TextView NumbersView = (TextView)v.findViewById(R.id.listNumber);
TextView usernamesView = (TextView)v.findViewById(R.id.listPostedBy);
commentView.setText(listComments[position]);
NumbersView.setText(listNumbers[position]);
usernamesView.setText(listUsernames[position]);
}
catch(Exception e)
{
e.printStackTrace();
}
return v;
}
}
overwrite the values of the adapter you have implemented or add setters for them. Then set the values and make sure you call notifyDatasetChanged on the adapter.
You can either reference the adapter directly if it is a global variable or pass it in the constructor of your AsyncTask
In onPostExecute you can create a new BaseAdapter with the new data and then use setAdapter to set it as the adapter for your ListView. Or, as already said, you can use setters to update data in your current adapter and then notify the ListView that it should redraw itself.
By the very nature of AsyncTask you can update your UI wherever you want in onPreExecution, onProgressUpdate and onPostExecution.
I'm working on a App that should get a JSON response from a webservice and write every element in a listview, I have read that I should work with AsyncTask to get the HTTP Response and I did it and I could retrieve data from the webservice and display them in TextViews. But when I try to display elements in a listview it doesn't display anything and gives me the following message in the logcat : 06-05 19:44:27.418: I/Choreographer(20731): Skipped 60 frames! The application may be doing too much work on its main thread.
here's my main code :
public class MainActivity extends Activity {
private static JsonObject response = new JsonObject();
private ArrayList<SearchResults> results = new ArrayList<SearchResults>();
private SearchResults sr1 = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new LoginAction().execute("");
ArrayList<SearchResults> searchResults = results;
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(new MyCustomBaseAdapter(this, searchResults));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class LoginAction extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
Map<String, String> callArgs = new HashMap<String, String>(1);
callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");
try {
response = EventPulseCloud.call("ListEvents", callArgs);
} catch (HttpClientException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JsonException e) {
e.printStackTrace();
}
return response.get("Type").toString();
}
protected void onPostExecute(String result) {
if(result.equals("success")) {
JsonArray records = null;
try {
records = response.getObject ("Data").getArray ("Records");
} catch (JsonException e) {
e.printStackTrace();
}
for(int i = 0; i < records.count(); i++) {
JsonObject record = (JsonObject) records.get(i);
sr1 = new SearchResults();
sr1.setAddress(record.get("address").toString());
results.add(sr1);
}
}
}
}
}
My list adapter :
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.custom_row_view, null);
holder = new ViewHolder();
holder.txtAddress = (TextView) convertView.findViewById(R.id.address);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtAddress.setText(searchArrayList.get(position).getAddress());
return convertView;
}
static class ViewHolder {
TextView txtAddress;
}
}
and finally, SearchResults.java :
public class SearchResults {
private String address = "";
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
So, what do I do wrong ? Do you have an idea about this ?
Thank you.
onPostExecute() happens on the Main UI thread. It looks like you are still doing a fair amount of work in that method that should be done off the UI thread, i.e. processing the response, iterating over JSON objects, etc. Do that in doInBackground() and have that return a list of results, so the only thing onPostExecute needs to do is pass the new items to your list adapter.
Also, do not use the same ArrayList as the one your adapter holds. If for some reason the adapter discovers that the data has changed without you having called notifyDataSetChanged(), it will probably crash (or at least display odd behaviors). Create a new ArrayList in your AsyncTask, then put this in your Adapter and call it from onPostExecute:
public void setListItems(ArrayList<SearchResult> newList) {
searchArrayList = newList;
notifyDataSetChanged();
}
private class LoginAction extends AsyncTaskList<String, Void, ArrayList<SearchResult>> {
#Override
protected ArrayList<SearchResult> doInBackground(String... params) {
List<SearchResults> resultList = new ArrayList<SearchResults>();
Map<String, String> callArgs = new HashMap<String, String>(1);
callArgs.put("suuid", "dtr0bdQGcqwSh3QO7fVwgVfBNWog6mvEbAyljlLX9E642Yfmur");
try {
response = EventPulseCloud.call("ListEvents", callArgs);
} catch (HttpClientException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JsonException e) {
e.printStackTrace();
}
//See here I am running the loop in the background so its not on the main thread, then passing the list off to the onpostexecute that way all the main thread does is set the adapter list and notify it of the data update and the list should be updated on the screen
if( response.get("Type").toString().equals("success")) {
JsonArray records = null;
try {
records = response.getObject ("Data").getArray ("Records");
} catch (JsonException e) {
e.printStackTrace();
}
for(int i = 0; i < records.count(); i++) {
JsonObject record = (JsonObject) records.get(i);
sr1 = new SearchResults();
sr1.setAddress(record.get("address").toString());
resultList.add(sr1);
}
}
return resultList;
}
protected void onPostExecute(ArrayList<SearchResult> resultList) {
setListItems(resultList);
}
}
}
add this line before the oncreate with all your other global var
//here you want to create an adapter var with your base adapter so you can set it the updated list later when you have populated data from the internet
ArrayList<SearchResults> searchResults = new ArrayList<SearchResults>();
MyCustomBaseAdapter adapter = new MyCustomBaseAdapter(this, searchResults)
paste this over your oncreate method (replace it)
//here is just the code to update your main method to reflect all the changes I made
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new LoginAction().execute("");
final ListView lv1 = (ListView) findViewById(R.id.ListView01);
lv1.setAdapter(adapter);
}
and add this method to the adapter(MyCustomnBaseAdapter class) code
public void setListItems(ArrayList<SearchResult> newList) {
searchArrayList = newList;
notifyDataSetChanged();
}