【发布时间】:2015-07-16 09:40:51
【问题描述】:
我正在创建一个具有动态ListView 的应用程序,它应该在获得响应后从服务器获取数据并绑定它们,点击Button。我已经完成了收到回复。但不知道如何将数据绑定到ListView。我还创建了一个用于绑定的适配器。但不知道通过适配器绑定数据的方法。谁能帮我?我在下面给出了我的代码以供参考..
主 Activity.java(来自 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();
}
});
适配器类:
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;
}
}
【问题讨论】: