douhu2370 2014-12-14 17:51
浏览 56
已采纳

android每次输入新的pid(id)然后刷新listview。 我怎么做的?

my code . please help me . i'm beginner . php json andriod Mainactivity java please help me . when data change listview update automaticly and real time show my android app please help me

  package com.example.aaa111;




    public class MainActivity extends ListActivity {

        private ProgressDialog pDialog;

        // URL to get contacts JSON
        private static String url = "http://test.pokupat.net/get_all_products.php";

        // JSON Node names




        private static final String TAG_PRODUCTS = "products";
        private static final String TAG_PID = "pid";
        private static final String TAG_NAME = "name";

        // contacts JSONArray
        JSONArray contacts = null;

        // Hashmap for ListView
        ArrayList<HashMap<String, String>> contactList;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            contactList = new ArrayList<HashMap<String, String>>();

            ListView lv = getListView();
    // refresh function if this true ..
             Handler handler = new Handler();

                handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                //do anything
                    new GetContacts().execute();

                }
                }, 10000);

            // Listview on item click listener
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // getting values from selected ListItem
                    String name = ((TextView) view.findViewById(R.id.name))
                            .getText().toString();
                    String pid = ((TextView) view.findViewById(R.id.pid))
                            .getText().toString();


                    // Starting single contact activity
                    Intent in = new Intent(getApplicationContext(),
                            SingleContactActivity.class);
                    in.putExtra(TAG_NAME, name);
                    in.putExtra(TAG_PID, pid);

                    startActivity(in);

                }
            });

            // Calling async task to get json uyffuyfuyfuy fyuf yfuy fuf uyfuy
            new GetContacts().execute();
        }

        /**
         * Async task class to get json by making HTTP call
         * */
        private class GetContacts extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setMessage("Please wait...");
                pDialog.setCancelable(false);
                pDialog.show();

            }

            @Override
            protected Void doInBackground(Void... arg0) {
                // Creating service handler class instance
                ServiceHandler sh = new ServiceHandler();

                // Making a request to url and getting response
                String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

                Log.d("Response: ", "> " + jsonStr);

                if (jsonStr != null) {
                    try {
                        JSONObject jsonObj = new JSONObject(jsonStr);

                        // Getting JSON Array node
                        contacts = jsonObj.getJSONArray(TAG_PRODUCTS);

                        // looping through All Contacts
                        for (int i = 0; i < contacts.length(); i++) {
                            JSONObject c = contacts.getJSONObject(i);

                            String name = c.getString(TAG_NAME);
                            String pid = c.getString(TAG_PID);



                                                    // tmp hashmap for single contact
                            HashMap<String, String> contact = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            contact.put(TAG_NAME, name);
                            contact.put(TAG_PID, pid);



                            // adding contact to contact list
                            contactList.add(contact);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Log.e("ServiceHandler", "Couldn't get any data from the url");
                }

                return null;
            }

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (pDialog.isShowing())
                    pDialog.dismiss();

                        ListAdapter adapter = new SimpleAdapter(
                                MainActivity.this, contactList,
                                R.layout.list_item, new String[] { TAG_NAME, TAG_PID
                                         }, new int[] { R.id.name,
                                        R.id.pid});

                        setListAdapter(adapter);



            }

        }

    }

refresh background please

  • 写回答

1条回答 默认 最新

  • duanjue6584 2014-12-14 17:59
    关注

    Below

    ListView lv = getListView();
    

    Add

    adapter = new SimpleAdapter(
                                MainActivity.this, contactList,
                                R.layout.list_item, new String[] { TAG_NAME, TAG_PID
                                         }, new int[] { R.id.name,
                                        R.id.pid});
    
                        setListAdapter(adapter);
    

    Declare your adapter as a private variable

    private SimpleAdapter adapter;
    

    Then in your onPostExecute method call

    adapter.notifyDataSetChanged();
    

    Rather than setting a new adapter each time with every call, you simply want to let the adapter know that new data has been added. This will then tell your list view to reevaluate itsself, and then you will see the new data appear.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥15 this signal is connected to multiple drivers怎么解决
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus