Comment faire pour afficher plusieurs colonnes à partir d'une prepopoulated base de données dans la vue liste?

C'est mon activité principale dans laquelle je suis à l'aide d'un pré-rempli de la base de données. J'essaie d'afficher les lignes dans la vue liste. Il y a cinq colonnes dans ma base de données à partir de laquelle je veux afficher toutes les colonnes à l'exception de l'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) {
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();
}
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);
}
});
}
@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
}
}

C'est la classe de base de données 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));
}
}

Il y a deux fichiers xml. C'est activity_main.xml... Il a une Vue de liste.

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

Un autre modèle est row.xml fichier. Ici, je suis entrain de créer un texte à Afficher pour chaque colonne dans ma base de données.

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

J'obtiens une liste vide en sortie avec une entrée de la liste pour chaque ligne, mais les données ne peuvent pas être vus. veuillez donner quelques suggestions pour résoudre le problème

InformationsquelleAutor Carbon | 2013-02-21