管理列表视图中的多行布局

时间:2014-04-12 15:52:08

标签: android android-listview android-sqlite

我已经建了三排布局。假设RowLayout1,RowLayout2,RowLayout3是三行布局。我想将RowLayout1添加为第一行,然后是RowLayout2,最后一行是rowLayout3。 RowLayout2的数据来自sqllite,其他数据来自值。

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Paasurams extends Activity {

    private Divyadesamdb dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pasuram);

        dbHelper = new Divyadesamdb(this);
        dbHelper.open();
        /*
         * Check if data already exist if total count is
         */
        int totalRows = dbHelper.getCountAllPasurams();
        Log.d("VC", "totalRows = " + totalRows);
        //dbHelper.deleteAllPasurams();
        //Log.d("VC", "totalRows after delete = " + totalRows);
        if (totalRows < 4535) {
            // Clean all data commented to improve performance
            dbHelper.deleteAllPasurams();
            // Add some data
            dbHelper.insertPasurams();
            totalRows = dbHelper.getCountAllPasurams();
            Log.d("VC","After insertPasurams rows found are "+totalRows);
        }

        Intent intent = getIntent();
        String mcategory = intent.getStringExtra(MainActivity.EXTRA_CATEGORY);
        String mstart = intent.getStringExtra(MainActivity.EXTRA_START);
        String mending = intent.getStringExtra(MainActivity.EXTRA_ENDING);
        String mpasuramNumber = intent
                .getStringExtra(MainActivity.EXTRA_PASURAMNUMBER);
        String maayiram = intent.getStringExtra(MainActivity.EXTRA_AAYIRAM);
        String mazhwaar = intent.getStringExtra(MainActivity.EXTRA_AZHWAAR);
        String mmangalasasanamOn = intent
                .getStringExtra(MainActivity.EXTRA_MANGALASASANAMON);
        String msubCategory = intent
                .getStringExtra(MainActivity.EXTRA_SUBCATEGORY);
        String mtitle = intent.getStringExtra(MainActivity.EXTRA_TITLE);

        // Generate ListView from SQLite Database

        displayListView(mpasuramNumber, maayiram, mazhwaar, mcategory,
                mmangalasasanamOn, msubCategory, mtitle, mstart, mending);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    private void displayListView(String mpasuramNumber, String maayiram,
            String mazhwaar, String mcategory, String mmangalasasanamOn,
            String msubCategory, String mtitle, String mstart, String mending) {
        String category = mcategory;
        String starting = mstart;
        String ending = mending;
        String pasuramNumber = mpasuramNumber;
        String aayiram = maayiram;
        String azhwaar = mazhwaar;
        String mangalasasanamOn = mmangalasasanamOn;
        String subCategory = msubCategory;
        String title = mtitle;
        int rowsFound = 0;

        Cursor cursor = dbHelper.fetchAllPasurams(pasuramNumber, aayiram,
                azhwaar, category, mangalasasanamOn, subCategory, title,
                starting, ending);
        /*
         * How many rows returned from the query
         */
        rowsFound = cursor.getCount();

        // the desired columns to be bound

        String[] columns = new String[] {
                Divyadesamdb.PASURAMS_COLUMN_PAASURAMNUMBER,
                Divyadesamdb.PASURAMS_COLUMN_AAYIRAM,
                Divyadesamdb.PASURAMS_COLUMN_AZHWAAR,
                Divyadesamdb.PASURAMS_COLUMN_CATEGORY,
                Divyadesamdb.PASURAMS_COLUMN_MANGALASASANAMON,
                Divyadesamdb.PASURAMS_COLUMN_PAASURAM_EN_STR,
                Divyadesamdb.PASURAMS_COLUMN_SUBCATEGORY,
                Divyadesamdb.PASURAMS_COLUMN_TITLE,
                Divyadesamdb.PASURAMS_COLUMN_TITLENUMBER,
                Divyadesamdb.PASURAMS_COLUMN_IMAGE_ID };

        // the XML defined views which the data will be bound to
        int[] to = new int[] { R.id.tvPaasuramNumber, R.id.tvAayiram,
                R.id.tvAzhwaar, R.id.tvCategory, R.id.tvMangalasasanamOn,
                R.id.tvPaasuram, R.id.tvSubCategory, R.id.tvTitle,
                R.id.tvtvTitleNumber, R.id.ivAzhwaar };
        // create the adapter using the cursor pointing to the desired data
        // as well as the layout information
        MyCursorAdapter dataAdapter = new MyCursorAdapter(this,
                R.layout.paasuram_single_item, cursor, columns, to, 0);
        ListView listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(dataAdapter);
        Log.d("VC", "Found rows " + rowsFound);
    }

    // extend the SimpleCursorAdapter to create a custom class where we
    // can override the getView to change the row colors
    private class MyCursorAdapter extends SimpleCursorAdapter {
        Context mContext;

        public MyCursorAdapter(Context context, int layout, Cursor c,
                String[] from, int[] to, int flags) {
            super(context, layout, c, from, to, flags);
            mContext = context;
        }

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

            // get reference to the row
            View view = super.getView(position, convertView, parent);
            // check for odd or even to set alternate colors to the row
            // background

            // want to get the value of this text view,
            // but not getting handle.

            if (position % 2 == 0) {
                view.setBackgroundColor(Color.rgb(238, 233, 233));
            } else {
                view.setBackgroundColor(Color.rgb(255, 255, 255));
            }
            return view;
        }

    }

}

0 个答案:

没有答案