1

I use custom Adapter and AsyncTask to read data and put it into listview. but it does not work.

Thread t;
int res;
ArrayList<mLink> values;
AdapterLink adapter;
ListView lv;
String title;
InputStream is;
private ReadXML readXML;
TextView status;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    status = (TextView)findViewById(R.id.textView1);
    lv = (ListView)findViewById(R.id.lView);
    values = new ArrayList<mLink>();       
    readXML = new ReadXML(getApplicationContext()); 
    load(); 
}

This is AsyncTask, but I think it not right?

private class DownloadWebpageText extends AsyncTask<String,integer,InputStream> {
    protected InputStream doInBackground(String... urls) {

        return connectT3H(urls[0]);

    }
    // onPostExecute displays the results of the AsyncTask.
    protected void onPostExecute(InputStream result) {
       values = readXML.read(result);
       status.setText("Connected");
        Log.d("Http code", "adapter " + adapter.getCount());
   }
}

This fun load and connect

public void load(){
    String stringUrl = "http://vnexpress.net/rss/gl/trang-chu.rss";
    ConnectivityManager connMgr = (ConnectivityManager) 
        getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        new DownloadWebpageText().execute(stringUrl);
    } else {
        status.setText("No network connection available.");
    }
    adapter = new AdapterLink(getApplicationContext(), R.layout.item_layout, values);
    lv.setAdapter(adapter);
}


public InputStream connectT3H(String mURL){

    int responseCode = 0;

    InputStream in = null;
    try {
        URL url = new URL(mURL);

        URLConnection connection = url.openConnection();

        HttpURLConnection httpURLConnection = (HttpURLConnection)connection;

        responseCode = httpURLConnection.getResponseCode();

        if (responseCode == HttpURLConnection.HTTP_OK){
            in = httpURLConnection.getInputStream();
        }   
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }   
    return in;
}   

This Code is not error but it not display data into list view

4

1 に答える 1

0
new DownloadWebpageText().execute(stringUrl) 

これがあなたの問題です。この関数doInBackgroundは入力ストリームを返します。この入力ストリームを変数に保存し、アダプターに渡します

于 2012-10-12T04:44:51.680 に答える