android-notifyDataSetChanged doesn't work on BaseAdapter - android

i've a listview on my activity , when I reach the end of listview , it calls async and it getting new data using json .
this is the async and baseAdaper codes :
ListAdapter ladap;
private class GetContacts AsyncTask<Void, Void,ArrayList<HashMap<String, String>>> {
#Override
protected Void doInBackground(Void... arg0) {
Spots_tab1_json sh = new Spots_tab1_json();
String jsonStr = sh.makeServiceCall(url + page, Spots_tab1_json.GET);
ArrayList<HashMap<String, String>> dataC = new ArrayList<HashMap<String, String>>();
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = new String(c.getString("id").getBytes("ISO-8859-1"), "UTF-8");
String dates = new String(c.getString("dates").getBytes("ISO-8859-1"), "UTF-8");
String price = new String(c.getString("gheymat").getBytes("ISO-8859-1"), "UTF-8");
HashMap<String, String> contact = new HashMap<String, String>();
contact.put("id", id);
contact.put("dates", dates);
contact.put("price", price);
dataC.add(contact);
}
}
} catch (JSONException e) {
goterr = true;
} catch (UnsupportedEncodingException e) {
goterr = true;
}
} else {
goterr = true;
}
return dataC;
}
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
if (!isCancelled() && goterr == false) {
if(ladap==null){
ladap=new ListAdapter(MainActivity.this,result);
lv.setAdapter(ladap);
}else{
ladap.addAll(result);
ladap.notifyDataSetChanged();
}
}
}
public class ListAdapter extends BaseAdapter {
Activity activity;
public ArrayList<HashMap<String, String>> list;
public ListAdapter(Activity activity,ArrayList<HashMap<String, String>> list) {
super();
this.activity = (Activity) activity;
this.list = list;
}
public void addAll(ArrayList<HashMap<String, String>> result) {
Log.v("this",result.size()+" resultsize");
this.list = result;
notifyDataSetChanged();
}
public int getCount() {
return contactList.size();
}
public Object getItem(int position) {
return contactList.get(position);
}
public long getItemId(int arg0) {
return 0;
}
private class ViewHolder {
TextView title,price;
ImageView img ;
//RelativeLayout rl;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, null);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.price = (TextView) convertView.findViewById(R.id.price);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
item = contactList.get(position);
holder.price.setText(item.get("price"));
return convertView;
}
}
I logged here , when I reach the end of listView , it calls addAll and it returns new 30 items buy it doesn't added to listview , I don't know why .

First of all you have already notify your list view when you call addAll() so this code no longer required please remove from your code :
ladap.notifyDataSetChanged();
Add new data to list view data holder instead of assign new data to list view data holder :
public void addAll(ArrayList<HashMap<String, String>> result) {
Log.v("this",result.size()+" resultsize");
this.list = result;
notifyDataSetChanged();
}
Replace with :
public void addAll(ArrayList<HashMap<String, String>> result) {
Log.v("this",result.size()+" resultsize");
if(list==null){
list = new ArrayList<HashMap<String, String>>();
}
list.addAll(result)
notifyDataSetChanged();
}

Try to change this :
public void addAll(ArrayList<HashMap<String, String>> result) {
Log.v("this",result.size()+" resultsize");
this.list = result;
notifyDataSetChanged();
}
To :
public void addAll(ArrayList<HashMap<String, String>> result) {
Log.v("this",result.size()+" resultsize");
for(int i = 0;i < result.size(); i++)
list.add(result.get(i));
//notifyDataSetChanged(); //no need to call again here
}
The point is, i think this.list = result; will delete the old items before adding a new item.

Related

No adapter attached; skipping layout when using RecyclerView

I'm new in recycle view. Tthis is sample code for my RecycerView. I'm getting data from internet and in onPostExecute() I set the adapter.
RecyclerView recycle;
MyAdapter adapters;
private static String url;
private static final String TAG_CONTACTS = "contacts";
JSONArray contacts = null;
ProgressDialog pDialog;
private int preLast;
int page = 0, in, to;
Boolean loadmore = true;
HashMap<String, String> item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url = "http://192.168.1.20/adres/getAds.php";
recycle = (RecyclerView) findViewById(R.id.recycle);
recycle.setItemAnimator(new DefaultItemAnimator());
new GetContacts().execute();
final GestureDetector mGestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
});
}
private class GetContacts extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {
Boolean goterr = false;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setCancelable(true);
pDialog.show();
}
#Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {
String jsonStr = Fun.getHtml(url);
Log.v("this", jsonStr);
ArrayList<HashMap<String, String>> dataC = new ArrayList<HashMap<String, String>>();
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
if (contacts.length() < 20)
loadmore = false;
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
HashMap<String, String> contact = new HashMap<String, String>();
contact.put("id", new String(c.getString("id").getBytes("ISO-8859-1"), "UTF-8"));
contact.put("name", new String(c.getString("name").getBytes("ISO-8859-1"), "UTF-8"));
dataC.add(contact);
dataC.add(contact);
}
} catch (JSONException e) {
Log.v("this", e.getMessage());
goterr = true;
} catch (UnsupportedEncodingException e) {
Log.v("this", e.getMessage());
goterr = true;
}
} else {
goterr = true;
}
return dataC;
}
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
if (pDialog.isShowing() && pDialog != null)
pDialog.dismiss();
if (!isCancelled() && goterr == false && result != null) {
if (adapters == null) {
adapters = new MyAdapter(MainActivity.this, result);
recycle.setAdapter(adapters);
recycle.setLayoutManager(new LinearLayoutManager(MainActivity.this));
} else {
adapters.addAll(result);
}
} else {
//MyToast.makeText(MainActivity.this, DariGlyphUtils.reshapeText(MainActivity.this.getResources().getString(R.string.problemload)));
}
}
}
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private LayoutInflater inflater;
public ArrayList<HashMap<String, String>> list;
public MyAdapter(Context context, ArrayList<HashMap<String, String>> list) {
inflater = LayoutInflater.from(context);
this.list = list;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parrent, int i) {
View view = inflater.inflate(R.layout.customrow, parrent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
public void addAll(ArrayList<HashMap<String, String>> result) {
if (this.list == null) {
this.list = result;
} else {
this.list.addAll(result);
}
notifyDataSetChanged();
}
public HashMap<String, String> geting(int position) {
return list.get(position);
}
#Override
public void onBindViewHolder(MyViewHolder viewHolder, int position) {
item = list.get(position);
viewHolder.txt.setText(item.get("onvan"));
}
#Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView txt;
ImageView img;
public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txt = (TextView) itemView.findViewById(R.id.txt);
img = (ImageView) itemView.findViewById(R.id.img);
}
#Override
public void onClick(View v) {
item = adapters.geting(getPosition());
Log.v("this", "id " + item.get("id"));
/*Intent in=new Intent (FistActiivty.this,AdDetails.class);
in.putExtra("ad_id",item.get("id"));
startActivity(in)*/
;
}
}
}
after I run it , I get this error :
RecyclerView﹕ No adapter attached; skipping layout
what is wrong with this code ?
The problem is that during the first layout pass, while your AsyncTask is still fetching data from the network, your RecyclerView has not adapter.
You can instead attach the (empty) adapter in onCreate() and update the adapter's data in your onPostExecute(). Just make sure that your adapter properly handles having an empty data set.

Show the Elements from ArrayList of map ,in list form in Android

I have a A data stored in ArrayList< HashMap< String, String> > retrieved from JSON
in the form (i.e.)
[{price: =1685 name: =Monographie Der Gattung Pezomachus (Grv.) by Arnold F. Rster}]
And I need to show the all map elements into list form in Android.
I've tried many ways but I'm unable to do it .
Also help me to know about the layouts to use in it
EDITED:
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(myarr_list);
setListAdapter(adapter);
And in the MySimpleArrayAdapter Class, in Constructor
public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl) {
LayoutInflator inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
The control does not proceed after this,
MySimpleArrayAdapter Class
public class MySimpleArrayAdapter extends BaseAdapter{
ArrayList<HashMap<String, String>> ProductList = new ArrayList<HashMap<String, String>>();
LayoutInflater inflater;
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#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;
}
//Constructor
public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl) {
this.ProductList = pl;
inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View getView(int position, View convertView, ViewGroup parent) {
View myview = convertView;
if (convertView == null) {
myview = inflater.inflate(R.layout.show_search_result, null);
}
TextView price = (TextView) myview.findViewById(R.id.price);
TextView name = (TextView) myview.findViewById(R.id.name);
HashMap<String, String> pl = new HashMap<String, String>();
pl = ProductList.get(position);
//Setting
price.setText(pl.get("price"));
name.setText(pl.get("name"));
return myview;
}
}
I am editing here a onPostExecute class from SearchResultsTask extended by AsyncTask
protected void onPostExecute(JSONObject json) {
if (json != null && json.length() > 0) {
try {
JSONArray json_results = (JSONArray)(json.get("results"));
String parsedResult = "";
System.out.println("-> Size ="+ json_results.length());
for(int i = 0; i < json_results.length(); i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject json_i = json_results.getJSONObject(i);
map.put("name: ",json_i.getString("name") + "\n");
map.put("price: ",json_i.getString("price") + "\n");
arr_list.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
System.out.println("-> Size =====arr_llist ="+ arr_list.size());
// CustomListAdapter adapter = new CustomListAdapter (arr_list);
//final StableArrayAdapter adapter = new StableArrayAdapter(this, R.id.result, arr_list);
// listview.setAdapter(adapter);
MyListActivity obj1 = new MyListActivity();
Bundle icicle = null;
obj1.onCreate(icicle);
}
public class MyListActivity extends Activity {
public void onCreate(Bundle icicle) {
// System.out.println("In my list Activity");
// super.onCreate(icicle);
//populate list
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this,arr_list);
// System.out.println("in 2");
adapter.getView(0, listview, listview);
listview.setAdapter(adapter);
}
}
#Override
public int getCount() {
return ProductList.size() ;
}
//Constructor
public MySimpleArrayAdapter( ArrayList<HashMap<String,String>> pl, Context c) {
this.ProductList = pl;
inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
getSystemService() is method of context, you are calling it on instance of adapter.

Loading new LazyAdapter from existing LazyAdapter

Ok, here we go. I have provided full mockup to avoid question why you don't do this that way, or this way.. So blue box is just inflated header that works, below the blue header I have Activity with ListView (green) populated with a LazyAdapter. Each row (white) have buttons that on click opens dialog (yellow) with another ListView populated with LazyAdapterTwo(black).
Problem is that I have null exception while setting second adapter from first, so I don't know where the problem is: listStatusComments.setAdapter(adapterStatusComments);
LazyAdapterStatus class
public class LazyAdapterStatus extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
ProgressDialog progressDialog;
DatabaseHandler db = new DatabaseHandler(activity);
ListView listStatusComments;
LazyAdapterStatusComments adapterStatusComments;
public LazyAdapterStatus(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.activity_main_list_row, null);
final Dialog dialog = new Dialog(activity);
TextView title = (TextView)vi.findViewById(R.id.activityStatus);
ImageButton comments = (ImageButton)vi.findViewById(R.id.comments_ico);
comments.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
HashMap<String, String> item = new HashMap<String, String>();
item = data.get(position);
dialog.setContentView(R.layout.dialog_status_comment);
dialog.setTitle("Comment");
String statusId = item.get(MainActivity.STATUS_ID);
new GetStatusCommentsTask().execute(statusId);
}
});
HashMap<String, String> item = new HashMap<String, String>();
item = data.get(position);
title.setText(item.get(MainActivity.STATUS_TEXT));
return vi;
}
public class GetStatusCommentsTask extends AsyncTask<String, Void, ArrayList<StatusComments>> {
protected void onPreExecute() {
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Loading");
progressDialog.setCancelable(true);
progressDialog.show();
}
protected ArrayList<StatusComments> doInBackground(String... arg0) {
String response = null;
try {
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpPost postReq = new HttpPost();
postReq.setURI(new URI(com.seventy.in.util.ClientUrls.GET_STATUS_COMMENTS_URL));
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair(
"status_id", arg0[0]));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameters);
postReq.setEntity(formEntity);
HttpResponse httpResponse = client.execute(postReq);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
response = NetworkUtil
.convertStreamToString(httpResponse.getEntity()
.getContent());
Log.i("RESP", response);
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (response != null && response != "") {
JSONObject myAwway = new JSONObject(response);
JSONArray jsonArrComments = myAwway.getJSONArray("status_comments");
ArrayList<StatusComments> commentsList = new ArrayList<StatusComments>();
for (int i = 0; i < jsonArrComments.length(); i++) {
StatusComments comment = StatusComments.fromJson(jsonArrComments
.getJSONObject(i));
if (comment.getCommentUserId() != null) {
commentsList.add(comment);
}
}
return commentsList;
}
else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
#Override
protected void onPostExecute(ArrayList<StatusComments> result) {
super.onPostExecute(result);
progressDialog.dismiss();
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.dialog_status_comment);
dialog.setTitle("Patka");
final ArrayList<HashMap<String, String>> postList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < result.size(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
StatusComments e = result.get(i);
map.put(MainActivity.COMMENT_USER_ID, e.getCommentUserId());
map.put(MainActivity.KEY_IMAGE_URL, e.getUserImageUrl());
map.put(MainActivity.STATUS_TEXT, e.getCommentText());
postList.add(map);
}
listStatusComments = (ListView)dialog.findViewById(R.id.list);
adapterStatusComments=new LazyAdapterStatusComments(activity, postList);
listStatusComments.setAdapter(adapterStatusComments);
}
}
}
LazyAdapterStatusComments
public class LazyAdapterStatusComments extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapterStatusComments(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.friends_list_row, null);
TextView title = (TextView)vi.findViewById(R.id.name); // title
TextView about = (TextView)vi.findViewById(R.id.item_description); // artist name
ImageView thumb_image=(ImageView)vi.findViewById(R.id.image_url); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
// Setting all values in listview
title.setText(song.get(MainActivity.STATUS_TEXT));
imageLoader.DisplayImage(song.get(MainActivity.KEY_IMAGE_URL), thumb_image);
return vi;
}
}
I found the problem. Code works perfectly, only I forgot to enter list element into the xml layout so listStatusComments = (ListView)dialog.findViewById(R.id.list); was searching for a element that was not there

android- Dynamically add more items to custom gridview json

I have a gridview on my activity ,I get data via json and add them to my adapter .
this is the code:
gridView = (GridView) findViewById(R.id.gridView);
contactList = new ArrayList<HashMap<String, String>>();
new GetContacts().execute();
private class GetContacts extends AsyncTask<Void, Void,ArrayList<HashMap<String, String>>> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {
Spots_tab1_json sh = new Spots_tab1_json();
String jsonStr = sh.makeServiceCall(url+page, Spots_tab1_json.GET);
ArrayList<HashMap<String, String>> dataC = new ArrayList<HashMap<String, String>>();
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
if(contacts.length()<20)
loadmore=false;
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
HashMap<String, String> contact = new HashMap<String, String>();
contact.put("id", new String(c.getString("id").getBytes("ISO-8859-1"), "UTF-8"));
contact.put("url", new String(c.getString("url").getBytes("ISO-8859-1"), "UTF-8"));
contact.put("text", new String(c.getString("text").getBytes("ISO-8859-1"), "UTF-8"));
dataC.add(contact);
}
} catch (JSONException e) {
goterr=true;
} catch (UnsupportedEncodingException e) {
goterr=true;
}
} else {
Log.v("this","mi;;");
goterr=true;
}
return dataC;
}
#Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
super.onPostExecute(result);
if(!isCancelled() && goterr==false){
if(ladap==null){
ladap=new ListAdapter(FistActiivty.this,result);
gridView.setAdapter(ladap);
}else{
ladap.addAll(result);
}
}else{
MyToast.makeText(FistActiivty.this, DariGlyphUtils.reshapeText(getResources().getString(R.string.problemload)));
}
}
}
public class ListAdapter extends BaseAdapter {
Activity activity;
public ArrayList<HashMap<String,String>> list;
public ListAdapter(Activity activity, ArrayList<HashMap<String, String>>list ) {
super();
this.activity=FistActiivty.this;
this.list=list;
}
public HashMap<String, String> geting(int position) {
return list.get(position);
}
public void addAll(ArrayList<HashMap<String, String>> result) {
if(this.list==null){
//this.list = new ArrayList<HashMap<String, String>>();
this.list =result;
}else{
this.list.addAll(result);
}
//list.addAll(result);
notifyDataSetChanged();
}
public int getCount() {
return list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int arg0) {
return 0;
}
private class ViewHolder {
TextView Message;
ImageView img ;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_grid, null);
holder = new ViewHolder();
holder.Message = (TextView) convertView.findViewById(R.id.text);
holder.Message.setTypeface(typeface);
holder.img=(ImageView)convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
item = list.get(position);
String text = item.get("text");
Log.v("this","11"+ text);
holder.Message.setText(text);
String imgurl = item.get("url");
if(imgurl.length()>5)
imageLoader.displayImage(imgurl, holder.img,options, imageListener);
else
holder.img.setImageDrawable(getResources().getDrawable(R.drawable.noimage));
return convertView;
}
ok , at this step, I get data from internet via json , post it to ListAdapter and add them to my gridview and no problem .
in listview ,we can add ask for more data when we reach at the bottom of listview , But how can I call function to add more items whwen I reach at the end of gridview ?
The best way to implement this is via using PullToRefreshLibrary here. Import that library to your workspace and implement setOnRefreshListener with onPullUpToRefresh. This will indicate the end of the list. You can set a request to the server to get more data to view when the end of the list is reached.
On the server, you can implement pagination to load the next set of data every time you reach the end of the list.
I hope this will help you.

Items in listview not setting up using BaseAdapter and SimpleAdapter

I'm facing problem with setting up the items in ListView, I'm using an Async task for updating the items. Here is what I have done so far.
Async Task onPostExecute()
#Override
protected void onPostExecute(String result) {
notifyList = new ArrayList<HashMap<String, String>>();
try {
JSONObject rootObj = new JSONObject(result);
JSONObject jSearchData = rootObj.getJSONObject("notifications");
int maxlimit = 5;
for (int i = 0; i < maxlimit; i++) {
JSONObject jNotification0 = jSearchData.getJSONObject(""
+ i + "");
String text = jNotification0.getString("text");
String amount = jNotification0.getString("amount");
String state = jNotification0.getString("state");
System.out.println(text);
System.out.println(amount);
System.out.println(state);
HashMap<String, String> map = new HashMap<String, String>();
map.put("text", text);
map.put("amount", amount);
notifyList.add(map);
}
if (notification_adapter != null) {
notification_list.setAdapter(new CustomNotificationAdapter(
notifyList));
}
} catch (Exception e) {
e.printStackTrace();
}
}
Here is my CustomNotification class which extends BaseAdapter
public class CustomNotificationAdapter extends BaseAdapter {
public ArrayList<HashMap<String, String>> notificationData = new ArrayList<HashMap<String, String>>();
public CustomNotificationAdapter(
ArrayList<HashMap<String, String>> notificationData) {
this.notificationData = notificationData;
}
#Override
public int getCount() {
return notificationData.size();
}
#Override
public Object getItem(int position) {
return notificationData.get(position).get("text").toString();
}
#Override
public long getItemId(int position) {
return notificationData.get(position).get("text").hashCode();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
LayoutInflater inflater = getLayoutInflater();
vi = inflater.inflate(R.layout.custom_notification_list, null);
TextView notificationText = (TextView) findViewById(R.id.notificationText);
TextView notificationAmount = (TextView) findViewById(R.id.notificationPoint);
notificationText
.setText(notificationData.get(position).get("text"));
notificationAmount.setText(notificationData.get(position).get(
"amount"));
return vi;
}
}
NotificationAdapter class which extends SimpleAdapter
public class NotificationAdapter extends SimpleAdapter {
List<Map<String, String>> cur_list = new ArrayList<Map<String, String>>();
public NotificationAdapter(Context context,
List<? extends Map<String, ?>> data, int resource, String[] from,
int[] to) {
super(context, data, resource, from, to);
}
}
I'm able to get all the data from JSONResponse but I'm not able to show it on the list. What am I missing?
Any kind of help will be appreciated.
Try replacing this:
if (notification_adapter != null) {
notification_list.setAdapter(new CustomNotificationAdapter(
notifyList));
}
with this:
notification_adapter = new CustomNotificationAdapter(notifyList);
notification_list.setAdapter(notification_adapter);
This will set the adapter to the new JSON data even if notification_adapter was previously null.
Call notification_adapter.notifyDataSetChanged() after updating your adapter's data or remove the if (notification_adapter != null) {if you want it easy and bad.
to update your data:
public void updateData(ArrayList<HashMap<String, String> notificationData){
this.notificationData = notificationData;
this.notifyDataSetChanged();
}
Inside your adapter, and call it like: notification_adapter.updateData(notifyList);

Categories

Resources