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);
}
}
Related
So I have a database being displayed within a listView. When I delete an item, I'm trying to get the list to update and remove the item. I have all the code and am receiving no errors. I just doesn't refresh the list.
Here's what I have:
public void Requery() {
db.open();
final Cursor c1 = DBAdapter.getAllRecords();
startManagingCursor(c1);
String[] from = new String[] { DBAdapter.KEY_ITEM };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row,
c1, from, to);
setListAdapter(notes);
c1.getCursor().requery(); //trying to refresh list
db.close();
}
I have also tried:
public void Requery() {
db.open();
final Cursor c1 = DBAdapter.getAllRecords();
startManagingCursor(c1);
String[] from = new String[] { DBAdapter.KEY_ITEM };
int[] to = new int[] { R.id.text1 };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row,
c1, from, to);
setListAdapter(notes);
notes.notifyDataSetChanged(); //another way that still didn't work
db.close();
}
Am I missing something here?
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 have created a listview to show all the contents of a database on a screen. the code i am using is this
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = datasource.getAllActs();
startManagingCursor(c);
String[] from = new String[] {DataBaseHelper.KEY_DATE,
DataBaseHelper.KEY_STEPS,DataBaseHelper.KEY_CALs };
int[] to = { R.id.code, R.id.Days, R.id.BMI };
SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
which worked beofre with the code below.
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = datasource.getAllGoals();
startManagingCursor(c);
String[] from = new String[] {DataBaseHelper.KEY_GOAL, DataBaseHelper.KEY_Current,DataBaseHelper.KEY_Target };
int[] to = { R.id.code, R.id.Days, R.id.BMI };
SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
i'm just wondering if there is something wrong with this code that i may be missing. i have the logcat if you need it
The only thing I can think of is that your new Cursor doesn't have a column named _id. Make sure that the cursor,
Cursor c = datasource.getAllActs();
has a column named _id, as the SimpleCursorAdapter class requires it.
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);
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.