0

Possible Duplicate:
nullPointerException in multi column list

I have following app which fetches data from URL and displays it.

The problem is I get the output in list which consists of various rows.

Now I want to split the row data into 4 columns as below.

SBIN ;1911.05;1886.85;1.28 should be displayed as 4 columns(seperated by ';') within 1 row and not as a single element. See this screenshot for reference:

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));
        }
    }
}

PLEASE CHECK THIS LINK

nullPointerException in multi column list


It is very hectic using multi column list this way. Instead I tried this and it worked..

Community
  • 1
  • 1
GAMA
  • 5,958
  • 14
  • 79
  • 126
  • Can it be done using tableView? – GAMA Dec 05 '11 at 12:40
  • Have you tried multi column listview? – user370305 Dec 05 '11 at 12:47
  • You can Creat customView For each row declare 4 textview in customview and set your data in that textViews – bindal Dec 05 '11 at 12:54
  • @ user370305 - Can u give me the brief idea about how it can be done? I don't want to process each element of a row separately. I just want to display them in appropriate format. – GAMA Dec 06 '11 at 04:29
  • @bindal - you are talking about table layout, right? But how do I insert the data in each of the 4 columns of each table row? – GAMA Dec 06 '11 at 04:31

1 Answers1

1

Use android table layout

Reference

MGK
  • 7,050
  • 6
  • 34
  • 41
  • But then inserting the data in each of the 4 columns of each table row will be a headache I guess... – GAMA Dec 06 '11 at 04:37
  • It is very hectic using multi column list this way. Instead I tried http://stackoverflow.com/questions/8411101/nullpointerexception-in-multi-column-list-using-hashmap and it worked.. The problem using current approach was value of variable position gets incremented by just 1 each time the getView() method is called whereas in my case I have 4 columns, so ideally it should be incremented ny 4 and not 1. Even if I try to increment the position by 4, it skips some intermediate data. So the better approach is to use hashmap. – GAMA Dec 07 '11 at 13:03