Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在列表视图中重复相同的数据_Android_Json_Listview_Baseadapter_Getter Setter - Fatal编程技术网

Android 在列表视图中重复相同的数据

Android 在列表视图中重复相同的数据,android,json,listview,baseadapter,getter-setter,Android,Json,Listview,Baseadapter,Getter Setter,我是android新手。我正在开发一个应用程序,在这个应用程序中,我以JSONArray格式从服务器获取一些数据。我想在列表视图中显示活动Called HistoryActivity的登录时间、注销时间和日期,以及另一个活动(即MapActivity)的纬度和经度。我有一个很大的JSONArray。这里的问题是我在列表视图中得到了相同的日期。只有重复数据时,数组的长度才正确。我该怎么做?我试了很多。这是我的密码 @Override protected void onCreate(Bundle

我是android新手。我正在开发一个应用程序,在这个应用程序中,我以JSONArray格式从服务器获取一些数据。我想在列表视图中显示活动Called 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();
                }

这里怎么了?请帮帮我

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

请像这样更新getView(int位置、View convertView、ViewGroup父级)方法

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");
}

对其他文本视图执行相同操作

调用历史记录=新建ArrayList();在历史之前添加(pojo)@Sree这只显示列表中的一项view@Sree清除历史记录只会删除数据。我想你的意思是在任务开始时更新它?这不会改变任何事情,因为它们在onCreate中是按顺序完成的@Pri,删除
getItemId
我不认为它做了你认为它做的事情,你永远不会回收视图,因为你告诉适配器每个视图都是唯一的。是的,所以你想调用history=new ArrayList();在此之前,for循环启动异步任务只运行一次,因此不会更改任何内容。你能在@Pri发布一个截图吗?我也试过使用ViewHolder。仍然不工作这里的mContext是什么?在您的案例中它是上下文类型变量。。语境活动;它应该有用。。我认为您应该调试代码,以了解在列表ArrayList中获得的数据;不应将getView()方法中的pojo变量声明为final。请从那里删除最终版本。您可以这样编写list.get(position).getDate()当您将一个变量标记为final时,这意味着您不能为该变量分配不同的对象引用。
  [{"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"}]}]
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");
}