在列表中的单独列中显示数据

时间:2011-12-05 12:38:43

标签: android string list

  

可能重复:
  nullPointerException in multi column list

我有以下应用程序从URL获取数据并显示它。

问题是我在列表中得到包含各行的输出。

现在我想将行数据拆分为4列,如下所示。

SBIN ;1911.05;1886.85;1.28应在1行内显示为4列(以';'分隔),而不是单个元素。请参阅此屏幕截图以供参考:

Screenshot of the app

ReadWebPageAsyncTask.java

public class ReadWebpageAsyncTask extends Activity {
    private EditText ed;
    private ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ed = (EditText) findViewById(R.id.ed);
        lv = (ListView) findViewById(R.id.list);
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://abc.com/default.aspx?id=G" });
    }

    private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
            int sub = result.lastIndexOf('@', result.length() - 1);
            String s1 = result.substring(0, sub);
            String s = s1.replace(";", " - ");
            String arr[] = s.split("@");    
            lv.setAdapter(new ArrayAdapter<String>(ReadWebpageAsyncTask.this,
                    android.R.layout.simple_list_item_1, arr));
        }
    }
}

请检查此链接

nullPointerException in multi column list


这种方式使用多列列表非常繁忙。相反,我尝试this并且它有效..

1 个答案:

答案 0 :(得分:1)

使用android表格布局

Reference