scrollview Android中的动态布局

时间:2015-11-23 13:54:58

标签: android android-layout

我想制作一个可以有4到10个线性水平布局的布局 每行都有一个Spinner和3个图像按钮 如果我有一个旋转器的结构,3个按钮会很好 微调器的数据来自数据库

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="@android:color/transparent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/linearDataFeed"></LinearLayout>
</ScrollView>
package com.example.ajd19.dynamiclayout;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;

import java.util.ArrayList;

/**
 * Created by ajd19 on 24-11-2015.
 */
public class DataFeed extends ActionBarActivity {
    int nosubs;
    int day,month,year;

    class rows{
        public Spinner s;
        public ImageButton b1;
        public ImageButton b2;
        public ImageButton b3;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_feed);
        try {
            LinearLayout linear = (LinearLayout) findViewById(R.id.linearDataFeed);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            Intent i = getIntent();
            int n = i.getIntExtra("number", 0);

            rows[] r = new rows[n];
            ArrayList<String> list = new ArrayList<String>();

            SQLiteDatabase DB = openOrCreateDatabase("DB", MODE_PRIVATE, null);
            Cursor cursor = DB.rawQuery("Select * from Subjects", null);
            cursor.moveToFirst();
            for (int k = 0; k < cursor.getCount(); k++) {
                cursor.moveToPosition(k);
                list.add(cursor.getString(1));
            }

            for (int j = 0; j < n; j++) {
                LinearLayout l = new LinearLayout(this);
                l.setOrientation(LinearLayout.HORIZONTAL);

                r[j].s = new Spinner(this);
                ArrayAdapter<String> SpinnerArrayAdapter = new ArrayAdapter<String>(this, R.layout.data_feed, list);
                r[j].s.setAdapter(SpinnerArrayAdapter);
                l.addView(r[j].s);

                r[j].b1 = new ImageButton(this);
                r[j].b1.setImageResource(R.mipmap.ic_tick);
                r[j].b1.setLayoutParams(params);
                l.addView(r[j].b1);

                r[j].b2 = new ImageButton(this);
                r[j].b2.setImageResource(R.mipmap.ic_circle);
                r[j].b2.setLayoutParams(params);
                l.addView(r[j].b2);

                r[j].b3 = new ImageButton(this);
                r[j].b3.setImageResource(R.mipmap.ic_cross);
                r[j].b3.setLayoutParams(params);
                l.addView(r[j].b3);

                linear.addView(l);
            }
        }catch (Exception e){
            Log.e("ohhh",e.toString() );
        }
    }
}

screenshot of the activity obtained

https://www.dropbox.com/sh/4r9zh27ki4t04nb/AACRpZQuK3Ep0kS1TlxtOgnya?dl=0

通过运行此代码,我得到一个空指针异常
java.lang.NullPointerException: Attempt to write to field 'android.widget.Spinner com.example.ajd19.dynamiclayout.DataFeed$rows.s' on a null object reference

包含截图和源代码请检查:)

0 个答案:

没有答案