-1

i have made this xml layout file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/gray"
    android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_margin="10dp"
    android:background="@color/black"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/s_hour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/gray"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/e_hour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/gray"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</RelativeLayout>

</LinearLayout>

and i have this line of codes:

class WorkingView extends LinearLayout
{

public WorkingView(Context context,String str) {
    super(context);
    // TODO Auto-generated constructor stub
    setContentView(R.layout.work_data);

    Text s_hour = (Text) findViewById(R.id.s_hour);
    s_hour.setData(str);
    }

}

and this line from another main class which extends activity:

private void readData() {
    WorkingView wv = new WorkingView(this,"hello");
    list.addView(wv);
 }

it all works fine, until the code calls this readData() method.

i don't understand what is wrong?

Daniel Mendel
  • 506
  • 5
  • 17
  • Please give more detailed information about what the problem is. Is your code crashing, is it not doing what you expect? How is the variable list defined? Is it a ListView? If you are trying to populate a list, you can't just add views, the best way is to use an adapter, have a look here: https://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews – Micky Sep 27 '12 at 08:39
  • please tell us which exception you are getting while running this code.? – Hemant Sep 27 '12 at 09:00
  • Nevermind i got the error, he wasn't catching the text, so it returned null pointer exception – Daniel Mendel Sep 27 '12 at 09:06

1 Answers1

1

For what you are doing you shouldn't use a list.

If you simply want add one view under another: Have a linearLayout as the "container", and then add them to that.

Lists are used if you have a bunch of data which all needs to be lined up under eachother with one or more properties. And for that you need to look into adapters, arrayadapter is the more simple one, and cursoradapter a bit more advanced.

Also, your method will probably crash since you don't inflate your xml. For inflation of views look here

For scrolling capabilities look into ScrollView and here is a guide to use it

Community
  • 1
  • 1
Anders Metnik
  • 6,096
  • 7
  • 40
  • 79
  • i do want to add one view under the other but, i also want to able to scroll down if start to get too much of them, and what do u mean by inflate my xml? – Daniel Mendel Sep 27 '12 at 08:49