2011-08-27 69 views
0

後沒有更新我有兩個列表視圖,單擊第一個列表視圖中的項目加載第二個列表視圖,與項目的一個新的列表。列表視圖(這取決於用戶在第一個列表中點擊)選擇不同的列表項

基本上,第二列表視圖保持加載同一列表每次,即使用戶選擇第一列表上的不同的項目。看起來像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(); 
      } 

UPDATE:懂了工作,我創建了一個新的方法,稱爲公共無效refreshNewList(),並把下面的代碼在它:

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

請把rem OVE多餘的線條和提高代碼的縮進,這是很難讀它這樣 – MByD

回答

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); 
+0

我做listview2.setAdapter(適配器2),因爲我有適配器2 INIT作爲一個全球性,仍然沒有運氣 – EI756

+0

適配器2 =新ArrayAdapter(這一點,android.R.layout .simple_list_item_1,this.fetchNewList());它的意思是fetchNewList undefined – EI756

+0

現在整條線以紅色下劃線,並且構造函數ArrayAdapter未定義的錯誤 – EI756

相關問題