dongsuishou8039 2015-11-03 14:18
浏览 71
已采纳

如何在不复制数据的情况下自动刷新(添加/更新)listview项目?

i made a simple code to retrieve some data from mysql database and put them into android listview everything works fine but the old items stays on the list view and it adds the new items(data is duplicated), below is php code :

$sql = "SELECT * FROM persons";

$res = mysqli_query($con,$sql);

$result = array();

while($row = mysqli_fetch_array($res)){
array_push($result,
array('id'=>$row[0],
'name'=>$row[1],
'address'=>$row[2]
 ));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);

and here the android code :

    public class FetchData extends Activity{
    String myJSON;

    private static final String TAG_RESULTS="result";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_ADD ="address";

    JSONArray peoples = null;

    ArrayList<HashMap<String, String>> personList;

    ListView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        list = (ListView) findViewById(R.id.listView);
        personList = new ArrayList<HashMap<String,String>>();
        getData();
        final Handler handler = new Handler();
        handler.postDelayed( new Runnable() {

            @Override
            public void run() {


                refreshData();
                handler.postDelayed( this, 2 * 100 );
            }
        }, 2 * 100 );

    }
    public void refreshData(){

    }
    public void getData(){
        class GetDataJSON extends AsyncTask<String, Void, String>{

            @Override
            protected String doInBackground(String... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
                HttpPost httppost = new HttpPost("http://192.168.1.4/get-data.php");

                // Depends on your web service
                httppost.setHeader("Content-type", "application/json");

                InputStream inputStream = null;
                String result = null;
                try {
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();

                    inputStream = entity.getContent();
                    // json is UTF-8 by default
                    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();

                    String line = null;
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "
");
                    }
                    result = sb.toString();
                } catch (Exception e) {
                    // Oops
                }
                finally {
                    try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
                }
                return result;
            }

            @Override
            protected void onPostExecute(String result){
                myJSON=result;

                showdata();
            }
        }
        GetDataJSON g = new GetDataJSON();
        g.execute();
    }
    protected void showdata(){
        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            peoples = jsonObj.getJSONArray(TAG_RESULTS);

            for(int i=0;i<peoples.length();i++){
                JSONObject c = peoples.getJSONObject(i);
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String address = c.getString(TAG_ADD);

                HashMap<String,String> persons = new HashMap<String,String>();

                persons.put(TAG_ID,id);
                persons.put(TAG_NAME,name);
                persons.put(TAG_ADD,address);

                personList.add(persons);
            }

            ListAdapter adapter = new SimpleAdapter(
                    FetchData.this, personList, R.layout.list_item,
                    new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                    new int[]{R.id.id, R.id.name, R.id.address}
            );

            list.setAdapter(adapter);

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

    }


    }

i would like to update automatically specific listview item if mysql database data changed for that specific item and add new items without duplicating

  • 写回答

2条回答 默认 最新

  • duangou2046 2015-11-03 14:44
    关注

    Try to modify your showdata() like this:

        protected void showdata(){
            try {
                JSONObject jsonObj = new JSONObject(myJSON);
                peoples = jsonObj.getJSONArray(TAG_RESULTS);
    
                personList.clear();
                for(int i=0;i<peoples.length();i++){
                    JSONObject c = peoples.getJSONObject(i);
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String address = c.getString(TAG_ADD);
    
                    HashMap<String,String> persons = new HashMap<String,String>();
    
                    persons.put(TAG_ID,id);
                    persons.put(TAG_NAME,name);
                    persons.put(TAG_ADD,address);
    
                    personList.add(persons);
                }
    
                ListAdapter adapter = new SimpleAdapter(
                        FetchData.this, personList, R.layout.list_item,
                        new String[]{TAG_ID,TAG_NAME,TAG_ADD},
                        new int[]{R.id.id, R.id.name, R.id.address}
                );
    
                list.setAdapter(adapter);
    
            } catch (JSONException e) {
                e.printStackTrace();
            }
    
        }
    

    personList.clear(); - for cleaning old result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题