选择不同的列表项后,Listview不会更新

时间:2011-08-27 10:33:06

标签: android listview

我有两个列表视图,单击第一个列表视图中的项目会加载第二个列表视图,并带有一个新的项目列表(取决于用户在第一个列表中单击的内容)。

基本上,即使用户在第一个列表中选择不同的项目,第二个列表视图也会每次都加载相同的列表。似乎onItemClick没有被调用或类似的东西。

以下是代码:

        listview.setOnItemClickListener(new OnItemClickListener(){

            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                Object item = listview.getItemAtPosition(position);

                //Log.v(LOG_TAG,"selected item: " + item);
                SavePreferences("item",item.toString());
    fetchNewList();
                 flipper.showNext();
            }});

        listview2.setOnItemClickListener(new OnItemClickListener(){
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                 flipper.showPrevious();
            }});

 @SuppressWarnings("null")
    public ArrayList<String> fetch()
    {
        ArrayList<String> listItems = new ArrayList<String>();
        try {
            URL twitter = new URL(
                    "JSON.php");
            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
          //make sure youe String line is completely filled after that..
          if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
           {
          JSONArray jArray = new JSONArray(line);
          Log.v(LOG_TAG,"jarray value: " + jArray);
          for (int i = 0;i < jArray.length(); i++) 
           {
             //SONObject jobj = jArray.getJSONObject(i);
              String country = jArray.getString(i);
              listItems.add(country); 
             // also make sure you get the value from the jsonObject using some key
             // like, jobj.getString("country");
            //istItems.add(jobj.getString(""));
           }
              }
            } 
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listItems;
    }
    @SuppressWarnings("null")
    public ArrayList<String> fetchNewList()
    {
        ArrayList<String> listItems = new ArrayList<String>();
        try {
            SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
            String selectedcounty = sharedPreferences.getString("item", "");
            Log.v(LOG_TAG,"ITEM**:" + selecteditem);
            URL twitter = new URL(
                    "JSON2.php?item=" + selecteditem);
            URLConnection tc = twitter.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    tc.getInputStream()));

        String line;
            while ((line = in.readLine()) != null) {
          //make sure youe String line is completely filled after that..
          if (!line.equals(null) && !line.equals("") && line.startsWith("[")) 
           {

          JSONArray jArray = new JSONArray(line);
         // Log.v(LOG_TAG,"jarray value: " + jArray);
          for (int i = 0;i < jArray.length(); i++) 
           {
             //SONObject jobj = jArray.getJSONObject(i);
              String country = jArray.getString(i);
              listItems.add(country); 
             // also make sure you get the value from the jsonObject using some key
             // like, jobj.getString("country");
            //istItems.add(jobj.getString(""));
           }
              }
            } 
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return listItems;
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
       private void SavePreferences(String key, String value){
            SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(key, value);
            editor.commit();
           }

更新:搞定了,我创建了一个名为public void refreshNewList()的新方法,并将以下代码放入其中:

adapter2 = null;
        adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetchNewList());
        listview2.setAdapter(adapter2);

1 个答案:

答案 0 :(得分:1)

        listview.setOnItemClickListener(new OnItemClickListener(){

        public void onItemClick(AdapterView<?> a, View v, int position, long id) {
            Object item = listview.getItemAtPosition(position);

            //Log.v(LOG_TAG,"selected item: " + item);
            SavePreferences("item",item.toString());
            listview2.setAdapter(new YourAdapter(context, resource, fetchNewList()));

            flipper.showNext();
        }});

编辑:试试这个

adapter2 = null; 
adapter2 = new YourAdapter(paramters); 
listview2.setdapter(adapter2);