How to fetch data into a textview? - java

This is my code to fetch database values into the listview.
private void fetchData2() {
db = helper.getReadableDatabase();
Cursor c = db.query(DBhelper.TABLE2, null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(
this,
R.layout.row2,
c,
new String[]{DBhelper.Amount},
new int[]{R.id.lbl});
list.setAdapter(adapter);
}
I just want to change this code to get the values into a textview,

You can do like this...
if(c.moveToFirst()){
String data = c.getString(c.getColumnIndex("Column's Index"));
yourTextView.setText(data);
}

Related

How do I get the ListView reverse position in android?

I use a ListView to show a history of a record. I use database in order to store and pull the record into the ListView. Now, I want to pull this record again by doing this:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
positions = position;
checkdata();
}
}
and get the data from the database using the positions and turning into id
public void checkdata() {
String[] columns = {"id", "titleexam", "address", "time", "score", "Examdata", "totalscore", "hour", "min", "sec", "timedis"};
Cursor cursor = sqLiteDatabase.query("record", columns, null, null, null, null, null);
cursor.move(positions);
ExamRecord recordr = new ExamRecord(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10));
title = cursor.getString(1);
data = cursor.getString(5);
}
The problem begin when I sort the ListView in reverse, because the ListView will show all the recent record at the top and the older ones at the bottom using this in the adapter to show it at the user.
Collections.reverse(records);
How can I get the position(id) on the reverse order?
you have many options
option 1: you can reverse query order based on one column
Cursor cursor= sqLiteDatabase.query("record", columns, null, null, null, null yourColumn+" DESC");
option 2: if you send data on ArrayList data structure you can reverse it
Collections.reverse(YourList);
option 3: you can reverse ListView it self i don't it will work for your question
android:stackFromBottom="true"
android:transcriptMode="normal"

ANDROID STUDIO >> Sql Listview

I have this code which displays the content of the JOUEUR column.
This table has other columns, but I do not know how to display the column _ID next to the JOUEUR name
private void show_tab() {
helper = new TaskDBHelper(ValiderJoueurs.this);
SQLiteDatabase sqlDB = helper.getReadableDatabase();
Cursor cursor = sqlDB.query(Round.TABLE,
new String[]{Playas.Columns._ID, Playas.Columns.JOUEUR, Playas.Columns.PLACE},
null, null, null, null, null);
listAdapter = new SimpleCursorAdapter(
this,
R.layout.players_view,
cursor,
new String[]{Playas.Columns.JOUEUR},
new int[]{R.id.nomdujoueur},
0
);
this.setListAdapter(listAdapter);
}
Thank you very much!
Lets say you have a text view that has id = R.id.myid
listAdapter = new SimpleCursorAdapter(
this,
R.layout.players_view,
cursor,
new String[]{Spray.Columns._ID,Playas.Columns.JOUEUR},
new int[]{R.id.myid, R.id.nomdujoueur},
0
);

After Binded Spinner , How to Do not select first item?

public void DatabaseConn(){
DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
myDbHelper = new DataBaseHelper(this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
SQLiteDatabase db = myDbHelper.getReadableDatabase();
//SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.abc2/databases/DB_BusData", null, 0);
Cursor c = db.rawQuery("SELECT * FROM Tbl_Driver", null);
startManagingCursor(c);
//create an array to specify which fields we want to display
String[] from = new String[]{"Driver_Name"};
//create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
//create simple cursor adapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
//get reference to our spinner
Spinner s = (Spinner) findViewById( R.id.DriverSpin);
s.setAdapter(adapter);
db.close();
}
This is the code how i bind my Spinner with my DataBase,
But After Binded Spinner, will automatically selected first item,
i would like to let User select the Spinner by themself, how to do with this?
read this documentation page: http://developer.android.com/reference/android/widget/AbsSpinner.html
Just add a default selection after populating the spinner something like --select driver-- then do a check in your listener to not do anything when the spinner position is 0
SQLiteDatabase db = myDbHelper.getReadableDatabase();
//SQLiteDatabase db = SQLiteDatabase.openDatabase("/data/data/com.example.abc2/databases/DB_BusData", null, 0);
Cursor c = db.rawQuery("SELECT * FROM Tbl_Vehicle", null);
//=====Add Additional=====
MatrixCursor extras = new MatrixCursor(new String[] { "_id", "Plat_No" });
extras.addRow(new String[] { "-1","< Select Vehicle >" });
Cursor[] cursors = { extras, c };
c = new MergeCursor(cursors);
//===========================
Just Add the First Items as blank or i have added "< Select Vehicle >" as the first item

How to Fill a Spinner with Database Info

I have a spinner that I need to fill with information from a database, but the examples that i found don't work.
Here is my code:
public void consulta() {
AdminSQLiteOpenHelper admin =
new AdminSQLiteOpenHelper(this, "administracion", null, 1);
SQLiteDatabase bd=admin.getWritableDatabase();
Cursor c = bd.rawQuery("select name from category",null);
startManagingCursor(c);
// create an array to specify which fields we want to display
String[] from = new String[]{"name"};
// create an array of the display item we want to bind our data to
int[] to = new int[]{android.R.id.text1};
// create simple cursor adapter
SimpleCursorAdapter adapter =
new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c, from, to );
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
spinner1.setAdapter(adapter);
bd.close();
}
This is how I do it:
db = SQLiteDatabase.openDatabase("/data/data/packagename/databases/mydata.db", null, SQLiteDatabase.OPEN_READONLY);
Cursor c_cat = db.rawQuery("select _id, column_desc from your_table", null);
startManagingCursor(c_cat);
spinner = (Spinner) findViewById(R.id.spinnername);
String[] from = new String[]{"column_desc"};
int[] to = new int[]{android.R.id.text1};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c_cat, from, to);
mAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(mAdapter);
db.close();
I did all that in my onCreate. I declared all my variables outside the method so that they are public to the whole class. One thing I made a mistake of in the past was closing the cursor after filling the spinner. The cursor must remain open if you want to see data in the spinner.
I hope this solves it for you.
EDIT: I noticed you are not querying for _id in your rawQuery. You must include that if you wish to populate the spinner. Analyze the code I provided.
Assuming you didn't leave any code out, you need to find/define spinner1, so that the framework knows where to put your info from the adapter.
private Spinner spinner1;
spinner1 = (Spinner) view.findViewById(R.id.yourspinnerid);
// rest of your code

How to set a contact's number in an auto complete textview in android?

I have a code from where I can get the contacts name in a auto complete text view, here is my code
autoContacts=(AutoCompleteTextView)findViewById(R.id.actvContacts);
Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(emailCursor);
autoContacts.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, emailCursor, new String[] {Email.DATA1}, new int[] {android.R.id.text1}));
autoContacts.setThreshold(0);
but when I click in one of the name then it sets a text in that Autocomplete textview like this:
android.content.ContentResolver$CursorWrapperInner#44efc9c8
but here I want to set the specific phone number on that, how to fix this issue?
You need to use the setCursorToStringConverter
Sets the converter used to convert the filtering Cursor into a String.
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html
In your case you could do something like this:
String[] from = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
autoContacts=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
startManagingCursor(emailCursor);
SimpleCursorAdapter adapter =new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, emailCursor, from, new int[] {android.R.id.text1});
adapter.setCursorToStringConverter(new CursorToStringConverter() {
#Override
public CharSequence convertToString(Cursor cursor) {
final int columnIndex = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
final String str = cursor.getString(columnIndex);
return str;
}
});
autoContacts.setAdapter(adapter);
autoContacts.setThreshold(0);

Categories