【问题标题】:android: update ListView with SimpleAdapterandroid: 使用 SimpleAdapter 更新 ListView
【发布时间】:2012-11-28 09:47:18
【问题描述】:

我有一个带有 SimpleAdapter 的 ListView。 ListView 的数据来自 json。我想每 5 分钟更新一次 ListView。 这很好用……但 ListView 总是一个 double。所有项目都在 ListView 中。为什么?然后我更新了 3 次.... 我试试

setListAdapter(null);

mylist.clear();

没有影响

public class StartActivity extends ListActivity {
    TextView text_1,text_2 ;
    private Timer autoUpdate;
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    Button btnShowLocation;

    // GPSTracker class
    GpsData gps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);

        btnShowLocation = (Button) findViewById(R.id.report_btn);

        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(StartActivity.this, ReportActivity.class);
                startActivity(intent);

            }
        });

        new task().execute();
    }

     @Override
     public void onResume() {
      super.onResume();
      autoUpdate = new Timer();
      autoUpdate.schedule(new TimerTask() {
       @Override
       public void run() {
        runOnUiThread(new Runnable() {
         public void run() {
             new task().execute();
         }
        });
       }
      }, 0, 300000); // updates each 5 min
     }

class task extends AsyncTask<String, String, Void>
{
private ProgressDialog progressDialog = new ProgressDialog(StartActivity.this);
    InputStream is = null ;
    String result = "";
    protected void onPreExecute() {
       progressDialog.setMessage("Status Update...");
       progressDialog.show();
       progressDialog.setOnCancelListener(new OnCancelListener() {
    @Override
        public void onCancel(DialogInterface arg0) {
        task.this.cancel(true);
       }
    });
     }
       @Override
    protected Void doInBackground(String... params) {
           //mylist.clear();


           JSONObject json = jsonFunctions.getJSONfromURL("http://my.de", mlat, mlon);

      try{
        //String text = getString(R.string.report_TJ);
        JSONArray  earthquakes = json.getJSONArray("uTraf");

            for(int i=0;i<earthquakes.length();i++){                        
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = earthquakes.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("first", "Stau: " + e.getString("road") + ", " + e.getString("county"));
                map.put("second", e.getString("timestamp") + ", " + e.getString("suburb"));
                mylist.add(map);            
            }       
      }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
      }

            return null;

        }
    protected void onPostExecute(Void v) {
        //setListAdapter(null);
        ListAdapter adapter = new SimpleAdapter(StartActivity.this, mylist , R.layout.main, 
                new String[] { "first", "second" }, 
                new int[] { R.id.item_title, R.id.item_subtitle });

setListAdapter(adapter);

final ListView lv = getListView();
lv.setTextFilterEnabled(true);  
lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        @SuppressWarnings("unchecked")
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
        Toast.makeText(StartActivity.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

    }
});
            this.progressDialog.dismiss();

        } 
    }


@Override
public void onPause() {
 autoUpdate.cancel();
 super.onPause();
}


}

【问题讨论】:

    标签: android listview simpleadapter


    【解决方案1】:

    您的问题是在 doInBackground 的 for 循环中调用 mylist.add(map);

    您创建地图很好,但通过调用 mylist 上的 add 函数,您将其附加到列表中,而不是使用相同的键覆盖当前地图。如果您想更新列表视图中的现有项目,而不是仅仅用新数据覆盖,那么mylist 变量可能也应该是HashMap

    编辑 - 解决方案:

    在doInBackground中,就在你进入循环处理数据之前

    (for(int i=0;i<earthquakes.length();i++))
    

    致电mylist.clear()。这将在您开始向其中添加新数据之前清空您的数组列表。

    【讨论】:

    • 我明白了!我该如何解决?
    • 在 doInBackground 中,就在您进入循环处理数据之前 (for(int i=0;i
    【解决方案2】:

    doInBackGround()方法中添加mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();

    try{
        //String text = getString(R.string.report_TJ);
        JSONArray  earthquakes = json.getJSONArray("uTraf");
             mylist = new ArrayList<HashMap<String, String>>();
    
            for(int i=0;i<earthquakes.length();i++){                        
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = earthquakes.getJSONObject(i);
    
                map.put("id",  String.valueOf(i));
                map.put("first", "Stau: " + e.getString("road") + ", " + e.getString("county"));
                map.put("second", e.getString("timestamp") + ", " + e.getString("suburb"));
                mylist.add(map);            
            }       
      }catch(JSONException e)        {
         Log.e("log_tag", "Error parsing data "+e.toString());
      }
    

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      相关资源
      最近更新 更多