【问题标题】:Same data repeated in list view在列表视图中重复相同的数据
【发布时间】:2015-10-06 11:42:26
【问题描述】:

我是安卓新手。我正在开发一个应用程序,在该应用程序中我从服务器获取一些 JSONArray 格式的数据。我想在名为 HistoryActivity 的活动上显示登录时间注销时间和日期以及另一个活动的纬度和经度,即列表视图中的 MapActivity。我有一个很大的 JSONArray。这里的问题是我在列表视图中得到相同的日期。数组的长度是正确的,只有数据是重复的。我怎样才能做到这一点?我已经尝试了很多。这是我的代码。

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);
    toolbar = (Toolbar) findViewById(R.id.app_bar);
    toolbar.setTitle("History");
    clicklat=new ArrayList<String>();
    clicklong=new ArrayList<String>();
    dttime=new ArrayList<String>();
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    list = (ListView) findViewById(R.id.historyList);
    history = new ArrayList<Pojo>();
    new NetCheck().execute();
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    ArrayList<String>clicklat= new ArrayList<String>(history.get(position).getLati());

    ArrayList<String>clicklong= new ArrayList<String>(history.get(position).getLongi());

    ArrayList<String>dttime= new ArrayList<String>(history.get(position).getDatetime());

    Intent i = new Intent(HistoryActivity.this, DetailsActivity.class);
    i.putStringArrayListExtra("clicklat", clicklat);
    i.putStringArrayListExtra("clicklong", clicklong);
    i.putStringArrayListExtra("clickdatetime", dttime);
    startActivity(i);
        }
    });

}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}


private class NetCheck extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        nDialog = new ProgressDialog(HistoryActivity.this);
        nDialog.setMessage("Loading..");
        nDialog.setTitle("Please Wait");
        nDialog.setIndeterminate(false);
        nDialog.setCancelable(false);
        nDialog.show();
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        nDialog.dismiss();
        // TODO Auto-generated method stub

        myAdapter = new HistoryAdapter(HistoryActivity.this, history);
        list.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        try {

            HttpClient httpClient = new DefaultHttpClient();

            HttpPost httpRequest = new HttpPost(

                    "http://techie-web.com/demo/app/japp/getpoint");

            httpRequest.setHeader("Content-Type", "application/json");
            SharedPreferences mmm = getSharedPreferences(
                    "MyPref", MODE_PRIVATE);

            String logempid = mmm.getString("id", null);

            JSONObject json = new JSONObject();

            json.put("empid", logempid);

            Log.e("JSON Object", json.toString());

            StringEntity se = new StringEntity(json.toString());

            se.setContentEncoding("UTF-8");
            se.setContentType("application/json");

            httpRequest.setEntity(se);
            HttpResponse httpRes = httpClient.execute(httpRequest);

            java.io.InputStream inputStream = httpRes.getEntity()
                    .getContent();
            InputStreamReader inputStreamReader = new InputStreamReader(
                    inputStream);

            BufferedReader reader = new BufferedReader(inputStreamReader);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            inputStream.close();
            strServerResponse = sb.toString();
            Log.e("Server Response", "" + strServerResponse.toString());

            if (strServerResponse != null) {
                try {

                    JSONArray arr = new JSONArray(strServerResponse);

                    for (int k = 0; k < arr.length(); k++) {

                        JSONObject jsonObj1 = arr.getJSONObject(k);
                        Pojo pojo = new Pojo();
                        JSONArray subArrayLat = jsonObj1.getJSONArray("lati_long");
                        List<String> lati= new ArrayList<String>();
                        List<String> longi= new ArrayList<String>();
                        List<String> dateandtime= new ArrayList<String>();

                        for (int i = 0; i < subArrayLat.length(); i++) {
                            String lat = subArrayLat.getJSONObject(i).getString("Latitude").toString();
                            String loong = subArrayLat.getJSONObject(i).getString("Longitude").toString();
                            String datetimee = subArrayLat.getJSONObject(i).getString("date_time").toString();
                            lati.add(lat);
                            longi.add(loong);
                            dateandtime.add(datetimee);

                        }

                        pojo.setLati(lati);//adding latitude list
                        pojo.setLongi(longi); //adding longitude list
                        pojo.setDatetime(dateandtime);

                        String dateee = arr.getJSONObject(k).getString("login_date");
                        String timeeee = arr.getJSONObject(k).getString("login_time");
                        String timeeee2 = arr.getJSONObject(k).getString("logout_time");
                        pojo.setDate(dateee);
                        pojo.setLoginTime(timeeee);
                        pojo.setLogoutTime(timeeee2);

                        history.add(pojo);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

这是我的适配器。

 public class HistoryAdapter extends BaseAdapter {
private Context activity;
TextView tv_date;
TextView tv_loginTime;
TextView tv_logoutTime;
ArrayList<Pojo> list;
private ArrayList<Pojo> arraylist = null;
public static LayoutInflater inflater;

public HistoryAdapter(Context a, ArrayList<Pojo> history) {
    // TODO Auto-generated constructor stub
    activity = a;
    list = history;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.arraylist = new ArrayList<Pojo>();
    this.arraylist.addAll(list);

}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return list.get(position);
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = convertView;
    if (convertView == null) {
        v = inflater.inflate(R.layout.history_item, parent, false);
    }

    final Pojo pojo = list.get(position);
    tv_date = (TextView) v.findViewById(R.id.historyDate);
    tv_loginTime = (TextView) v.findViewById(R.id.historyLoginTime);
    tv_logoutTime = (TextView) v.findViewById(R.id.historyLogoutTime);
    tv_date.setText(pojo.getDate());
    tv_loginTime.setText(pojo.getLoginTime());
    tv_logoutTime.setText(pojo.getLogoutTime());
    return v;

}
}

这是我的json

  [{"login_time":"10:30:28","logout_time":"10:31:47","login_date":"2015-09-30","lati_long":[{"date_time":"2015:09:30 11:15:15","Latitude":"21.121776","Longitude":"79.047563"},{"date_time":"2015:09:30 11:15:52","Latitude":"21.121776","Longitude":"79.047563"},{"date_time":"2015:09:30 11:17:16","Latitude":"21.121776","Longitude":"79.047563"}]},{"login_time":"10:42:56","logout_time":"10:44:41","login_date":"2015-09-30","lati_long":[{"date_time":"2015:09:30 11:14:53","Latitude":"21.121776","Longitude":"79.047563"},{"date_time":"2015:09:30 11:15:01","Latitude":"21.121776","Longitude":"79.047563"},{"date_time":"2015:09:30 11:15:15","Latitude":"21.121776","Longitude":"79.047563"}]},{"login_time":"10:45:29","logout_time":"10:45:36","login_date":"2015-09-30","lati_long":[{"date_time":"2015:09:30 11:14:53","Latitude":"21.121776","Longitude":"79.047563"},{"date_time":"2015:09:30 11:15:01","Latitude":"21.121776","Longitude":"79.047563"}]}]

这里有什么问题?请帮我。

【问题讨论】:

  • 通话记录 = new ArrayList();在 history.add(pojo); 之前
  • @Sree 在列表视图中只显示一项
  • @Sree 清除历史只会删除数据。我认为您的意思是在 AsyncTask 开始时对其进行更新?无论如何,这不会改变任何事情,因为它们是按顺序在 onCreate 中完成的。 @Pri,删除 getItemId 我不认为它做你认为它做的事情,你永远不会回收视图,因为你告诉适配器每个都是独一无二的。
  • 是的,所以你想调用 history = new ArrayList();在for循环开始之前
  • 异步任务只运行一次,所以不会改变任何东西。你可以张贴截图@Pri 吗?

标签: android json listview baseadapter getter-setter


【解决方案1】:

使用ListView时始终使用ViewHolder。请参阅this 文章。使用 ViewHolder 重写您的代码并检查。

【讨论】:

  • 我也尝试过使用 ViewHolder。还是不行
【解决方案2】:

请像这样更新您的 getView(int position, View convertView, ViewGroup parent) 方法

public View getView(int position, View convertView, ViewGroup parent) {

if (convertView == null) {
    LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    convertView = mInflater.inflate(R.layout.mytask_list_item, parent, false);
    tv_date = (TextView) v.findViewById(R.id.historyDate);
    convertView.setTag(R.id.historyDate, tv_date);

} else {
    tv_date = (TextView) convertView.getTag(R.id.histroyDate);
}
tv_date.setText("date");
}

对其他 textViews 做同样的事情

【讨论】:

  • 这里的mContext是什么?
  • 它是 Context 类型变量,在您的情况下它是 .. Context 活动;
  • 它应该可以工作..我认为您应该调试代码以了解您在列表 ArrayList list; 中获得的数据
  • 您不应在 getView() 方法中将 pojo 变量声明为 final 。请从那里删除final。你可以这样写 list.get(position).getDate()
  • 当你将一个变量标记为final时,这意味着你不能为该变量分配不同的对象引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多