如何从列表视图prepopoulated数据库显示多列? [英] How to display multiple columns from a prepopoulated database in list view?

查看:139
本文介绍了如何从列表视图prepopoulated数据库显示多列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在我使用的是pre填充的数据库我的主要活动。我想显示在列表视图中的行。有五列在我的数据库了,而我想显示除ID的所有列。

This is my main activity in which I am using a pre-populated database. I am trying to display the rows in list view. There are five columns in my database out of which I want to display all columns except id.

public class MainActivity extends Activity implements OnClickListener{

private IngHelper dbIngHelper = null;
private Cursor curname=null;
private SimpleCursorAdapter adapter = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    //try{


    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ListView myList; 
    myList = (ListView)findViewById(R.id.listView1);

    dbIngHelper = new IngHelper(this);
    dbIngHelper.createDB();
    dbIngHelper.openDB();


    fillData();

//}赶上(例外五){

// }catch(Exception e){

//      Log.e("Error", "Error in code: "+ e.toString());

// e.printStackTrace();

// e.printStackTrace();

//  }
}


public void fillData(){

    //ListView myList; 
    ListView myList; 
    myList = (ListView)findViewById(R.id.listView1);
    curname=dbIngHelper.getCursor();
    startManagingCursor(curname);
    String[] columns = new String[] {
            IngHelper.COLUMN_ID,
            IngHelper.COLUMN_NAME,
            IngHelper.COLUMN_NATURE,
            IngHelper.COLUMN_SECTION,
            IngHelper.COLUMN_PENALTY
      };

    int[] to = new int[] { 
            R.id.id,
            R.id.name,
            R.id.nature,
            R.id.penalty,
            R.id.section
          };

    adapter = new SimpleCursorAdapter(
            this, R.layout.activity_main, 
            curname, 
            columns, 
            to);



    myList.setAdapter(adapter);



myList.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> listView, View view,int position, long   id) {
   //Get the cursor, positioned to the corresponding row in the result set
   // Cursor cursor = (Cursor) listView.getItemAtPosition(position);


   }
});
}

@覆盖     公共布尔onCreateOptionsMenu(功能菜单){         。getMenuInflater()膨胀(R.menu.activity_main,菜单);         返回true;     }

@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; }

class IngAdapter extends CursorAdapter{
    @SuppressWarnings("deprecation")
    IngAdapter(Cursor c){
        super(MainActivity.this,c);
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        // TODO Auto-generated method stub

        IngHolder holder=(IngHolder)row.getTag();
        holder.populateFrom(c,dbIngHelper);


    }

    @Override
    public View newView(Context cxtx, Cursor c, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater inf = getLayoutInflater();
        View row=inf.inflate(R.layout.row, parent, false);
        IngHolder holder = new IngHolder(row);
        row.setTag(holder);
        return(row);
    }

}

static class IngHolder{
    private TextView name=null;
   //   private TextView nature=null;
    //private TextView section=null;
    //private TextView penalty=null;

    IngHolder(View row){

        name=(TextView)row.findViewById(R.id.name);

    }

    void populateFrom(Cursor c,IngHelper r){

        name.setText(r.getName(c));


    }


}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

}

这是数据库类IngHelper.java

This is database class IngHelper.java

  public class IngHelper extends SQLiteOpenHelper{


private static final String DATABASE_PATH =        "//data//data//com.example.db//databases//";
private static final String DATABASE_NAME = "traffic.sqlite";
private static final int SCHEMA_VERSION = 1;
public static final String TABLE_NAME="mumbai";
public static final String COLUMN_ID="_id";
public static final String COLUMN_NAME="name";
public static final String COLUMN_NATURE="nature";
public static final String COLUMN_SECTION="section";
public static final String COLUMN_PENALTY="penalty";


public SQLiteDatabase dbSqlite;

private final Context myCnt;

public IngHelper (Context context)
{
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
    this.myCnt=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}

public void createDB(){

    boolean dbExist = DBExist();

    if(!dbExist){

        this.getReadableDatabase();

        copyDBFromResource();
    }
}

private boolean DBExist(){
    SQLiteDatabase db = null;

    try{
        String dbPath = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(dbPath, null,     SQLiteDatabase.OPEN_READWRITE);
        db.setLocale(Locale.getDefault());
        db.setLockingEnabled(true);
        db.setVersion(1);
    }catch(SQLiteException e){
        Log.e("SqlHelper", "db nt found");
        }

    return db != null ? true : false;
}

private void copyDBFromResource(){
    InputStream iS =null;
    OutputStream oS = null;
    String dbFilePath = DATABASE_PATH + DATABASE_NAME;

    try{
         iS = myCnt.getAssets().open(DATABASE_NAME);

         oS = new FileOutputStream(dbFilePath);

         byte[] buffer = new byte[1024];
         int length;
         while((length=iS.read(buffer))>0){
             oS.write(buffer, 0, length);
         }

         oS.flush();
         oS.close();
         iS.close();
    }catch(IOException e){
        throw new Error("Prob copyin db from resource file.");
    }

}

public void openDB()throws SQLException {
    String myPath = DATABASE_PATH + DATABASE_NAME;
    dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}

@Override
public synchronized void close() {
    // TODO Auto-generated method stub
    if(dbSqlite != null)
    {
        dbSqlite.close();
    }
    super.close();
}


public Cursor getCursor(){
    SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
    qBuilder.setTables(TABLE_NAME);
    String[] asColmToRetrn = new String[] {COLUMN_ID, COLUMN_NAME, COLUMN_NATURE, COLUMN_SECTION, COLUMN_PENALTY};

    Cursor mCur = qBuilder.query(dbSqlite, asColmToRetrn, null, null, null, null, null);

    if (mCur != null) {
       mCur.moveToFirst();
      }
      return mCur;

     }



public String getName(Cursor c){
    return(c.getString(1));


}

}

有两个XML文件。这是activity_main.xml ......它有一个列表视图。

There are two xml files. This is activity_main.xml... It has a list View.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >



<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" >

</ListView>

</RelativeLayout>

另一个布局row.xml文件。在这里,我创建一个文本视图在我的数据库中的每个列。

Another layout is row.xml file. Here I am creating a text View for each column in my database.

<?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" >

<TextView
    android:id="@+id/id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="TextView" />

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="TextView" />

<TextView
    android:id="@+id/nature"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="TextView" />

<TextView
    android:id="@+id/section"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:text="TextView" />

<TextView
    android:id="@+id/penalty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#000000"

    android:text="TextView" />

</LinearLayout>

我得到一个空白列表,与每个排​​列表条目输出,但数据不能被看见。请给一些建议来解决问题。

I am getting a blank list as output with a list entry for each row but the data cannot be seen. please give some suggestions to resolve the problem

推荐答案

如果你想显示所有的列值,则必须创建自定义适配器。请参见这里和的 Vogella教程

If you want to show all the column value, you must create custom adapter. See here and Vogella tutorial.

这篇关于如何从列表视图prepopoulated数据库显示多列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆