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);
}
Related
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 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
}
}
});
Cursor c = managedQuery(People.CONTENT_URI,null,null,null,People.NAME);
String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,c,cols,views);
listview.setAdapter(adapter);
I'm using this code snippet to connect my ListView with Cursor.
I want to ask what
String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};
exactly does ??
and please explain about the arguments required for the constructor of SimpleCursorAdapter
It is a map, telling the adapter which columns from your cursor to use to fill which Widgets in your layout.
They get used in the order given. The data in the first column listed in the from array ( you called it cols ) will go into the first id listed in the to array ( you called it views), and so on.
The other parameters are the layout containing the view ids your specify in the to array and the cursor containing the data to be used in the array.
list_item.xml Refer this LINK
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
HelloListView.java
public class HelloListView extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, getNames()));
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
private ArrayList<String> getNames(){
ArrayList<String> namesArray = new ArrayList<String>();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
Cursor names = getContentResolver().query(uri, projection, null, null, null);
int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
names.moveToFirst();
do {
namesArray.add(names.getString(indexName));
} while (names.moveToNext());
return namesArray;
}
}
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.
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