Showing database in ListView - java

I am new to android and after going through many tutorials I still can't get things.
I have created a database and is now reading this database.
This database and 3 columns All three string type.
Now I want to show this database using ListView.I have this ListView
created with id "listDatabaseItems" in R.layout.viewDatabase
Currently my code is:
Cursor c = sqliteDatabase.query(MY_DATABASE_TABLE, null, null, null, null, null, null);
int firstNameColumn = c.getColumnIndex("name_column");
int protectedColumn = c.getColumnIndex("protected_id_column");
ArrayList<String> results = new ArrayList<String>();
int i = 0;
c.moveToFirst();
do {
i++;
String firstName = c.getString(firstNameColumn);
String protectedID = c.getString(protectedColumn);
results.add(firstName + protectedID); // NEED TO add here
} while (c.moveToNext());
sqliteDatabase.close();
Please tell me what changes I do and how can I show my database in ListView.
Best Regards.

Make a Adapter add your ListArray values to it, and then setAdapter to your listview simple,
After your code, add just two lines,
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results);
mListView.setAdapter(adapter);
Also, your question sounds basic for android So just look at these links..
SimpleCursorAdapters and ListViews
Android SQLite Database and ContentProvider - Tutorial
Filling the Layout with Data

Since you're using a Cursor in the first place, I'd actually go with a SimpleCursorAdapter. A CursorAdapter allows you to avoid loading the whole data set into memory, making initial load times faster and avoiding huge allocations with large data sets.
To expand on the basic setup in this post (SimpleCursorAdapters and ListViews), You'll also want to set a view binder to customize the way data is bound to the view, ie. concatenate your two strings.
Something like this:
SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
//you're only using one view, so we can ignore columnIndex
TextView text = view.findViewById(R.id.list_item_text);
//the cursor is already in position for this list item
int firstNameColumn = c.getColumnIndex("name_column");
int protectedColumn = c.getColumnIndex("protected_id_column");
String firstName = c.getString(firstNameColumn);
String protectedID = c.getString(protectedColumn);
text.setText(firstName+protectedID);
return true; //we have handled binding the data
}
};
Then when you set up your adapter, you pass it a layout that can be used for each item. The layout should contain a TextView to hold the one text item you want to show. You may be able to use android.R.layout.simple_list_item_1 for this as user370305 suggested, but I'm not sure what the id of its TextView is -- it might be android.R.id.text1.
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
context, //can be your Activity
R.layout.list_item, //xml layout containing textview with id 'list_item_text'
c, //the cursor
new String[] {"name_column"}, //the column to read (we'll do more in the binder)
new int[] {R.id.list_item_text}, //the view to hold the data
0 //no flags
);
adapter.setViewBinder(binder);
Add it to your ListView the same way:
ListView listView = //your ListView reference
listView.setAdapter(adapter);
//or in a ListActivity:
setListAdapter(adapter);

Related

How to get an attribute from an object from an arrayList

I want to populate a listview from an ArrayList received from another activity, but I don't know how to show only the name of the country.
This is what I have tried. This will only show the address of my countries, not the name.
lv = (ListView) findViewById(R.id.listViewCountries);
lv.setAdapter(new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, countries));
I expect to have a listView full of only the names of the countries and when I press on one of them to have an activity open to show info about that country.
You could create another array, like :
Arraylist <String> array2 = new ArrayList() ;
for (Country country : countries){
array2.add(country.getname())}
And then in your spinner you put array2 instead of countries.
Since you added them in the same order, their index will be the same than in your countries array, then if you want to get à country back, in your onselected item listener, for example, you just have to out sthg like
Country selectedcountry = countries.get(position);
This is how I did it for a list of contacts :
String [] contractList = new String[user.getUserContractsList().size()];
for (int i = 0; i < user.getUserContractsList().size(); i ++){
contractList[i] = user.getUserContractsList().get(i).getCONTRACT_NAME();
}
//Setting the spinner
ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, contractList);
spin.setAdapter(adapter);
spin.setOnItemSelectedListener(this);
And then to get back the contact value when the user selects an item :
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
contract = user.getUserContractsList().get(position);
}
I don't know if this is clear to you ?
Because you are using default implementatino, such as FragmentList, you have prebuilt xml for item list and hence, cannot change it in right way. You need to implement you custom implementation, with new RecyclerView.
Please search for the information, before asking this question. Basic sample you can find in another answer.

Populating an ExpandableListView with data from database

I have a SQLite database that contains multiple tables. For each table, I am trying to represent all the data in an ExpandableListView using a custom CursorTreeAdapter. As far as I understand, the method getChildrenCursor returns a cursor that points to the data I needed to populate my child views. However, I do not have a concrete idea on how to retrieve the children cursor using the groupCursor parameter.
#Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
String[] projection = { "columnNeeded" };
return context.getContentResolver()
.query(CONTENT_URI, projection, null, null, null);
}
the above method will return a cursor that returns all rows containing the column I need. Is this the right way to do this?
In the "columnNeeded" column for each row of the table, it contains a String representation of a jsonArray. I was trying to store an arrayList into each row of the table using JSONArray. Therefore, I am trying to retrieve this arrayList and populate the child views with this arrayList like so:
#Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
TextView name = (TextView) view.findViewById(R.id.summary_child_name);
TextView bill = (TextView) view.findViewById(R.id.summary_child_bill);
String arrayListString = cursor
.getString(cursor.getColumnIndex("columnNeeded"));
JSONObject json = new JSONObject();
try {
json = new JSONObject(arrayListString);
} catch (JSONException e) {
Log.e(LOG_TAG, "Unable to retrieve list of items");
}
JSONArray jsonArray = json.optJSONArray(ReceiptContract.JSONARRAY_NAME);
// retrieveArrayList iterates through jsonArray and adds it to items
items = retrieveArrayList(jsonArray);
name.setText(What do I put here?);
bill.setText(What do I put here?);
}
As you can see I have managed to retrieve the entire array list as an ArrayList type object. However, I am stuck on displaying the array list in the child views. Any idea on how I can go about doing this?
Use HashMap to store your data as key value and populate your data into your ExpandableListView using BaseExpandableListAdapter

How to make search results clickable

I have created a database app, where the user can search for a film. The app will then return the results of this search in a ScrollView. I then want these search results to be clickable, which will lead to another page to display full details of the film selected. What is the best way to do this?
public void search(View v){
EditText search = (EditText)findViewById(R.id.edtSearch);
String searchresult = "%" + search.getText().toString() + "%";
db = new DbHelper(this).getReadableDatabase();
String[] tblColumns = {"*"};
String where = "film LIKE ? OR actor LIKE ? OR actor2 LIKE ? OR director LIKE ?";
String[] args = {searchresult, searchresult, searchresult, searchresult};
Cursor results = db.query("FILMTABLE", tblColumns, where, args, null, null, null);
film(results);
}
public void film (Cursor c){
c.moveToFirst();
int titleIndex = c.getColumnIndex("film");
int idIndex = c.getColumnIndex("id");
String title = c.getString(titleIndex);
int filmID = c.getInt(idIndex);
TextView txt = new TextView(getApplicationContext());
txt.setId(filmID);
txt.setText(title);
txt.setTextColor(Color.BLACK);
txt.setTextSize(15);
ScrollView scrollView = (ScrollView)findViewById(R.id.scrolLView);
scrollView.addView(txt);
}
Filter your search result in arraylist and use the listview for showing the result. Then implement listview.setOnItemClickListner.
Follow this : http://www.javacodegeeks.com/2013/06/android-listview-tutorial-and-basic-example.html
Every widget in Android is a clickable object. You can format the results however you like, likely in a ListView and set each one's onClick attribute to be whatever function in Java you need executed when clicked. If they all operate the same, you can just put a click listener on the entire list. If they are intended to operate differently (the more likely case) you can individually assign methods to each element in the list using the above.

How do I get String values on listview from the ArrayList<String[]>?

Hi I had asked before how do I update listview and got suggestion to make use of ArrayAdapter.I follow the thing now the issue is about it's updating listviews but not with expected values some object .
My code is like this:
Cursor cursor = db.rawQuery("SELECT count(*) FROM BOOKMARKS ", null);
//if any contact found moveToFirst of that contacts or points to 1st email
cursor.moveToFirst();
//checking columns exhist or not
if (cursor.getInt(0) > 0)
{
//get all the contacts from table 'BOOKMARKS'
c = db.rawQuery("SELECT * FROM BOOKMARKS", null);
//get the index of mentiond and required columns
ID = c.getColumnIndex("ID");
booktitl = c.getColumnIndex("Booktiltle");
audiotime=c.getColumnIndex("BookmarkTime");
// Check result.
c.moveToFirst();
if (c != null)
{
// Loop through all Results
do
{
cont = new String[3];
cont[0] = Integer.toString(c.getInt(ID));
cont[1] = c.getString(booktitl);
cont[2] = c.getString(audiotime);
audiobooks.add(cont);
}
while(c.moveToNext());
c.close();
db.close();//close db resources
ArrayAdapter<String[]> arrayAdapter1=new ArrayAdapter<String[]>(this, R.layout.row3, R.id.itemr1,audiobooks);
lv.setAdapter(arrayAdapter1);
How do i get the string value on listview?Actually the values fetching from database and storing into Arraylist.At present I am displaying some object value on listview but I want to display content that "audiobooks" holds. Please help me to solve it.
You can't give the ArrayAdapter the type String[], because you're giving it a List called audiobooks.
Also, I'm not sure if the standard ArrayAdapter can handle a datatype List<String[]>. It might not work. You would have to make your own implementation of the ArrayAdapter by extending it, if it shouldn't work.
If I understand correctly, you want to populate a ListView with data from each row from your database. You must create a new class of type Audiobook with the member variables corresponding with database row. Then create a layout in your res/layouts folder that must show your object's data. Create a new class extending ArrayAdapter class, in which you inflate views with created layout and populate listview with childs. Then in your main you set the new adapter to the list.
The ArrayAdapter should look like this:
public class AudiobookAdapter extends ArrayAdapter<Audiobook> {
// constructor
// here you populate each view and put it in listview
public View getView (int position, View convertView, ViewGroup parent)
{
}
}
This is a start line, you should read more about extending arrayadapters and custom ListViews.

Android: replace values of a cursor/sql query

In my SQL database table there are colums _id, int dayofweek, String text ...
I now, do a SQL query on that table to get a cursor to the results.
The results then are shown in a listView:
private void showBlocks()
{
Cursor blocks = ttDB.getBlocks();
startManagingCursor(blocks);
int[] key = new int []{
android.R.id.text1,
android.R.id.text2
};
SimpleCursorAdapter blockAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_2,
blocks,
new String[] {
"day",
"text"
},
key);
lv1.setAdapter(blockAdapter);
}
How (and where) can I now replace the integer values of dayofweek by the corresponding strings? I thought about joining two tables while querying but then it will not be multilingual any more.
You can try using a ViewBinder via setViewBinder() on your SimpleCursorAdapter.
Or, extend SimpleCursorAdapter, override bindView() and do it yourself.
you can not directly replace values .you have to store first it in list and than you can replace it with ur desired fromat

Categories