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);
}
}
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?
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
}
}
});
I have a ListView with android:choiceMode="multipleChoice". I fill this ListView from a Cursor through a SimpleCursorAdapter. Is there really no way to directly bind the "CheckBox" of the ListView's CheckedTextView layout to a boolean value from the cursor?
Currently I loop through the cursor calling ListView.setItemChecked() if the value is true:
private void showMyData(long myId) {
// fill the list
String[] fromColumns = { "myTextColumn" };
int[] toViews = { android.R.id.text1 };
Cursor myCursor = _myData.readData(myId);
CursorAdapter myAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_multiple_choice,
myCursor, fromColumns, toViews);
ListView myListView = (ListView) findViewById(R.id.myListView);
myListView.setAdapter(myAdapter);
// mark items that include the object specified by myId
int myBooleanColumnPosition = myCursor
.getColumnIndex("myBooleanColumn");
for (int i = 0; i < myCursor.getCount(); i++) {
myCursor.moveToPosition(i);
if (myCursor.getInt(myBooleanColumnPosition ) == 1) {
myListView.setItemChecked(i, true);
}
}
}
That does the job. But I would like to have code like this:
String[] fromColumns = { "myTextColumn", "myBooleanColumn" };
int[] toViews = { android.R.id.text1, android.R.id.Xyz };
and have no loop. Am I missing something here or is it Android?
EDIT:
I tried this as suggested by Luksprog:
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
CheckedTextView ctv = (CheckedTextView) view;
ctv.setText(cursor.getString(cursor
.getColumnIndex("myTextColumn")));
if (cursor.getInt(cursor.getColumnIndex("myBooleanColumn")) == 1) {
ctv.setChecked(true);
Log.d("MY_TAG", "CheckBox checked");
}
return true;
}
That logged checking the CheckBox but didn't actually do it. Maybe that's a bug on my side. And while it's even more complicated than the initial loop at least it feels like one is using the framework, not working against it. So thank you Luksprog for the answer.
But to sum it up: Android is actually missing the straight forward approach.
Use a SimpleCursorAdapter.ViewBinder on your adapter. Make sure your Cursor has the boolean values column in it and then:
String[] fromColumns = { "myTextColumn" };
int[] toViews = { android.R.id.text1 };
Cursor myCursor = _myData.readData(myId);
CursorAdapter myAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, myCursor, fromColumns, toViews);
myAdapter.setViewBinder(new ViewBinder() {
public boolean setViewValue (View view, Cursor cursor, int columnIndex) {
// set the text of the list row, the view parameter (simple use cursor.getString(columnIndex))
// set the CheckBox status(the layout used in the adapter is a CheckedTextView)
return true;
}
});
I have written the code to get the values from the database and bind that values to listview.For that I have used the customized listview now according to requirement I want the checkbox for my each item of the list.How to do that
image=(ImageView)findViewById(R.id.image);
note=(ImageButton)findViewById(R.id.note);
tick=(ImageButton)findViewById(R.id.tick);
cross=(ImageButton)findViewById(R.id.cross);
Intent intent = getIntent();
Bitmap photo = (Bitmap) intent.getParcelableExtra("photooo");
image.setImageBitmap(photo);
if(photo!=null)
{
dbHelper = new RecordsDbAdapter(this);
dbHelper.open();
displayListView();
}
}
private void displayListView() {
Cursor cursor = dbHelper.fetchAllRecords();
String[] columns = new String[] {
RecordsDbAdapter.KEY_NAME,
RecordsDbAdapter.KEY_BIRTHDAY,
};
int[] to = new int[] {
R.id.name,
R.id.birthdate,
};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.row,
cursor,
columns,
to);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
}
}
using LayoutInflater add check box to list view
after using SetTag methode add Tagvalue to checkbox
This tagvalue separate checkbox in every item in listview
following code example:`
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.home1, null);
ViewHolder holder1 = new ViewHolder();
holder1.text = (TextView) convertView.findViewById(R.id.textView1);
holder1.ch=(CheckBox)convertView.findViewById(R.id.checkBox1);
holder1.ch .setTag(position);
like you had done before go to the XML file of R.layout.row and put this code:
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="checkbox" />
and then edit your code like this:--
private void displayListView() {
Cursor cursor = dbHelper.fetchAllRecords();
String[] columns = new String[] {
RecordsDbAdapter.KEY_NAME,
RecordsDbAdapter.KEY_BIRTHDAY,
RecordsDbAdapter.KEY_CHECKBOX
};
int[] to = new int[] {
R.id.name,
R.id.birthdate,
R.id.checkbox
};
dataAdapter = new SimpleCursorAdapter(
this, R.layout.row,
cursor,
columns,
to);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(dataAdapter);
}
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.