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();
Related
I need to display 5 different groups of strings. Some of the items in these strings are one or two sentences long. I was considering using an expandablelistview. The user could click one of the 5 categories and it would open up with all the strings.
Is there a string cut off for strings in an expandablelistview and listview or a way to display more then a sentence of information? I don't want the strings to get cut off. Note, I am not asking about the number of elements in the list.
public class HappyStuff extends ExpandableListActivity {
private static final String NAME = "NAME";
private static final String IS_EVEN = "IS_EVEN";
private ExpandableListAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.somelayout);
List<Map<String, String>> someData = new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> someotherData = new ArrayList<List<Map<String, String>>>();
//FROM HERE
for (int i = 0; i < 1; i++) {
Map<String, String> curGroupMap = new HashMap<String, String>();
someData.add(curGroupMap);
curGroupMap.put(NAME, "It's a wonderful life");
curGroupMap.put(IS_EVEN, (i % 2 == 0) ? "This group is even" : "This group is odd");
List<Map<String, String>> children3 = new ArrayList<Map<String, String>>();
{
Map<String, String> curChildMap = new HashMap<String, String>();
children3.add(curChildMap);
curChildMap.put(IS_EVEN, "And it's a beautiful day in the neighborhood");
}
someotherdata.add(children3);
}
//TO HERE
//YOU CAN JUST KEEP ON ADDING NEW INSTANCES
supermagnificentAdapter = new SimpleExpandableListAdapter(
this,
someData,
R.layout.somespecialtypeoflayout,
new String[] { NAME, IS_EVEN },
new int[] { R.id.text1, R.id.text2 },
childData,
R.layout.rowsimpleitemlist2,
new String[] { NAME, IS_EVEN },
new int[] { R.id.text1, R.id.text2 }
);
setListAdapter(supermagnificentAdapter);
}
}
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();
}
In my ListView, I save my data like this:
public void listview_fuellen(){
DBHelper db = new DBHelper(this);
ListView lv = (ListView) findViewById(R.id.lvKinder);
Cursor c = db.select();
int count = c.getCount();
TextView tv = (TextView) findViewById(R.id.secondLine);
List<String> auswahl = new ArrayList<String>();
List<String> auswahl1 = new ArrayList<String>();
int i = 0;
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
while(c.moveToNext())
{
auswahl.add(c.getString(c.getColumnIndex("name")));
auswahl1.add(" " + i);
System.out.println("SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS" + auswahl.get(i).toString());
i++;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.firstLine, auswahl);
//ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.list_black_text,R.id.secondLine, auswahl1);
lv.setAdapter(adapter);
}
Now, I tried the layout for my listview from here:
http://android-developers.blogspot.co.at/2009/02/android-layout-tricks-1.html
Now, how can I set values for the second line in each ListView-Item or the icon?
EDIT:
I tried it like this now, but there are no entries in my listview then.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();;
int i = 0;
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$1" + c.getCount());
while(c.moveToNext())
{
//map = new HashMap<String, String>();
map.put("name",c.getString(c.getColumnIndex("name")));
map.put("datum", c.getString(c.getColumnIndex("zeit")));
i++;
}
mylist.add(map);
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.list_black_text, new String[] { "datum",
"name" }, new int[] { R.id.firstLine, R.id.secondLine });
lv.setAdapter(mSchedule);
setListAdapter(mSchedule);
What have I done wrong?
for that you can make a custom adapter and then you can set values for the second line in each ListView-Item.
You can check this link
http://devtut.wordpress.com/2011/06/09/custom-arrayadapter-for-a-listview-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.
I am trying to refresh my list view after deleting items from it, but i am not geting the desired result. The items are getting deleted when i reload the activity but not at the time i press the delete button. Here is my code:
public void deletelist()
{
mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<favtitle.size();i++)
{
map = new HashMap<String, Object>();
map.put("train",favtitle.get(i));
map.put("value",favloc.get(i));
map.put("employer",favemp.get(i));
mylist.add(map);
}
mSchedule = new SimpleAdapter(this, mylist, R.layout.listdelete,
new String[] {"train","value","employer"}, new int[] {R.id.dept,R.id.jobloc,R.id.employer});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
lv.setCacheColorHint(Color.WHITE);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
String str3 = favtitle.get(position);
db.delete("Favorites7","title= '"+str3+"'",null);
db.close();
//Data Base Select
db = openOrCreateDatabase(
"ClassifiedAds.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
//End-----
cur= db.query("Favorites7",
null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
favtitle.add(cur.getString(1));
favloc.add(cur.getString(3));
favemp.add(cur.getString(2));
lat.add(cur.getString(7));
log.add(cur.getString(8));
cur.moveToNext();
}
cur.close();
//mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<favtitle.size();i++)
{
map = new HashMap<String, Object>();
map.put("train",favtitle.get(i));
System.out.println("==="+favtitle.get(i));
map.put("value",favloc.get(i));
System.out.println("==="+favloc.get(i));
map.put("employer",favemp.get(i));
System.out.println("==="+favemp.get(i));
mylist.add(map);
}
mSchedule.notifyDataSetChanged();
mSchedule = new SimpleAdapter(Favorites.this, mylist, R.layout.listdelete,
new String[] {"train","value","employer"}, new int[] {R.id.dept,R.id.jobloc,R.id.employer});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
lv.setCacheColorHint(Color.WHITE);
}
});
}
Any help would be appreciated.
Thanx in advance.
Use notifyDataSetChanged() method with the listview within the condition where you are deleting the record , just after deleting the record put the listview.notifyDataSetChanged() code.
hope this will solve your problem.
Did you tried invalidateViews, it will redrawn you list view.
My problem is solved. What i had to do was that whenever the arraylist was reloading i had to clear the previous one.
Like:
array-list name.clear();
before adding items into the hashmap.