【问题标题】:Listview not updating after selecting different list item选择不同的列表项后列表视图不更新
【发布时间】:2011-08-27 10:33:06
【问题描述】:

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

基本上,第二个列表视图每次都会加载相同的列表,即使用户在第一个列表中选择不同的项目。似乎 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);

【问题讨论】:

  • 请去掉多余的行并改进代码缩进,这样读起来很难

标签: android listview


【解决方案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);

【讨论】:

  • 我做了 listview2.setAdapter(adapter2) 因为我有adapter2 init 作为一个全局的,仍然没有运气
  • adapter2 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,this.fetchNewList());它说 fetchNewList 未定义
  • 现在整行都加了红色下划线,错误是 Constructor ArrayAdapter is undefined
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多