Je rencontre actuellement un problème de génération de vues de liste en fonction des données de ma base de données. J'ai essayé de créer une classe d'entité pour stocker la variable 2 (ID & DESCR) récupérée de la base de données et stockée dans une liste de tableau. Ensuite, j'ai créé une autre liste de chaînes pour stocker la variable (DESCR) que je veux afficher et la mettre dans la liste. PS: J'ai inséré un journal pour tester dans populateList() pour vérifier si j'ai exécuté la méthode populateList() ou non et il n'est pas apparu dans mon chat de journal.Génération de vues de liste à partir de données récupérées à partir de la base de données
Voici mon code:
package com.example.businesscalculatorassignment;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
public class HistoryActivity extends Activity implements OnItemClickListener {
private TextView tvHis;
private TableRow row1;
private SQLiteAdapter mySQLiteAdapter;
private ListView lv;
DatabaseHelper myDbHelper = new DatabaseHelper(this);
private SQLiteDatabase sqLiteDatabase;
private ListAdapter HTListAdapter;
private ArrayList<HistoryTrans> HTArrayList = new ArrayList<HistoryTrans>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// db testing
lv = new ListView(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
} catch (SQLException sqle) {
throw sqle;
}
lv.setOnItemClickListener(this);
HTListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_2, populateList());
lv.setAdapter(HTListAdapter);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.addView(lv);
setContentView(ll);
}
@SuppressWarnings("deprecation")
public List<String> populateList() {
mySQLiteAdapter.openToRead();
String test = "testing";
Log.d("test", test);
List<String> descList = new ArrayList<String>();
String ID = new String();
String DESCR = new String();
Log.d("ID", ID);
Log.d("DESCR", DESCR);
String[] columns = new String[] { ID, DESCR };
Cursor cursor = sqLiteDatabase.query(SQLiteAdapter.MYDATABASE_TABLE,
columns, null, null, null, null, null);
while (cursor.moveToNext()) {
ID = cursor.getString(cursor.getColumnIndex(ID));
DESCR = cursor.getString(cursor.getColumnIndex(DESCR));
HistoryTrans HT = new HistoryTrans();
HT.setID(ID);
HT.setDESCR(DESCR);
HTArrayList.add(HT);
descList.add(DESCR);
Log.d("ID", ID);
Log.d("DESCR", DESCR);
}
mySQLiteAdapter.close();
return descList;
}
@Override
protected void onResume() {
super.onResume();
HTListAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, populateList());
lv.setAdapter(HTListAdapter);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "Clicked on :" + arg2, Toast.LENGTH_SHORT).show();
// We want to redirect to another Activity when the user click an item on the ListView
Intent HistoryResultIntent = new Intent(this, HIstoryResult.class);
// We have to identify what object, does the user clicked, because we are going to pass only clicked object details to the next activity
// What we are going to do is, get the ID of the clicked item and get the values from the ArrayList which has
//same array id.
HistoryTrans clickedObject = HTArrayList.get(arg2);
// We have to bundle the data, which we want to pass to the other activity from this activity
String IDbuffer = new String();
IDbuffer = clickedObject.getID();
// Attach the bundled data to the intent
HistoryResultIntent.putExtra("ID",IDbuffer);
// Start the Activity
startActivity(HistoryResultIntent);
}
}
Voici ma classe d'entités juste au cas où nécessaire
package com.example.businesscalculatorassignment;
import java.io.Serializable;
import java.util.ArrayList;
public class HistoryTrans implements Serializable{
private String ID;
private String DESCR;
private ArrayList<String> IDList = new ArrayList<String>();
private ArrayList<String> DescrList = new ArrayList<String>();
public HistoryTrans(String iD, String dESCR) {
super();
ID = iD;
DESCR = dESCR;
}
public HistoryTrans() {
}
public String getDESCR() {
return DESCR;
}
public void setDESCR(String dESCR) {
DESCR = dESCR;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
}
Connexion rapport de chat
04-02 21:56:08.400: E/AndroidRuntime(5125): in writeCrashedAppName, pkgName :com.example.businesscalculatorassignment
04-02 21:56:08.400: D/AndroidRuntime(5125): file written successfully with content: com.example.businesscalculatorassignment StringBuffer : ;com.example.businesscalculatorassignment
04-02 21:56:08.400: E/AndroidRuntime(5125): FATAL EXCEPTION: main
04-02 21:56:08.400: E/AndroidRuntime(5125): Process: com.example.businesscalculatorassignment, PID: 5125
04-02 21:56:08.400: E/AndroidRuntime(5125): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.businesscalculatorassignment/com.example.businesscalculatorassignment.HistoryActivity}: java.lang.NullPointerException
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.os.Handler.dispatchMessage(Handler.java:102)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.os.Looper.loop(Looper.java:136)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.app.ActivityThread.main(ActivityThread.java:5021)
04-02 21:56:08.400: E/AndroidRuntime(5125): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 21:56:08.400: E/AndroidRuntime(5125): at java.lang.reflect.Method.invoke(Method.java:515)
04-02 21:56:08.400: E/AndroidRuntime(5125): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
04-02 21:56:08.400: E/AndroidRuntime(5125): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
04-02 21:56:08.400: E/AndroidRuntime(5125): at dalvik.system.NativeStart.main(Native Method)
04-02 21:56:08.400: E/AndroidRuntime(5125): Caused by: java.lang.NullPointerException
04-02 21:56:08.400: E/AndroidRuntime(5125): at com.example.businesscalculatorassignment.HistoryActivity.populateList(HistoryActivity.java:71)
04-02 21:56:08.400: E/AndroidRuntime(5125): at com.example.businesscalculatorassignment.HistoryActivity.onCreate(HistoryActivity.java:59)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.app.Activity.performCreate(Activity.java:5231)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
04-02 21:56:08.400: E/AndroidRuntime(5125): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-02 21:56:08.400: E/AndroidRuntime(5125): ... 11 more
Merci à l'avance pour aide à condition!
Avez-vous l'impression 'IDbuffer = clickedObject.getID();'? Voici l'ID? –
@NewDeveloper J'ai juste essayé de le voir à travers le journal et il ne l'a pas montré. Je doute que cela montrera quelque chose aussi parce que l'interface n'est même pas générée quand j'essaye d'exécuter cette page ainsi il est impossible que je fasse n'importe quel clic et déclenche l'auditeur onitemclick. – JamesYTL
Je pense que vous n'avez pas créé d'objet pour 'mySQLiteAdapter'. Peut être que ce sera le problème. De votre journal, l'erreur se produit à la ligne 72. Veuillez vérifier que ... –