我正在创建一个包含动态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;
}
}
答案 0 :(得分:2)
首先创建一个你已经完成的适配器:
list = (ListView) findViewById(R.id.take_payment_list);
adapter = new CustomListAdapter(this, notifications);
list.setAdapter(adapter);
但是在第二行列表项缺失,休息一切正常:
adapter = new CustomListAdapter(this,<<listItem>>, notifications);
现在收到回复
将您的项目添加到notifications数组,然后更改适配器以获得新的通知数组:
notifications.add("data");
adapter = new CustomListAdapter(this,<<listItem>>, notifications);
答案 1 :(得分:0)
执行简单的步骤:
适配器中的覆盖方法:
public void notifyDataSetChanged(List<Users> notifications){
this.notifications = notifications;
super.notifyDataSetChanged();
}
现在,而不是打电话:
adapter.notifyDataSetChanged();
称之为:
adapter.notifyDataSetChanged(notificationsresponse);
此外,从服务器获取数据的方法也不正确。当您尝试在主UI线程上获取数据时,这将减慢用户体验。您应该只在异步线程中调用服务器/数据库调用,并在UI线程上更新该响应。
答案 2 :(得分:0)
首先,永远不要在主线程上调用Web服务,在AsyncTask
中调用它们。 AsyncTask
将是您的Activity / Fragment类的内部类。
private class WebServiceCaller extends AsyncTask<String, Void, JSONObject> {
@Override
protected JSONObject doInBackground(String... params) {
//Call your web service here
}
return response; //the json response you get
}
@Override
protected void onPostExecute(JSONObject result) {
//here bind the data using your adapter to the listview
//Parse the JSONObject you get as the response and add them to the list of Your User
//now call the adapter
adapter= new CustomListAdapter(MainActiviy.this,List);
listView.setAdapter(adapter);
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
答案 3 :(得分:0)
如果您希望再次使用获取的数据,请先将其存储在 Sqlite数据库中,否则请使用 ArrayAdapter 将数据绑定到listView。
参考this链接。
答案 4 :(得分:0)
在你们的帮助下成功找到它。它只是在获得响应后添加这3行代码。对于我的应用程序,它是这样的:
adapter = new CustomListAdapter(TakePayment.this, R.layout.custom_rows, (ArrayList<JSONObject>) notificationsresponse);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();