数据未在listview上设置

时间:2012-11-12 16:29:08

标签: android arrays string listview

我已经编写了一些代码来将数据从网页设置为listview。我已成功阅读网页,并在String数组中存储了必要的数据。现在我想使用ListView将其分配给ArrayAdapter,但数据未在listview中设置。请帮我解决错误:

输出logcat以验证数据是否存储在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

CODE

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 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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);
}

适配器值未在代码中更新。