Android listview with database and fixed values - android

I'm quite beginner with android and I have problem with my listview.
So I'm getting listview values from database and thats working fine. But I want to edit one of those values before showing it to user and after many failures I kindly ask your advice.
Here is my code for fetching data and showing it in listview
listSquad=(ListView)findViewById(R.id.listview);
String[] arrayColumns = new String[]{"sq_pos", "sq_name", "sq_born", "sq_gam", "sq_goal"};
int[] arrayViewID = new int[]{R.id.pos,R.id.name,R.id.age,R.id.games,R.id.goals};
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c;
c = db.doQuery("squad", null, null, null, null, null, "sq_name");
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.team_list_lay, c, arrayColumns, arrayViewID);
listSquad.setAdapter(adapter);
So I would like to take value of "sq_born" column and alter it and then show up in listview. That column is integer.

Like suggested by Aditya Rai, I used viewbinder like below:
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int column) {
if( view.getId() == R.id.age){
//do something here
TextView text = (TextView)view;
//set text in here
text.setText("blaah");
return true;
}
return false;
}
});

Related

How to query data for edit before show in list view?

I query database and show in list view like this.
public void onResume() {
super.onResume();
db = dbHelper.getWritableDatabase();
String[] queryColumns = new String[]{"_id", DBHelper.COL_VEHICLE_TYPE, DBHelper.COL_OPTION_NAME,DBHelper.COL_DATE };
cursor = db.query(DBHelper.TABLE_NAME, queryColumns,null,null,
null,null,null);
String[] showColumns = new String[]{DBHelper.COL_VEHICLE_TYPE, DBHelper.COL_DATE};
int[] views = new int[] {android.R.id.text1, android.R.id.text2};
adapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item, cursor, showColumns, views);
lv_driver.setAdapter(adapter);
}
I keep data 0, 1 in DBHelper.COL_VEHICLE_TYPE. If data equal 0 I want to show string "car" if equal 1 show string "motobike".
How to write condition (if, else) for change integer to string and show in list view id lv_driver.
instead of using SimpleCursorAdapter, you can use extends CursorRecyclerAdapter for recyclerView, this CursorRecyclerAdapter extended RecyclerView.Adapter, you can create your cursor fetch logic on CursorRecyclerAdapter class.
this website will help you to customized each view item:
https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter

I want to update my listview once i add data in SQLite

Hi every one I am working on SQLite.when i add the data listview doesnt get updated
String[] from = { helper_ob.FNAME, helper_ob.LNAME };
int[] to = { R.id.tv_fname, R.id.tv_lname };
cursor = adapter_ob.queryName();
#SuppressWarnings("deprecation")
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
nameList.setAdapter(cursorAdapter);
here I am inserting the data to SQLite.
public long insertDetails(String fname, String lname) {
ContentValues contentValues = new ContentValues();
contentValues.put(openHelper_ob.FNAME, fname);
contentValues.put(openHelper_ob.LNAME, lname);
opnToWrite();
long val = database_ob.insert(openHelper_ob.TABLE_NAME, null,
contentValues);
Close();
return val;
}
i am following
http://androidtuts4u.blogspot.in/2012/11/android-sqlite-and-listview-example.html this given tutorial .
Any suggestion is appreciated.
But when I run same code 2nd time it shows added data.
Its not clear, at what stage of the view creation you are loading the data. Its worth you try calling the notifyDataSetChanged after loading the data. Its basically notifies the attached list that the underlying data has been changed and any View reflecting the data set should refresh itself.
cursorAdapter.notifyDataSetChanged()
Hope this helps

How to: Populating views in a layout from a single record returned by a cursor without using list view

I have an activity page which has textview fields(name , phoneNo) and some buttons. I query the database to get the details of a single customer(1 record). A cursor is returned. I want to show the values(name , phoneNo) returned in the textviews of this activity. How can I do that without using a listView. I don't need a list because only a single record is returned. It's like a profile page. But to use a cursor I think I need somekind of listView. How to do this ???? Thankz in advance
String[] fromColumns = {TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_RequestID,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_Destination,TakenJobDataBaseOpenHelper.TAKENJOBS_COLUMN_CustomerName};
int[] toViews = {R.id.textView1, R.id.textView3,R.id.CustomerName};
TakenJobDataBaseOpenHelper jobDatabaseHelper = new TakenJobDataBaseOpenHelper(this);
Cursor cursor = jobDatabaseHelper.getSingleRecord(id);
SimpleCursorAdapter cursoradapter = new SimpleCursorAdapter(this,
R.layout.activity_job_details, cursor, fromColumns, toViews, 0);
you don't have to use a listview. simply use it like this:
if (cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex('NAME'));
String phoneNo = cursor.getString(cursor.getColumnIndex('phoneNo'));
((TextView)findViewById(R.id.textView1)).setText(name);
((TextView)findViewById(R.id.textView2)).setText(phoneNo);
} else {
// nothing found!
}

How to implement a MatrixCursor for a Spinner?

I have a SQLite query that returns a Cursor. I want to add some extra rows to the Cursor by implementing a MatrixCursor(to try to keep the first item of real data from automatically selected when clicked). Then I want to map them to a SimpleCursorAdapter. I kept reading the posts(and codes) but still remain fuzzy to me how to code it to my existing code listed below.
Cursor cursor = myDB.query(DATABASE_TABLE_NAME, resultColumns, whereClause,
whereArgs, null, null, null, null);
// Create Spinner View object from layout resource
Spinner spinner = (Spinner) findViewById(R.id.spinner);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, // Use a template
// that displays a
// text view
cursor, // Give the cursor to the adapter
new String[] {"ename"}, // Map the NAME column in the
// people database to...
new int[] {android.R.id.text1});
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
If you want to build a MatrixCursor from a simple Cursor you'll have to parse the entire initial Cursor and append the rows you want:
//...
MatrixCursor mc = new MatrixCursor(resultColumns);
// add extra rows, this will probably not work if you want to insert them
// between the initial cursor rows because of the _id column that need autoincrement values
mc.addRow(new Object[] { new Long(-2), "Extra name1" });
mc.addRow(new Object[] { new Long(-1), "Extra name2" });
// I don't know what your cursor holds, I assumed you have the _id column(long value)
// and a name(String value)
int size = cursor.getCount();
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
mc.addRow(new Object[] {
cursor.getLong(cursor.getColumnIndex(/*the _id column*/)),
cursor.getString(cursor.getColumnIndex(/* the name column(ename?!?)*/)) });
}
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, mc, new String[] {"ename"}, new int[] {android.R.id.text1});
//...
If you're doing this just to avoid the OnItemSelectedListener from firing when the Spinner is shown, maybe you could have another approach. For example in your listener:
//boolean status = true; flag in MyOnItemSelectedListener
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if (status) {
status = false;// this is the first listener trigger so you probably want to ignore it
return;
}
// do stuff here
}
Note: I don't know how good is the above solution. If you look there are probably much better solution to this Spinner related issue.

Spinner Data Connectivity issue- data binding successful but spinner shows blank text

i have been trying to bind spinner to a database and finally got succeeded. i have connected spinner to database table using cursoradapter. but the problem is the spinner gets populated but the list items in it shows blank text. i know binding is successful because it shows as many rows as there are records in database table.
can not figure out what it is. some body please help i am stuck here
i am posting the code below
public long createAccount() {
ContentValues initialValues = createContentValues();
Log.i("DB", initialValues.get(KEY_NAME)+":"+
initialValues.get(KEY_MAILBOXTYPE)+":"+
initialValues.get(KEY_OUTPORT)+":"+
initialValues.get(KEY_INPORT)+":"+
initialValues.get(KEY_INSERVER)+":"+
initialValues.get(KEY_OUTSERVER)+":");
return database.insert(DATABASE_TABLE, null, initialValues);
}
/**
* Return a Cursor over the list of all todo in the database
*
* #return Cursor over all notes
*/
public Cursor fetchAccount() {
Log.i("DB", "Cursor opened");
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_NAME,KEY_INSERVER}, null, null, null,
null, null);
}
Code for spinner binding is below:
mAccAdap.createAccount();
Cursor c = mAccAdap.fetchAccount();
startManagingCursor(c);
SimpleCursorAdapter CursorAdapter = new SimpleCursorAdapter(
this,android.R.layout.simple_spinner_item,c,
new String[]{mAccAdap.KEY_INSERVER},new int[]{R.id.tvDBViewRow});
CursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinEmail=(Spinner)findViewById(R.id.spinAccount);
spinEmail.setAdapter(CursorAdapter);
Try changing this part of your SimpleCursorAdapter...
new int[]{R.id.tvDBViewRow}
to this...
new int[]{android.R.id.text1}

Categories

Resources