在 android 中得到响应后,如何在 ListView 中绑定数据?
How to bind data in ListView after getting responses in android?
我正在创建一个具有动态 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();
}
});
适配器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;
}
}
做简单的步骤:
在您的适配器中覆盖方法:
public void notifyDataSetChanged(List<Users> notifications){
this.notifications = notifications;
super.notifyDataSetChanged();
}
现在,而不是调用:
adapter.notifyDataSetChanged();
称其为:
adapter.notifyDataSetChanged(notificationsresponse);
另外,您从服务器获取数据的方法不正确。当您尝试在主 UI 线程上获取数据时,这会降低用户体验。您应该仅在异步线程中调用 server/database 调用并在 UI 线程上更新该响应。
首先,永远不要在主线程上调用 Web 服务,在 AsyncTask
中调用它们。 AsyncTask
将是您 Activity/Fragment class 的内部 class。
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) {}
}
如果你想再次使用获取的数据然后首先将它存储在 Sqlite 数据库,如果不是然后使用 ArrayAdapter将数据绑定到 listView。
参考thislink.
首先创建一个适配器,您已经完成了:
list = (ListView) findViewById(R.id.take_payment_list);
adapter = new CustomListAdapter(this, notifications);
list.setAdapter(adapter);
但是第2行列表项丢失,其余一切正常:
adapter = new CustomListAdapter(this,<<listItem>>, notifications);
收到回复后现在
将您的项目添加到通知数组,然后更改适配器以获得新的通知数组:
notifications.add("data");
adapter = new CustomListAdapter(this,<<listItem>>, notifications);
在你们的帮助下成功找到了。收到响应后,只需添加这 3 行代码。对于我的应用程序,它是这样的:
adapter = new CustomListAdapter(TakePayment.this, R.layout.custom_rows, (ArrayList<JSONObject>) notificationsresponse);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
我正在创建一个具有动态 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();
}
});
适配器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;
}
}
做简单的步骤:
在您的适配器中覆盖方法:
public void notifyDataSetChanged(List<Users> notifications){
this.notifications = notifications;
super.notifyDataSetChanged();
}
现在,而不是调用:
adapter.notifyDataSetChanged();
称其为:
adapter.notifyDataSetChanged(notificationsresponse);
另外,您从服务器获取数据的方法不正确。当您尝试在主 UI 线程上获取数据时,这会降低用户体验。您应该仅在异步线程中调用 server/database 调用并在 UI 线程上更新该响应。
首先,永远不要在主线程上调用 Web 服务,在 AsyncTask
中调用它们。 AsyncTask
将是您 Activity/Fragment class 的内部 class。
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) {}
}
如果你想再次使用获取的数据然后首先将它存储在 Sqlite 数据库,如果不是然后使用 ArrayAdapter将数据绑定到 listView。
参考thislink.
首先创建一个适配器,您已经完成了:
list = (ListView) findViewById(R.id.take_payment_list);
adapter = new CustomListAdapter(this, notifications);
list.setAdapter(adapter);
但是第2行列表项丢失,其余一切正常:
adapter = new CustomListAdapter(this,<<listItem>>, notifications);
收到回复后现在
将您的项目添加到通知数组,然后更改适配器以获得新的通知数组:
notifications.add("data");
adapter = new CustomListAdapter(this,<<listItem>>, notifications);
在你们的帮助下成功找到了。收到响应后,只需添加这 3 行代码。对于我的应用程序,它是这样的:
adapter = new CustomListAdapter(TakePayment.this, R.layout.custom_rows, (ArrayList<JSONObject>) notificationsresponse);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();