I have a database with a "flagname" field. What I want to do is display a listview with the relevant flag in the image view that corresponds with the database field, along side a text field. The text field from the database is being displayed ok, but I'm having trouble displaying the relevant flag beside it. Can someone please let me know where I'm going wrong. My code is listed below:
public class favourites extends Activity{
Cursor cursor;
ListView lv;
ImageView iv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listmain);
iv = (ImageView)findViewById(R.id.imgFlag);
DBAdapter db = new DBAdapter(this);
db.open();
lv = (ListView)findViewById(R.id.list);
//Get cursor for list items
cursor = db.getAllRadioStations();
//assign fields to columns
String[] columns = new String[]{DBAdapter.KEY_STATION_NAME.toString()};
int[] to = new int[]{R.id.txtFlag};
startManagingCursor(cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.flagchoice, cursor, columns, to);
lv.setAdapter(adapter);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
What you need to do is override the Adapter.setViewBinder(). Try this:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.flagchoice, cursor, columns, to);
adapter.setViewBinder(new ViewBinder() {
public boolean setViewValue(View aView, Cursor aCursor, int aColumnIndex) {
String country=aCursor.getString(aColumnIndex);
int id = getResources().getIdentifier("yourpackagename:drawable/" + country , null, null);
iv = (ImageView)findViewById(R.id.imgFlag);
iv.setImageDrawable(getResources().getDrawable(id));
TextView tv=(TextView) aView;
tv.setText(aCursor.getString(aColumnIndex));
return true;
}
});
lv.setAdapter(adapter);
Related
My ListView currently is able to display only one column data. I want it to display two column data.
Here is the code for my ListView:
public class ProjectExplorer extends ListActivity {
private projectdatabase database;
protected Cursor cursor;
protected ListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
database = new projectdatabase(ProjectExplorer.this);
getinfo();
}
private void getinfo() {
database.open();
cursor = database.getDataforDisplay();
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, new String[] {"project_name"}, new int[] { android.R.id.text1 }, 0);
setListAdapter(adapter);
}
#Override
public void onListItemClick(ListView parent, View view, int position, long id) {
super.onListItemClick(parent, view, position, id);
Cursor c = ((SimpleCursorAdapter)parent.getAdapter()).getCursor();
c.moveToPosition(position);
// get project name here
String str_projectname= c.getString(c.getColumnIndex("project_name"));
Toast.makeText(this, str_projectname, Toast.LENGTH_LONG).show();
}
Here is the method in database class which return the cursor:
public Cursor getDataforDisplay () {
String[] columns = new String[] {KEY_ROWID, PROJECT_NAME, PROJECT_FINISH_DATE, PROJECT_DIFFICULTY, PROJECT_STATUS};
Cursor c = projectDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
projectDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
c.moveToFirst();
return c;
}
I also want to display KEY_ROWID which is defined as _id
You can use android.R.layout.simple_list_item_2 specifying column no.2 from table from where data can be retrieved and specifies location with help of it can be displayed on Activity - android.R.id.text2
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, new String[] {"project_name","column_no2"}, new int[] { android.R.id.text1, andriod.R.id.text2 }, 0);
You can also create your on custom Adapter specifying your own Listview and creating it from scratch and customized everything.
The best way to achieve this at least in my opinion is to create custom Adapter for your ListView in that case you can set your own design how single listview element should look like . Just populate some arrays using your Cursor and set them to your custom adapter.
I'm trying to display a ListView of all contacts and enable the user to select multiple records. I want to use ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE) but I'm not sure how to apply it here. The code below pulls up a list of contacts but I can't select from it.
Any tips very much appreciated
Cheers
public class addContacts extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_contacts);
Uri allContacts = Uri.parse("content://contacts/people");
Cursor c = managedQuery(allContacts, null, null, null, null);
String[] columns = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID };
int[] views = new int[] { R.id.contactName, R.id.contactID };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.add_contacts, c, columns, views);
this.setListAdapter(adapter);
}
}
Since you are using a ListActivity you can fetch the ListView with getListView(), use:
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
But I noticed that you are using the layouts incorrectly:
setContentView(R.layout.add_contacts);
...
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.add_contacts, c, columns, views);
Understand that:
The layout passed to setContentView() should have a ListView with the id android:id="#android:id/list".
The row layout passed to SimpleCursorAdapter should never have a ListView... You are trying to create a ListView with ListViews on every row.
Try using built in layouts like this:
public class addContacts extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri allContacts = Uri.parse("content://contacts/people");
Cursor c = managedQuery(allContacts, null, null, null, null);
String[] columns = new String[] {
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID };
int[] views = new int[] { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, c, columns, views);
this.setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
}
I am using database in my application,in that i have fetched data from database,but i dont know how to display it in the textview.
Here is my code:
super.onCreate(savedInstanceState);
setContentView(R.layout.report);
db.open();
Cursor c = db.getAllTitles();
startManagingCursor(c);
String[] from = new String[] { db.KEY_INCOME };
int[] to = new int[] { R.id.textView1 };
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.report, c, from, to);
Here the notes have the database value.But don't know how to proceed how to display it in listview.
do like this
String[] titles= new String[] {c.TITLE };
int[] view = new int[] { R.id.textview };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.list_example_entry, c, titles, view );
listview.setAdapter(notes);
You need to find your listview and set your adapter. (Assuming you are not using a ListActivity)
ListView list = (ListView) this.findViewById(R.id.yourlistview);
list.setAdapter(notes);
If you are using a ListActivity (or ListFragment), it's even simpler as the framework assumes you are using a certain id and you can just set the adapter like so:
setListAdapter(notes);
if you want to set set Tesxt on list item click you can do iy like
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor c = (Cursor) arg0.getAdapter().getItem(arg2);
tv.setTextt(c.getString(1));
}
I want to be able to fetch a row from a list view and populate textViews in a different activity. I want to be able to do this onclick of an item with the list view..
public class CBFilter extends Activity {
ListView RecipeNames;
Cursor cursor;
SimpleCursorAdapter adapter;
CBDataBaseHelper data;
SQLiteDatabase data2;
TextView RecipeText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RecipeNames = (ListView) findViewById(R.id.List1);
RecipeNames.setTextFilterEnabled(true);
RecipeText = (TextView) findViewById(R.id.recipeText);
adapter = new SimpleCursorAdapter (this, 0, cursor, null, null);
data = new CBDataBaseHelper(this);
data.open();
cursor = data.query();
startManagingCursor(cursor);
String[] from = {CBDataBaseHelper.KEY_NAME};
int[] to = { R.id.recipeText};
adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
RecipeNames.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public void CreateNew(View view){
Intent myIntent = new Intent(this, CBCreate.class);
startActivity(myIntent);
}
}
How can i convert this code in order to achieve the required functionality.. thanks Stefan
try like this
public void onListItemClick(ListView parent, View v, int position, long id) {
String string = from[position]); // this depends on your Adapter ...
Intent i = new Intent(CheckData.this,ShowData.class);
i.putExtra("SELECTED", string);
startActivity(i);
i got it from this link , it will be use full for you :
Use of SQLite
I have a problem displaying an records from my db in listview. This is my code
public class FirstTab extends ListActivity {
private DatabaseHelper dbhelper;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.first);
dbhelper = new DatabaseHelper(getApplicationContext());
Cursor cursor = dbhelper.getAllNotes();
startManagingCursor(cursor);
String[] columns = new String[] {DatabaseHelper.colTitle , DatabaseHelper.colDate};
int[] to = new int[] { android.R.id.text1, android.R.id.text2};
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);
setListAdapter(mAdapter);
}
}
and
...
public Cursor getAllNotes()
{
SQLiteDatabase db=this.getReadableDatabase();
return db.query(noteTable, new String [] {colTitle, colDesc, colDate}, null, null, null, null, null);
}
...
if you need more , here is repo https://github.com/grzegorz-l/myHomeworks
When i start my app it crash at the beggining (RuntimeException). If I comment 2 last lines in onCreate method it run but ofc doesn't show anything.
Thanks in advance
Greg
Don't pass getApplicationContext() when creating SimpleCursorAdapter. Use this instead, i.e., the context of your ListActivity.
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);
Your FirstTab.java file is extending a ListActivity. This means that it's layout file has to contain a ListView with android:id set to:
android:id="#android:id/list"
Also, your FirstTab.java has it's layout pointing to note_entry.xml. It should either point to a layout which has a ListView of above description in it or not be extended by a ListActivity.