jeudi 2 avril 2015

Generating list views from data retrieved from database

I'm currently facing problem in generating list views based on my database's data. I have tried to create an entity class to store the 2 variable(ID & DESCR) retrieved from database and stored it in an array list. Then I created another String list to store the variable (DESCR) that I want to display and put it into the list view.


PS: I inserted a log to test in populateList() to check whether I did run the populateList() method or not and it didn't showed up in my log cat.


Here are my 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);

}
}


Here are my entity class just in case needed



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


}


Thanks in advance for helps provided!


Aucun commentaire:

Enregistrer un commentaire