3

I already tried references from similar question on SO, but hasn't got the appropriate solution.

I'm trying to fetch the data from a webpage and display it in format consisting of rows having 4 columns.

Data present on webpage:

SBIN ;1916.00;1886.85;1.54@LT ;1315.50;1310.30;0.40@TCS ;1180.00;1178.00;0.17@AXISBANK ;1031.30;1005.95;2.52@MARUTI ;1000.35;992.35;0.81@PNB ;931.90;916.35;1.70@GAIL ;400.00;398.45;0.39@

I want to diaplay it in the form

SBIN.........1916.00.....1886.85.....1.54

LT...........1315.50.....1310.30.....0.40  and so on. 

Note that I don't want dots, I want each value to be a separate column within a row.

My Data consists of 7 rows.

When I run the below code, I get this output Screenshot i.e.

 values[0]   values[1]   values[2]   values[3]

 values[1]   values[2]   values[3]   values[4]

 values[2]   values[3]   values[4]   values[5]

(It prints all 4 cols of 1st row, then col 2-4 of 1st row and col1 of 2nd row, then cols 3-4 of 1st row and col 1-2 of 2nd row and so on...)

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 + 2);
            String temp[];
            String subarr[] = new String[100];
            ;
            Log.v("data = ", s1);
            // String s = s1.replace(";", " - ");
            final String arr[] = s1.split("@");

            for (int i = 0; i < arr.length; i++) {
                Log.v("arr" + i, arr[i] + "    " + arr.length);
            }

            for (int i = 0; i < arr.length - 1; i++)

            {
                temp = arr[i].split(";");
                subarr[(4 * i)] = temp[0];
                subarr[(4 * i) + 1] = temp[1];
                subarr[(4 * i) + 2] = temp[2];
                subarr[(4 * i) + 3] = temp[3];
                        }
        lv.setAdapter(new MyAdapter(ReadWebpageAsyncTask.this, subarr));
        }
    }
}

main.xml

            <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <EditText android:id="@+id/ed" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:hint="Search">
    </EditText>

    <ListView android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    </ListView>

    </LinearLayout>

MyAdapter.java

   public class MyAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] values;

    private String item1, item2, item3, item0;
    int x = 0, i = 1, y = 1;

    public MyAdapter(Context context, String[] values) {
        super(context, R.layout.row, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public String getItem(int position) {
        return values[position];
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row, parent, false);
        TextView tv1 = (TextView) rowView.findViewById(R.id.col1);
        TextView tv2 = (TextView) rowView.findViewById(R.id.col2);
        TextView tv3 = (TextView) rowView.findViewById(R.id.col3);
        TextView tv4 = (TextView) rowView.findViewById(R.id.col4);

        if (y < 8) {
            item0 = getItem(position);
            Log.v("pos = ", "" + position);
            item1 = getItem(position + 1);
            item2 = getItem(position + 2);
            item3 = getItem(position + 3);

            tv1.setText(item0);
            tv2.setText(item1);
            tv3.setText(item2);
            tv4.setText(item3);

        } else {
            Log.v("y= ", "" + y);
        }
        return rowView;
    }
}

row.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

             <TextView android:id="@+id/col1"
             android:layout_width="150dip"
             android:layout_height="wrap_content"/>

             <TextView android:id="@+id/col2"
             android:layout_width="70dip"
             android:layout_height="wrap_content"/>

              <TextView android:id="@+id/col3"
             android:layout_width="70dip"
             android:layout_height="wrap_content"/>

              <TextView android:id="@+id/col4"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>

    </LinearLayout>

ANY HELP APPRICIATED

GAMA
  • 5,958
  • 14
  • 79
  • 126

3 Answers3

2

Try using ListView

Instead of TableLayout add ListView to your xml and place the content on table in new xml Create an adapter by extending ArrayAdapter and set this adapter on your listView.

Its not blank
  • 3,055
  • 22
  • 37
  • Please check http://stackoverflow.com/questions/8385428/display-data-in-seperate-columns-in-list where I already done this by using listview. But display is not in proper format. – GAMA Dec 06 '11 at 05:53
  • you can always create a custom xml accoring to your need.Thats what i meant. I mean create a Layout with 4 textview each being seperated as per your requirement.coz using tableView programatically can be quite hectic. – Its not blank Dec 06 '11 at 06:04
  • remove t1 = (TextView) findViewById(R.id.col1); t2 = (TextView) findViewById(R.id.col2); t3 = (TextView) findViewById(R.id.col3); t4 = (TextView) findViewById(R.id.col4); from main activity. You need to create a customAdapter, see [link] (http://www.vogella.de/articles/AndroidListView/article.html)this – Its not blank Dec 06 '11 at 07:45
  • I've modified acc. to ur suggestions, but it only displays last row data in all 7 rows. – GAMA Dec 06 '11 at 09:23
  • use @Override public String getItem(int position) { return values[position]; } and add this to getView temp=getItem(position); and remove the for loop – Its not blank Dec 06 '11 at 09:44
  • But I have 7 rows having 4 columns each & I have an array consisting of 28 elements. How do I fetch data into 4 textviews. – GAMA Dec 06 '11 at 12:04
  • getView is called recursively.read the link.It will clear many of your problems – Its not blank Dec 06 '11 at 12:13
  • @PareshMayani I refered to this link and modified the code according to my requirements. But It's giving me nullpointerexception. – GAMA Dec 07 '11 at 05:49
  • It is very hectic using multi column list this way. Instead I tried [this] and it worked. [this]:http://stackoverflow.com/questions/8411101/nullpointerexception-in-multi-column-list-using-hashmap – GAMA Dec 07 '11 at 10:16
0

Don't add the views in your Java code, as they are added based on your XML file. It looks to me like your Java code is duplicating what you did in XML which is very precarious...

I don't see why you can't just remove the Java code and use setContentView to use the XML layout you defined.

skynet
  • 9,898
  • 5
  • 43
  • 52
  • I don't want just one row. I want multiple rows to be created with 4 column each with data added to each col at run time. – GAMA Dec 06 '11 at 05:42
  • Ah... I see. So you are running that code in a loop? Could you post more code showing how you are doing this? – skynet Dec 06 '11 at 05:46
  • code updated. For the moment I have not used loop as I want to insert data from a webpage as shown in my earlier question http://stackoverflow.com/questions/8385428/display-data-in-seperate-columns-in-list – GAMA Dec 06 '11 at 05:54
0

Set the width to 0, and add weight as 1 for all the testviews, it will evenly assign space for every item in the row.

    textView1.setWidth(0);
    textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
Mal
  • 373
  • 4
  • 15