I am creating an app with dynamic ListView
, which should fetch data from the server and bind them after getting the responses, on a Button
click. I've done upto receiving response. But dont know how to bind the data to ListView
. I've also created an Adapter that is used to bind. But doesnt know the way to bind the data via adapter. Can anyone help me? I've given my codes below for reference..
Main Activity.java (from onCreate):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_payment);
list = (ListView) findViewById(R.id.take_payment_list);
adapter = new CustomListAdapter(this, notifications);
list.setAdapter(adapter);
refresh_btn = (Button) findViewById(R.id.refresh_btn);
refresh_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
HttpHandler httpHandler1 = new HttpHandler();
String res = null;
try {
Log.d("edwLog", TAG + " get_payment_notifications " + HttpHandler.API_URL + "get_payment_notifications/" + AuthToken + "/");
res = httpHandler1.makeServiceCall(HttpHandler.API_URL + "get_payment_notifications/" + AuthToken + "/", HttpHandler.GET);
Log.d("edwLog", TAG + " response > " + res);
if (res != null) {
JSONObject jsonObject = new JSONObject(res);
String responseType = jsonObject.getString("type");
if (responseType.equals("success")) {
if (jsonObject.has("response")) {
JSONArray jsonArray = jsonObject.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++) {
notificationsresponse.add(jsonArray.getJSONObject(i));
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
Log.d("edwLog", TAG + " IOException > " + e.toString());
}
adapter.notifyDataSetChanged();
}
});
Adapter Class:
public class CustomListAdapter extends BaseAdapter {
private final Activity activity;
private LayoutInflater inflater;
private List<Users> notifications;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Users> notifications) {
this.activity = activity;
this.notifications = notifications;
}
@Override
public int getCount() {
return notifications.size();
}
@Override
public Object getItem(int location) {
return notifications.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.custom_rows, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
TextView name = (TextView) convertView.findViewById(R.id.txt_customer_name4);
TextView time = (TextView) convertView.findViewById(R.id.txt_notification_time4);
// getting notifications data for the row
Users m = notifications.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailurl(), imageLoader);
// name
name.setText(m.getName());
// notification time
/*time.setText(String.valueOf(m.getTime()));*/
CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
Long.parseLong(m.getTime()), System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
time.setText(timeAgo);
return convertView;
}
}