Getting individual text values from items in listView - android

I have a custom list view over here that displays data from a sqlite database with the help of a simple cursor adapter.
I would like to know how to display a toast showing the title of each entry (e.g. School) when I hold down each entry (using a longClickListener). Thanks.
The method for the simple cursor adapter which is called during onResume:
private void populateListViewFromDatabase() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.retrieveAllEntriesCursor();
String[] from = {DBAdapter.ENTRY_TITLE, DBAdapter.ENTRY_DESTINATION};
int[] to = {R.id.tvAlarmTitle, R.id.tvAlarmDestination};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.custom_alarm_destination_row,c,from,to);
db.close();
lvDestinations.setAdapter(myCursorAdapter);
}

Related

Retrieve values from database and launch a new activity when listview item is clicked

I created a listview using an adapter. The list view contains elements retrieved from a SQLite database using a distinct query.
When the listview item is clicked, it should retrieve all items which have that value used to do the distinct query.
My query looks like this:
#Override
public List<Registro> distinctByCol3(){
List aux = new ArrayList();
Cursor cursor = db.query(true,Registro.TABLE_NAME,new String[]{
BaseColumns._ID,
RegistroColumns.Col1,
RegistroColumns.Col2,
RegistroColumns.Col3,
},null,null,RegistroColumnas.Col3,null,null,null);
if (cursor.moveToFirst())
do {
Registro registro = this.parseCursorToR(cursor);
if (registro != null)
aux.add(registro);
}
while (cursor.moveToNext()) ;
if(!cursor.isClosed()){
cursor.close();
}
return aux;
}
The java class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_registros);
final ListView listView = (ListView) findViewById(R.id.lvRegistros);
DataManager servicio = new DataManagerImpl(this);
List<Registro> listRM = service.getRegistroDistinctedByCol3();
final ArrayAdapter<Registro> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listRM);
listView.setAdapter(adapter);
After click in the element, I need to launch a new activity and show all the elements whit COL3, which is the value I used in my query.
How can I achieved my goal?
If some clarification is needed, let me know. Thanks.

How to create a list view from a string variable which is from an SQLite Database

In my database code I am currently going through the ID column of my SQLite database. Now at the moment this is returning a string variable which is listing ALL entries when I put it is a textview for example. But I want to put that variable in some sort of listview (or similar) and for the user to be able to click that listview and bring up the rest of the information relating to that primary key. I have been doing research and have found tutorials to do this but they do not suite what I want to do and didn't exactly work for me. Here is the code below which is in the activity that I want to show the list view. Thanks.
public class View_Saved_Dates extends Activity {
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_saved_dates);
list = (ListView)findViewById(R.id.listView1);
Database info = new Database(this);
info.open();
//I want to put the variable below which lists all data within the id column of my database into a list view or some sort of list where I can
//click the id and bring up the rest of the information in the databse.
String cowid = info.getCowid();
info.close();
}
}
Try the below links-
http://www.mysamplecode.com/2012/07/android-listview-cursoradapter-sqlite.html
http://androidtuts4u.blogspot.in/2012/11/android-sqlite-and-listview-example.html
How to Read Data from SQLite Database and show it on a list view
Display data from SQLite database into ListView in Android
How to get data form SQLite database and display it in ListView
how to fetch the data from sqlite database and show in listview in android
It may help you.
I beleive that this should fix your problem. In your database class, instead of putting all your queried data into a string put it into a string array.
public String[] getid() {
// TODO Auto-generated method stub
ArrayList<String> strings = new ArrayList<String>();
String[] id = new String[]{KEY_ID};
Cursor c = ourDatabase.query(DATABASE_TABLE, id, null, null, null, null, null);
int i = c.getColumnIndex(KEY_ID);
while(c.moveToNext()){ strings.add(c.getString(i));
}
String[] mString = (String[]) strings.toArray(new String[strings.size()]);
return mString;
}
Then in your main class where you want this list view to be displayed create a list view and use the following code and it should work
lv = (ListView)findViewById(R.id.list_view);
Database info = new Database(this);
info.open();
String [] id = info.getid();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, id);
lv.setAdapter(arrayAdapter);
info.close();
I hope that works for you

Query into list activity with rawquery using multiple tables?

Basicly I have two tables in my database brought together with a rawQuery and a ListActivity that is supposed to pull info from both tables and show it inside of the ListActivity. I don't get a force close or anything the problem is the information does not show up.
MY RAWQUERY
public Cursor fetchName() {
return ourdb.rawQuery("SELECT wwJobTable._id, wwJobTable.JobName, wwLatLonTable.JobLatitude FROM wwJobTable, wwLatLonTable WHERE wwLatLonTable.JobLatitude=wwJobTable.JobCode", null);
}
HOW I AM VIEWING THE TABLE INFORMATION
private void fillData() {
// Get all of the rows from the database and create the item list
mTimeNotesCursor = mDbHelper.fetchName();
mTimeNotesCursor.moveToFirst();
startManagingCursor(mTimeNotesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{WWDatabase.KEY_JOBNAME,
WWDatabase.KEY_LATITUDE};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.textView1, R.id.textView2};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.time_text_row, mTimeNotesCursor, from, to);
setListAdapter(notes);
}
What could I possibly be doing wrong?

Problem using Cursor to populate a spinner

I'm having a problem populating my spinner with data from my SQLite database. Here's the code from my Activity. The Activity crashes with an Unable to start Activity ComponentInfo error where indicated with an arrow.
public class ProjectsActivity extends Activity {
private ReelDbAdapter dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.projects_select);
fillProjectSpinner();
}
private void fillProjectSpinner(){
// initialize cursor to manage data binding to spinner
Cursor projectCursor = null;
Spinner spnExistingProjects = (Spinner)findViewById(R.id.spnExistingProject);
---> projectCursor = dbHelper.getExistingProjects();
//startManagingCursor(projectCursor);
/*
//get the list of project names from the database
String[] from = new String[] {dbHelper.clmProjectName};
//add a new item to the spinner for each of the rows in the database
int [] to = new int[]{R.id.txtViewProjectRow};
//initialize a cursor adapter (similar to ArrayAdapter when populating a spinner from a pre-defined array)
SimpleCursorAdapter projectAdapter = new SimpleCursorAdapter(this, R.layout.view_project_row, projectCursor, from, to);
//add all the rows to the spinner
spnExistingProjects.setAdapter(projectAdapter);
*/
}
Here's the code from the getExistingProjects method from my dbAdapter
public Cursor getExistingProjects() {
if(mDb == null)
{
this.open();
}
return mDb.query(dbTableProject, new String[] {clmProjectName, clmProjectShootingTitle, clmProjectJobNumber},
null, null, null, null, null);
}
Any clues on what I might be doing wrong?
TIA for any help.
Norm
Why don't you try making sure the query is returning something before returning the cursor in your method? Put a log line in that spits out the count of the cursor. Also, you should be able to see this easily while stepping through with the debugger.
Also, why assign null to the cursor's deceleration when you're just going to initialize it a few lines down. Do it all in one line.
Lastly, what db are you trying to one with that this.open() line? I obviously can't tell with just the code you've posted, but put a try catch around that whole thing and spit out the strackTrace. You should see your issue.

ListView Click event

I have a List of Items which I retrieved from my Sqlite DB...
I want to set a Click event for each item. How I can customize this event based on the Item clicked????
Be descriptive... I am a beginner.
This is the method that I used to fill data in my List:
private void fillData() {
db = new DBAdapter(this);
db.open();
ArrayList db_results = new ArrayList();
//All Category
//Cursor cursor = db.getAllTitles();
//Single Category
Cursor cursor = db.getTitle(1);
if (cursor.moveToFirst())
{
do {
db_results.add(cursor.getString(4));
} while (cursor.moveToNext());
}
cursor.close();
this.list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, db_results));
}
Call setOnItemClickListener() on the ListView. The AdapterView.OnItemClickListener listener you provide will be given the position (0-based index) and ID (if you were using a CursorAdapter, as you should be, rather than converting the Cursor into an ArrayList), so you will know which item was clicked upon.

Categories

Resources