从数据库以编程方式填充的活动中的多个列表视图

时间:2014-11-03 05:10:54

标签: android android-listview

我以编程方式创建了listviews,并将它们添加到水平scrollview中作为图像中的shon enter image description here

和我的活动

    package com.example.dynamiclistviewtest;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.ScrollView;

public class MainActivity extends ActionBarActivity {
    //LinearLayout llListView;
    LinearLayout llMain;
    ListView[] listView;
    private HorizontalScrollView scrollListView;
    private int counter = 0; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollListView = (HorizontalScrollView)findViewById(R.id.scrollListView);
        //llListView = (LinearLayout) findViewById(R.id.llListView);
        llMain = (LinearLayout)findViewById(R.id.llMain);
        listView = new ListView[5];

        //Onclick listener
        Button.OnClickListener btnpayClick = new Button.OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                Button button = (Button) v;
                button.setText("Button "+counter);
            }

        };

        LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ArrayList<ArrayList<HashMap<String, String>>> arr = new ArrayList<ArrayList<HashMap<String, String>>>();

        for (int pages = 0; pages <10 ; pages++) {
            LinearLayout llViewHolder = new LinearLayout(this);
            llViewHolder.setOrientation(LinearLayout.VERTICAL);
            llViewHolder.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f));
            for (int page_row = 0; page_row < 2; page_row++) {
                LinearLayout row = new LinearLayout(this);
                row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1.0f));
                llViewHolder.addView(row);
                for (int sale = 0; sale< 3; sale++) {
                    View item = inflator.inflate(R.layout.view_inflate, null);
                    ListView listofItems = (ListView) item.findViewById(R.id.listView1);
                    Button btnpay = (Button)item.findViewById(R.id.btnpay);
                    btnpay.setOnClickListener(btnpayClick);
                    btnpay.setText("pay "+sale);
                    LayoutParams lpll = new LinearLayout.LayoutParams(300,300,1.0f);
                    lpll.setMargins(30, 10, 10, 10);
                    listofItems.setLayoutParams(lpll);
                    String[] data = new String[]{"Ashok","Shaishavi","jatin","Akansha","Aditi","Bhavesh","Rajesh","Roshni","Heema"};
                    ArrayAdapter<String> arrAdapater = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
                    listofItems.setAdapter(arrAdapater);

                    /*listView[i] = new ListView(this);
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(200,100);
                    lp.setMargins(10, 10, 10, 10);
                    listView[i].setLayoutParams(lp);
                    listView[i].setBackgroundColor(getResources().getColor(R.color.red));
                    llListView.addView(listView[i]);*/

                    row.addView(item);
                }

                //listView[3].setVisibility(View.INVISIBLE);
            }
            llMain.addView(llViewHolder);
        }



    }




}

和xml布局文件activity_main,其中包含水平滚动视图和线性布局,用于保存此布局文件的所有视图和代码,如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:id="@+id/scrollListView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/llMain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="1" >
        </LinearLayout>
    </HorizontalScrollView>
    <!--<LinearLayout
        android:id="@+id/llListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>-->

</LinearLayout>

并查看我从view_inflate.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="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="540dp"
        android:layout_weight="1"
        android:background="@color/paybtn_lgreen" >
    </ListView>

    <Button
        android:id="@+id/btnpay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Payment" />

</LinearLayout>

现在我想填充我从本地数据库sqlite添加的所有列表视图,我该如何实现?

0 个答案:

没有答案