开发者

ListView layout problem

开发者 https://www.devze.com 2023-02-22 11:51 出处:网络
I am trying to preview an exercise log where it shows all the exercises that the user perform in a day along with the duration and the burned calories, at the end there will be the total calories burn

I am trying to preview an exercise log where it shows all the exercises that the user perform in a day along with the duration and the burned calories, at the end there will be the total calories burned in that day.

when I first tried to do this, I build a layout with 10 rows each has 3 text views that represent the exercise name, duration and the burned calories. but it was impractical, since the user entries will differ from day to day. So I came up with this solution which I am not sure if it is some thing I can do in android or not.

please take a look, and tell me what's wrong excuse me for the long introduction

the layout:

<?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="fill_parent"
    android:orientation="vertical">
    <TableLayout android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:stretchColumns="0">
        <TableRow android:layout_width="wrap_content"
            an开发者_运维百科droid:layout_height="wrap_content">
            <TextView android:text="Activity" 
                android:layout_height="wrap_content" 
                android:layout_width="wrap_content">
            </TextView>
            <TextView android:layout_width="wrap_content" 
                android:text="Duration" 
                android:layout_height="wrap_content">
            </TextView>
            <TextView android:layout_width="wrap_content" 
                android:text="Burned" 
                android:layout_height="wrap_content">
            </TextView>
        </TableRow>
        <TableRow android:layout_height="wrap_content"
            android:layout_width="fill_parent">
        <ListView android:id="@+id/lstAct" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content">
        </ListView>
        <ListView android:id="@+id/lstDur" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content">
        </ListView>
        <ListView android:id="@+id/lstbrn" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content">
        </ListView>         
        </TableRow>
            <TableRow android:layout_width="wrap_content"
            android:layout_height="wrap_content">           
            <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Total Burned Calories">
        </TextView>
        <TextView android:id="@+id/Totbrn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" Kcal">
        </TextView>
    </TableRow> 
        <Button android:id="@+id/addExcBtn" 
         android:layout_width="wrap_content" 
         android:text="Add another exercise" 
         android:layout_height="wrap_content">
        </Button>           
    </TableLayout>
</LinearLayout>

This is the class code:

public class ExerciseDiary extends Activity implements OnClickListener {
private DataBaseHelper helper;
private ListView l1;
private ListView l2;
private ListView l3;
static String[] activity;
static Integer[] duration;
static Integer[] burned;

@Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.test);
      helper = new DataBaseHelper(this);      
      l1 = (ListView) findViewById(R.id.lstAct);
      l2 = (ListView) findViewById(R.id.lstDur);
      l3 = (ListView) findViewById(R.id.lstbrn);
      fillArray();
      l1.setAdapter(new ArrayAdapter<String>(this, R.layout.items,activity));
      l2.setAdapter(new ArrayAdapter<Integer>(this, R.layout.items,duration));
      l3.setAdapter(new ArrayAdapter<Integer>(this, R.layout.items,burned));
          View AddButton = findViewById(R.id.addExcBtn);
      AddButton.setOnClickListener(this);                 
   }
public void onClick(View v){
       switch (v.getId()) {     
            case R.id.addExcBtn:
                startActivity(new Intent(this, Exercise.class));
            break;                              
       }
   }
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK){
        startActivity(new Intent(this, Activity_DietTracker.class));
        return true;
    }
    return false;
}   
public void fillArray(){
    GregorianCalendar toDay = new GregorianCalendar();
    String today = AllCmnMtd.getDate(toDay);
    activity = new String[100];
    duration = new Integer[100];
    burned = new Integer[100];
    int i = 0;
      helper.openDataBase();          
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor c = db.rawQuery("Select * from ExerciseLog where EDate = '" + today + "'", null);
    if(c!=null){
        if(c.moveToFirst()){
            do{
                activity[i] = c.getString(c.getColumnIndex("Exercise"));
                duration[i] = c.getInt(c.getColumnIndex("Duration"));
                burned[i] = c.getInt(c.getColumnIndex("Burned"));
                i++;
            }while(c.moveToNext());
        }
    }
    helper.close();
}
}

and these are the errors:

java.lang.NullPointerException
at android.widget.ArrayAdapter.createViewFromResource (ArrayAdapter.java:351)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
at android.widget.AbsListView.obtainView(AbsListView.java:1251)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1117)
at android.widget.ListView.onMeasure(ListView.java:1030)
at android.view.View.measure(View.java:7115)
at android.widget.TableRow.measureChildBeforeLayout(TableRow.java:221)
at android.widget.LinearLayout.measureHorizontal (LinearLayout.java:619)
at android.widget.TableRow.onMeasure(TableRow.java:112)
at android.view.View.measure(View.java:7115)  

and about 25 others.

if anyone has any information about that or if there is another way to do it,please let me know.

Thanks


The XML is just a part of the problem. In main class you have to declare an instance of the view as member data: private ListView mConversationView;

Then in your onCreate you have to initialize the pointer. mConversationView = (ListView) findViewById(R.id.in);

If you don't to the latter, you will get the null pointers your seeing.

0

精彩评论

暂无评论...
验证码 换一张
取 消