Android ArrayList HashMap does not show data - android

I need your help! 3 pages of purple links on Google and I'm stuck on such a simple task! What I'm trying to do is get an ArrayList of type HashMap that contains mutiple HashMaps and display them using a ListAdapter. I'm getting the information from an SQL DB, I know this is possible but I can't see what I'm doing wrong, the list just shows up blank.
public class History extends ListActivity {
ArrayList<HashMap<String, String>> inboxList;
HashMap<String, String> dealList;
ListView lv;
AddSQL entry;
Button bRefresh;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.history);
entry = new AddSQL(History.this);
inboxList = new ArrayList<HashMap<String, String>>();
ListAdapter adapter = new SimpleAdapter(History.this, inboxList, R.layout.history_list, new String[] { SEX, NAME, RATING, DATE },
new int[] { R.id.sex, R.id.name, R.id.rating, R.id.date });
inboxList = entry.getAllEntrys();
setListAdapter(adapter);
}
/** This is my SQL */
public ArrayList<HashMap<String, String>> getAllEntrys(){
Log.d("Select", "Get");
String s = "";
String n = "";
String r = "";
String d = "";
String select = "SELECT sex, name, rating, date FROM " + MAIN_TABLE;
ArrayList<HashMap<String, String>> inboxList = new ArrayList<HashMap<String, String>>();
Log.d("Select", "1");
try{
open();
Cursor mCursor = database.rawQuery(select, null);
while(mCursor.moveToNext()){
HashMap<String, String> dealList = new HashMap<String, String>();
s = mCursor.getString(mCursor.getColumnIndex(SEX));
n = mCursor.getString(mCursor.getColumnIndex(NAME));
r = mCursor.getString(mCursor.getColumnIndex(RATING));
d = mCursor.getString(mCursor.getColumnIndex(DATE));
dealList.put(SEX, s);
dealList.put(NAME, n);
dealList.put(RATING, r);
dealList.put(DATE, d);
inboxList.add(dealList);
}
return null;
}catch (Exception e){
Log.e("Sort By List", "ERROR: " + e);
e.printStackTrace();
}
return null;
}

Your getAllEntrys always returns null, should be inboxList in the try block.

Related

Adapter showing the last value in all tabs

I am using maps in my app. Everything is fine except adapter. I don't know why it is showing only the last element in all tabs.
Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String,String>> list = new ArrayList<Map<String,String >>();
Map<String,String> map = new HashMap<String, String>();
EmployeeDatabase empClick = new EmployeeDatabase(getApplicationContext());
Cursor cursor = empClick.getDetails();
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
map.put("name",name);
map.put("age",age);
list.add(map);
}while(cursor.moveToNext());
cursor.close();
}
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.show_data, new String [] {"name", "age"}, new int[] {R.id.name,R.id.age});
setListAdapter(adapter);
}
if(cursor.moveToFirst()){
do{
map = new HashMap<String, String>();<-------Add this line
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
map.put("name",name);
map.put("age",age);
list.add(map);
}while(cursor.moveToNext());
cursor.close();
}

Android : Put SQLite data in BaseAdapter

Currently im making a custom listview with baseadapter of(image, ressource and database). I got a problem on putting a data from sqlite inside baseadapter. my code is look like this, Database Code:
public HashMap<String, String> getResult(String year) {
HashMap<String, String> questionmap = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT _id, COLUMN_B, COLUMN_C FROM TABLE_NAME WHERE YEAR_COLUMN='"+ year + "'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
questionmap.put("_id", cursor.getString(0));
questionmap.put("COLUMN_B", cursor.getString(1));
questionmap.put("COLUMN_C", cursor.getString(2));
} while (cursor.moveToNext());
}
return questionmap;
}
And Here is my BaseAdapter looks like:
class singlerow_info {
String A, B, C;
int img;
singlerow_info(String A, String B, String C,
int img) {
this.A = A;
this.B = B;
this.C = C;
this.img = img;
}
}
class resultadapter extends BaseAdapter {
//Get YEAR values from previous activity
Intent intent = getIntent();
String year = intent.getStringExtra("YEAR");
HashMap<String, String> questionmap = myDb.getResult(year);
ArrayList<singlerow_info> list;
resultadapter(Context c)
{
list = new ArrayList<singlerow_info>();
//get String A from String resource and get String []B and []C from database
Resources ress = c.getResources();
String[] A = ress.getStringArray(R.array.exam_question);
//do here get B,C from db, Here im stuck with.
String[] B=;
String[] C= ;
int[] images = {R.drawable.icon_1,R.drawable.icon_2};
for (int i =0; i<40; i++ ){
list.add(new singlerow_info(A[i],B[i],C[i],images[i]));
}
}
Please help.
class resultadapter extends BaseAdapter {
//Get YEAR values from previous activity
Intent intent = getIntent();
String year = intent.getStringExtra("YEAR");
ArrayList questionmapB,questionmapC;
ArrayList list;
resultadapter(Context c)
{
list = new ArrayList<singlerow_info>();
questionmapB = new ArrayList<String>;
questionmapC = new ArrayList<String>;
readFromDB();
//get String A from String resource and get String []B and []C from database
Resources ress = c.getResources();
String[] A = ress.getStringArray(R.array.exam_question);
//do here get B,C from db, Here im stuck with.
int[] images = {R.drawable.icon_1,R.drawable.icon_2};
for (int i =0; i<40; i++ ){
list.add(new singlerow_info(A[i],questionmapB.get(i) ,questionmapC.get(i),images[i]));
}
}
private void readFromDB()
{
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT _id, COLUMN_B, COLUMN_C FROM TABLE_NAME WHERE YEAR_COLUMN='"+ year + "'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
questionmapB.put("COLUMN_B", cursor.getString(1));
questionmapC.put("COLUMN_C", cursor.getString(2));
} while (cursor.moveToNext());
}
}

Android: Inserting Arraylist values into database

Hi can someone point me in right direction how to insert arraylist values into database. i have two arraylist as below:
ArrayList<HashMap<String, String>> achieve_val = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> achieve_vol = new ArrayList<HashMap<String, String>>();
HashMap<String, String> Achiev_Vol_map = new HashMap<String, String>();
Achiev_Vol_map.put(PRODUCT_CAKE, Achieve_Vcake);
Achiev_Vol_map.put(PRODUCT_YEASTEXTRACT, Achieve_Vyeastextract);
.
.
.
achieve_vol.add(Achiev_Vol_map);
i created a POJO class for this values as below:
public class ProductCategory {
ArrayList<HashMap<String, String>> achieve_val,achieve_vol ;
public ArrayList<HashMap<String, String>> getAchieve_val() {
return achieve_val;}
public void setAchieve_val(ArrayList<HashMap<String, String>> achieve_val) {
this.achieve_val = achieve_val;}
public ArrayList<HashMap<String, String>> getAchieve_vol() {
return achieve_vol;}
public void setAchieve_vol(ArrayList<HashMap<String, String>> achieve_vol) {
this.achieve_vol = achieve_vol;}
/**my constructor**/
public ProductCategory(ArrayList<HashMap<String, String>> achieve_val,
ArrayList<HashMap<String, String>> achieve_vol) {
super();
this.achieve_val = achieve_val;
this.achieve_vol = achieve_vol;}}
I am passing this values to my database adapter as below
dbHelper.open();
dbHelper.topinchartsRecord(new ProductCategory(achieve_val,achieve_vol));
Now in my database adpater i am not sure how to access those vlaues and insert in database , done somethin as below:
public void topinchartsRecord(ProductCategory productCategory) {
for(ArrayList<HashMap<String, String>> map : achieve_val){
ContentValues cv = new ContentValues();
cv.put(FILE_NAME, map.get(FILE_NAME));
db.insert("tablename", null, cv);
}
}
Try this method Hope this help you
public void insertRecords(ArrayList<HashMap<String, String>> data,
String tableName) {
if (data != null && data.size() > 0) {
ContentValues con = new ContentValues();
db.beginTransaction();
for (int i = 0; i < data.size(); i++) {
con.clear();
HashMap<String, String> row = data.get(i);
Iterator<String> it = row.keySet().iterator();
while (it.hasNext()) {
String columnName = it.next();
con.put(columnName, row.get(columnName));
}
db.insertWithOnConflict(tableName, null, con,
SQLiteDatabase.CONFLICT_IGNORE);
}
db.setTransactionSuccessful();
db.endTransaction();
}
}
From your Method
public void topinchartsRecord(ProductCategory productCategory) {
try{
insertRecords(productCategory.getAchieve_val(),"tableNameForThisValues");
insertRecords(productCategory.getAchieve_vol(),"tableNameForThisValues");
}catch (Exception e) {
}
}
Pass ArrayList of Hashmap to this method and name of table will insert into database.

List view is not populating with database when first time activity start

I am using SQlite Database.I create 2 tables and populate them(working fine) and use there data to show in list view. when first time list adapter is populating list view show newDATA varibale as zero. some data. but when i go back and furter start that activity newDATA has correct values. . I use below code
public ArrayList<HashMap<String, Object>> GetDataFirst(String id) {
ArrayList<HashMap<String, Object>> firstvariable = new ArrayList<HashMap<String, Object>>();
Cursor c =sdb.rawQuery("Select * from table_name ",new String[]{id} );
String[] colums = c.getColumnNames();
while (c.moveToNext()) {
HashMap<String, Object> record = new HashMap<String, Object>();
for (int i = 0; i < colums.length; i++) {
record.put(colums[i], c.getString(c.getColumnIndex(colums[i])));
}
{
ArrayList<HashMap<String, String>> now_add = GetmainData(id, record.get("vget").toString());
record.put("NewDATA", now_add);
hint.add(record);
}
}
}
c.close();
return firstvariable ;
}
public ArrayList<HashMap<String, String>> GetCluesDataVerical(String id,String vhid) {
ArrayList<HashMap<String, String>> hint = new ArrayList<HashMap<String, String>>();
Cursor c = sdb.rawQuery("SELECT * FROM table_data", new String[] {id,vhid});
String[] colums = c.getColumnNames();
while (c.moveToNext()) {
HashMap<String, String> record_add = new HashMap<String, String>();
for (int i = 0; i < colums.length; i++) {
record_add.put(colums[i], c.getString(c.getColumnIndex(colums[i])));
}
hint.add(record_add);
}
c.close();
return hint;
}
and in list adapter i call these as below:
recordData=context.crossDatabaseAdapter.GetDataFirst(value);
wArray = (ArrayList<HashMap<String, String>>) recordData.get(position).get("NewDATA");
I tried a lot But still not come with any solution.
You must use an instance state to save the content of the list adapter when you stop the application and when application is starting you must check if you can retrieve data thanks to instance state, else load data from database, this might solve your problem

Database results in a listView is not working

i m trying to get data from my database and present them to a listView.i m using two classes for my database:
1st class is the DBAdapter and the method i use to get data is:
public String[] getData()
{
// String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
String[] columns =new String[]{DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
String result="";
String sa = null;
String sb = null;
String sc = null;
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
sa=c.getString(is1);
sb=c.getString(is2);
sc=c.getString(is3);
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return new String[] {sa,sb,sc};
}
2.The secind class in SQLView and thats the way i m trying to create my list
public class SQLView extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
HotOrNot entry2=new HotOrNot(this);
entry2.open();
String[] data2=entry2.getData();
entry2.close();
Toast.makeText(SQLView.this, data2[1].toString(), Toast.LENGTH_SHORT).show();
ListView list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map = new HashMap<String, String>();
map.put("name",data2[0].toString());
map.put("address", data2[1].toString());
map.put("address2", data2[2].toString());
mylist.add(map);
// ...
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
data2, new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
What i have to do in order to work?This is my first try to use database in android!
Now i just get an empty background...
EDIT:
With my toast i m getting the last item in position 1 that i added to db
Could anybody help me with the return statement in my DBHelper please?
Below is the fixed code. Try this out.
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] { "name", "address", "address2"},
new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
Basically you have to specify the columns/key of the map that you added. So it will map key from map to the corresponding view.
For looping issue, this will do the job:
for(int i=0; i<data2.length; i+=3) {
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("name",data2[i].toString());
map.put("address", data2[i+1].toString());
map.put("address2", data2[i+2].toString());
mylist.add(map);
}
The getData fix:
ArrayList<String> arrForData = new ArrayList<String>();
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
arrForData.add(c.getString(is1));
arrForData.add(c.getString(is2));
arrForData.add(c.getString(is3));
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return arrForData.toArray();

Categories

Resources