I am developing a quiz application which contain both multiple answer type and single answer type.the question and answer are store in sqlite database.i use simple cursor adapter and get idea about how to use from here! My question is that how i can switch from simple_list_item_single_choice to simple_list_item_multiple_choice if my answer type changes(single to multiple) and also how can i save the answer choosen.Please give some idea about it.Here is the coding....
db = new DBAdapter(this);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
check this code: for single choice:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_single_choice,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
for multiple:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
simple_list_item_multiple_choice,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
depend on your requirement you have to set one of above: i think you aleady kno which question require single ans & which require multiple depend on that you have to also create list item click like
listView.setOnItemClickListener(new OnItemClickListener() {
private String my_sel_items;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
if(ans == multiple){
my_sel_items = new String("Selected Items");
SparseBooleanArray a = lView.getCheckedItemPositions();
for (int i = 0; i < a.size(); i++) {
if (a.valueAt(i)) {
my_sel_items = my_sel_items + ","
+ (String) listView.getAdapter().getItem(i);
}
}
Log.v("values", my_sel_items);
}else{
// for single it default selected item
}
}
});
Related
i have a list view filled with data coming from database, i want when i click on an item to
disappear, i used --list.remove(position)-- but i have an error add cast to list.
HERE the list view :
private void populateListViewFromDB() {
Cursor cursor = db.getAllRecords();
startManagingCursor(cursor);
String[] databaseColumnNames = new String[] { DBAdapter.col_Region };
int[] toViewIDs = new int[] { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter myCursordapter = new SimpleCursorAdapter(this,
android.R.layout.simple_expandable_list_item_1, cursor,
databaseColumnNames, toViewIDs, FLAG_REGISTER_CONTENT_OBSERVER);
final ListView list = (ListView) findViewById(android.R.id.list);
list.setAdapter(myCursordapter);
HERE my code when i want to delete an item :
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(final AdapterView<?> arg0, View arg1,
final int position, long arg3) {
list.remove(position);
adapter.notifyDataSetChanged();}
There is no ListView.remove() method. You should remove correspondent item from the data model and then refresh your adapter.
In your OnItemClickListener listener get the bound data item:
Object itemObj = adapter.getItem(position);
Then, delete this item from your DB.
Then get new instance of your cursor and update your cursor adapter:
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD){
adapter.swapCursor(newCurosr);
} else {
adapter.changeCursor(newCursor);
}
Then, notify your list view about data changes:
adapter.notifyDataSetChanged();
I have code which takes data from SQLite cursor and puts into listview.
private void displayListView() {
cursor = myDatabase.getJoinedInfo(etSearch.getText().toString().trim());
String[] columns = new String[] { "re_value", "g_value", "ke_value" };
int[] to = new int[] { R.id.tvHiragana, R.id.tvMeaning, R.id.tvKanji };
dataAdapter = new SimpleCursorAdapter(this, R.layout.wordonlist,
cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.lvWordlist);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
When EditText is empty, it is taking first 10 lines. How to show empty ListView when EditText is empty?
Do something like this :
If(yourEditText.length() == 0)
yourListView.setAdapter(null);
Or you can use :
If(yourEditText.length() == 0)
yourAdapter.clear();
do not set the adapter until EditText is empty. You can set the adapter after there's some data in EditText.
Try this
private void displayListView() {
if (etSearch.getText().toString().trim().equals("")) {
listView.setAdapter(null);
} else {
cursor = myDatabase.getJoinedInfo(etSearch.getText().toString()
.trim());
String[] columns = new String[] { "re_value", "g_value", "ke_value" };
int[] to = new int[] { R.id.tvHiragana, R.id.tvMeaning,
R.id.tvKanji };
dataAdapter = new SimpleCursorAdapter(this, R.layout.wordonlist,
cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.lvWordlist);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
}
}
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 have a ListActivity in which i have used a SimpleAdapter to create a list which has 2 fields. The pair of values are stored using a Map, and the list is an ArrayList.
I have an onItemClickListener for this. On selecting a list entry, i get the pair i.e. the 2 values. I need to get only 1 of those values.
For example, if the list item is "John", "123" . I want to store "123" in a string after selecting the entry from the list
Any help?
Here's the code snippet:
ListView lv = getListView();
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData(scanned_name, scanned_addr));
String[] from = { "name", "address" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(getApplicationContext(),
parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
});
You have to catch the content of android.R.id.text1 or android.R.id.text2. So, in your onItemClick(...) you have to do:
TextView v = (TextView)findViewById(R.id.text1);
TextView v1 = (TextView)findViewById(R.id.text2);
String content1=v.getText().toString();
String content2=v2.getText().toString();
and you can manage them as you want, even within a Toast:
Toast.makeText(getApplicationContext(),v.getText().toString(),Toast.LENGTH_LONG).show();
My android flash cards app has a bug that only occurs in ICS. The checkboxes behave like radiobuttons, like it was single-choice.
public class CardSetListActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.card_set_list);
ListAdapter adapter = getSimpleCursorAdapter();
// ListAdapter adapter = getArrayAdapter();
ListView list = (ListView) findViewById(R.id.listView);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setAdapter(adapter);
list.setItemsCanFocus(false);
}
private ListAdapter getSimpleCursorAdapter() {
CardsDbAdapter dbAdapter = CardsDbAdapter.getInstance(this);
Cursor cursor = dbAdapter.fetchAllCardSets();
startManagingCursor(cursor);
String[] fromColumns = new String[] { CardsDbAdapter.COL_CARD_SET };
int[] toResources = new int[] { android.R.id.text1 };
return new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, cursor, fromColumns, toResources);
}
private ListAdapter getArrayAdapter() {
String[] GENRES = new String[] { "Action", "Adventure", "Animation" };
return new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, GENRES);
}
}
Any idea where I could look for a solution?
With the SimpleCursorAdapter, the Checkboxes behave like RadioButtons. With the ArrayAdapter they behave right.
I'm changing to use ArrayAdapter now but would still like to know what's going on here.