【问题标题】:How to bind data in ListView after getting responses in android?在android中获得响应后如何在ListView中绑定数据?
【发布时间】: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;
}



}

【问题讨论】:

    标签: android listview bind


    【解决方案1】:

    首先创建一个你已经完成的适配器:

    list = (ListView) findViewById(R.id.take_payment_list);
    adapter = new CustomListAdapter(this, notifications);
    list.setAdapter(adapter);
    

    但在第二行列表项丢失,其余都很好:

    adapter = new CustomListAdapter(this,<<listItem>>, notifications);
    

    现在收到回复后

    将您的项目添加到通知数组,然后更改适配器以拥有新的通知数组:

    notifications.add("data");
    adapter = new CustomListAdapter(this,<<listItem>>, notifications);
    

    【讨论】:

    • > 究竟是什么意思 Rajesh?
    • 在你的情况下应该是 R.layout.custom_rows 。
    • 这对拉杰什没有帮助。包含 `adapter = new CustomListAdapter(this,R.layout.custom_rows, notifications);` 时显示错误
    • ok 将构造函数更改为将第二个参数作为整数。 public CustomListAdapter(Activity activity, int textViewResourseId ,List 通知) { }
    • 我现在明白了,拉杰什...!我确实喜欢这样: adapter = new CustomListAdapter(MainActivity.this, R.layout.custom_rows, (ArrayList) notificationsresponse); list.setAdapter(适配器);适配器.notifyDataSetChanged();它完成了......!干杯...!
    【解决方案2】:

    做简单的步骤:

    在你的适配器中覆盖方法:

    public void notifyDataSetChanged(List<Users> notifications){
       this.notifications = notifications;
       super.notifyDataSetChanged();
    }
    

    现在,而不是调用:

    adapter.notifyDataSetChanged();
    

    称之为:

    adapter.notifyDataSetChanged(notificationsresponse);
    

    另外,您从服务器获取数据的方法也不正确。当您尝试在主 UI 线程上获取数据时,这会降低用户体验。您应该只在异步线程中调用服务器/数据库调用,并在 UI 线程上更新该响应。

    【讨论】:

      【解决方案3】:

      首先,永远不要在主线程上调用 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) {}
      }
      

      【讨论】:

        【解决方案4】:

        如果您希望再次使用获取的数据,则首先将其存储在 Sqlite 数据库中,如果没有,则使用 ArrayAdapter 将数据绑定到 listView。

        参考this链接。

        【讨论】:

          【解决方案5】:

          在你们的帮助下成功找到了它。得到响应后只需添加这 3 行代码。对于我的应用程序,它是这样的:

          adapter = new CustomListAdapter(TakePayment.this, R.layout.custom_rows, (ArrayList<JSONObject>) notificationsresponse);
          list.setAdapter(adapter);
          adapter.notifyDataSetChanged();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-02
            • 2011-10-24
            • 1970-01-01
            • 2017-09-18
            • 1970-01-01
            • 2020-04-01
            相关资源
            最近更新 更多