2012-11-12 6 views
0

웹 페이지에서 데이터를 listview으로 설정하는 코드를 작성했습니다. 웹 페이지를 성공적으로 읽고 필요한 데이터를 String 배열에 저장했습니다. 이제 ArrayAdapter을 사용하여 ListView에 할당하려고하지만 데이터가 listview으로 설정되지 않습니다. 저 오류 해결 도와주세요 : 데이터가 String array목록보기에서 데이터가 설정되지 않음

11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterflies 
11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfly 
11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfies 
11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfly 
11-07 23:03:47.640: I/NEW VAUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterflies 
11-07 23:03:47.640: I/VALUES(3192): Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterflies<br/>Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfly<br/>Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfies<br/>Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterfly<br/>Happiness is Like a Butterfly, You run after it, It keeps flying away. But if you stand still, It comes and Sits On You Wish you lots of Butterflies 

코드에 저장되는 확인하기 위해 로그 캣의

출력을

private Spinner spinner1; 
private ProgressDialog pd; 
private StringBuilder response; 
private ListView listView; 
private String[] values = new String[0]; 
private ArrayAdapter<String> adapter; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    listView = (ListView) findViewById(R.id.mylist); 
    spinner1 = (Spinner) findViewById(R.id.spinnerCategory); 
    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 

     public void onItemSelected(AdapterView<?> av, View view, int id, 
       long ids) { 
      new sendMessageAsync().execute(); 
     } 

     public void onNothingSelected(AdapterView<?> av) { 
     } 
    }); 

    adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, android.R.id.text1, values); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

private class sendMessageAsync extends AsyncTask<Void, Void, String> { 

    @Override 
    protected void onPreExecute() { 
     pd = ProgressDialog.show(main.this, null, "Loading...", 
       true, true); 
    } 

    @Override 
    protected void onCancelled() { 
     Toast.makeText(getApplicationContext(), 
       "Message Sending Cancelled", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    protected String doInBackground(Void... arg0) { 
     try { 
      doInBg(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     pd.dismiss(); 
     adapter.notifyDataSetChanged(); 
     listView.setAdapter(adapter); 
    } 
} 

public void doInBg() { 
    try { 
     final String msgURL = "http://example.com/messages.php?category=" + String.valueOf(spinner1.getSelectedItem().toString().replace(" ", "%20")); 
     URLConnection connection = new URL(msgURL).openConnection(); 
     connection.setRequestProperty("Accept-Charset", "UTF-8"); 
     InputStream responseStream = connection.getInputStream(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(responseStream)); 
     response = new StringBuilder(); 
     String line; 
     while ((line = br.readLine()) != null) { 
      response.append(line); 
     } 
     values = String.valueOf(response).split("<br/>"); 
     for (int i = 0; i < values.length;i++) { 
      Log.i("NEW VAUES", values[i]); 
     } 
     Log.i("VALUES", String.valueOf(response)); 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

답변

2

당신은 어레이를 변경하지 않는 어댑터 저장 수행 할 작업 :

values = String.valueOf(response).split("<br/>"); 

새 클래스를 클래스 멤버에 저장하고 있지만 어댑터에 여전히 이전 배열이 저장되어 있습니다. 새 어댑터를 작성하고 설정할 수 있지만이 솔루션은 성능에 좋지 않습니다. 대신이 새 배열에서 각 항목을 어댑터에 추가해야합니다. 이 시도 :

private class sendMessageAsync extends AsyncTask<Void, Void, String> { 

    @Override 
    protected void onPreExecute() { 
     pd = ProgressDialog.show(main.this, null, "Loading...", 
       true, true); 
    } 

    @Override 
    protected void onCancelled() { 
     Toast.makeText(getApplicationContext(), 
       "Message Sending Cancelled", Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    protected String doInBackground(Void... arg0) { 
     try { 
      return doInBg(); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     pd.dismiss(); 
     if (result == null) { 
      // request failed! 
      // return; 
     } 
     values = String.valueOf(result).split("<br/>"); 
     adapter.clear(); 
     for (String str : values) 
      adapter.add(str); 
    } 
} 

public String doInBg() { 
    try { 
     final String msgURL = "http://example.com/messages.php?category=" + String.valueOf(spinner1.getSelectedItem().toString().replace(" ", "%20")); 
     URLConnection connection = new URL(msgURL).openConnection(); 
     connection.setRequestProperty("Accept-Charset", "UTF-8"); 
     InputStream responseStream = connection.getInputStream(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(responseStream)); 
     response = new StringBuilder(); 
     String line; 
     while ((line = br.readLine()) != null) { 
      response.append(line); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    return response; 
} 
+0

난 당신의 대답에 따라 난 내 제안이 어댑터를 변경 되었기 때문입니다이 오류'java.lang.UnsupportedOperationException' –

+0

좀 더 설명해주십시오 ... 당신이 말하고자하는 것을 얻을하지 않았다 배경 스레드 및 가능한 것은 아닙니다. 내 새 코드를 확인하십시오. –

+0

을 얻고로 변경 한 후 –

0

이 시도 :

@Override 
protected void onPostExecute(String result) { 
    pd.dismiss(); 
    adapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_list_item_1, android.R.id.text1, values); 
    listView.setAdapter(adapter); 
} 

어댑터 값을 코드에서 업데이트되지 않습니다.

+0

플라 비오의 대답이 더 좋습니다. –

관련 문제