SimpleCursorAdapter - set 2 columns to 1 view - android

I have a SQLite Datebase from which I am displaying data in ListView by SimpleCursorAdapter. I have 2 columns but I want to displey them in 1 TextView. How can I do that? Thanks

Try This Code , it may help you
Method For calling Listview from Oncreate
private void populateListViewFromDB1()
{
Cursor cursor = helper1.getAllData(Sharedemail);
startManagingCursor(cursor);
String[] productname = new String[]
{
DataBaseHelper.KEY_NAMEVALUE,
DataBaseHelper.KEY_TYPE,
};
int[] viewproduct = new int[]
{
R.id.textView1,
};
// Create Adapter
MySimpleCursoradapter myCursorAdapter = new MySimpleCursoradapter
(this,
R.layout.listview,
cursor,
productname,
viewproduct);
ListView List = (ListView) findViewById(R.id.listView1);
List.setAdapter(myCursorAdapter);
}
}
MySimpleCursoradapter.java
public class MySimpleCursoradapter extends SimpleCursorAdapter {
public MySimpleCursoradapter(Context context, int layout,
Cursor cur, String[] from, int[] to) {
super(context, layout, cur, from, to);
}
#SuppressWarnings("static-access")
#Override
public View newView(Context con, Cursor c, ViewGroup arg2) {
LayoutInflater inflater = (LayoutInflater) con
.getSystemService(con.LAYOUT_INFLATER_SERVICE);
View retView = inflater.inflate(R.layout.listview, null);
return retView;
}
public void bindView(View v, Context context, Cursor c) {
String pname = c.getString(c.getColumnIndex(DataBaseHelper.KEY__NAMEVALUE));
String issuetype = c.getString(c.getColumnIndex(DataBaseHelper.KEY_TYPE));
TextView name_text = (TextView) v.findViewById(R.id.textView1);
name_text.setText(pname +":"+ issuetype);
}
}

Related

How can I set up button onclicklistner when using simple cursor adapter

Here is the code I am using to show a custom ListView using simple CursorAdapter
I am using this code to show cart items, and I want to add button in ListView
As I am new to android development I'm not able to figure out what the problem is
myDbHelper.openDataBase();
final Cursor ictemp = myDbHelper.getOrdredItems(myDbHelper);
if (ictemp != null) {
ictemp.moveToFirst();
count = ictemp.getCount();
Log.d("count", "count===" + count);
String[] from = new String[] { "item_name", "item_rate", "qty",
"unit" };
int[] to = new int[] { R.id.tv_Name, R.id.tv_Rate, R.id.et_qty,
R.id.tv_unit };
final SimpleCursorAdapter sc = new SimpleCursorAdapter(this,
R.layout.list_row2, ictemp, from, to, 0);
final Cursor crs = myDbHelper.getTotal(myDbHelper);
if (crs != null) {
crs.moveToFirst();
String total = crs.getString(0);
Gtotal.setText("Rs." + total);
tvcount.setText("" + count);
}
lv.setAdapter(sc);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
final int pos, long arg3) {
remove = (Button) arg0.findViewById(R.id.btn_remove);
switch (arg0.getId()) {
case R.id.btn_remove:
Toast.makeText(ctx, "Clicked on " + pos,
Toast.LENGTH_SHORT).show();
break;}
}
});
One solution is to create a CustomAdapter that extends SimpleCursorAdapter.
Then override bindView method
In bindView, find the Button then handle onClickEvent
public class CustomAdapter extends SimpleCursorAdapter {
private Context mContext;
private Context appContext;
private int layout;
private Cursor cr;
private final LayoutInflater inflater;
public CustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.mContext = context;
this.inflater = LayoutInflater.from(context);
this.cr = c;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(layout, null);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
...
TextView tv_Name = (TextView) view.findViewById(R.id.tv_Name);
tv_Name.setText(...);
...
Button btnRemove = (Button) view.findViewById(R.id.btn_remove);
btnRemove.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// button click
// Remove your item here
}
});
}
}
Use it in Activity by
final CustomAdapter sc = new CustomAdapter(this,R.layout.list_row2,ictemp, from, to, 0);
lv.setAdapter(sc)
Hope this helps

Issue with custom SimpleCursorAdapter

I'm trying to fill a grid view using data from a db cursor using a custom SimpleCursorAdapter.
My cursor has data (I checked), but nothing is shown in the GridView, and the getView() method is not even called.
Anybody can help? Why is getView() not called?
Thanks
Activity
dbAdapter = new DBAdapter(this);
dbAdapter.open();
Cursor c;
c = dbAdapter.fetchPCList();
startManagingCursor(c);
String[] from = new String[] {};
int[] to = new int[] {};
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new PCIconAdapter(this, R.layout.pc_icon, c, from, to));
c.close();
dbAdapter.close();
Adapter
public class PCIconAdapter extends SimpleCursorAdapter {
private final Context mContext;
private final int mLayout;
private final Cursor mCursor;
private final int mPCIDIndex;
private final int mClassNameIndex;
private final LayoutInflater mLayoutInflater;
private final class ViewHolder {
public TextView pc_id_view;
public TextView clas_name_view;
}
public PCIconAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.mContext = context;
this.mLayout = layout;
this.mCursor = c;
this.mPCIDIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_LM_ID);
this.mClassNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_CLAS_NAME);
this.mLayoutInflater = LayoutInflater.from(mContext);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mLayoutInflater.inflate(mLayout, null);
viewHolder = new ViewHolder();
viewHolder.pc_id_view = (TextView) convertView.findViewById(R.id.pc_id);
viewHolder.clas_name_view = (TextView) convertView.findViewById(R.id.clas_name);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) convertView.getTag();
}
String pc_id = mCursor.getString(mPCIDIndex);
String clas_name = mCursor.getString(mClassNameIndex);
viewHolder.pc_id_view.setText(pc_id);
viewHolder.clas_name_view.setText(clas_name);
}
return convertView;
}
}
I had the same problem. I was using an ArrayAdapter and then I changed to a SimpleCursorAdapter, but it doesn't show anything on screen.
I removed
c.close();
dbAdapter.close();
and now its working fine.
because CursorAdapter does not have getView method . it have newView method .
so extend SimpleCursorAdapter and overRide newWiev and other methods like
public class ContactListCursorAdapter extends SimpleCursorAdapter implements Filterable {
private Context context;
private int layout;
public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int nameCol = c.getColumnIndex(People.NAME);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
return v;
}
#Override
public void bindView(View v, Context context, Cursor c) {
int nameCol = c.getColumnIndex(People.NAME);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
}
#Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }
StringBuilder buffer = null;
String[] args = null;
if (constraint != null) {
buffer = new StringBuilder();
buffer.append("UPPER(");
buffer.append(People.NAME);
buffer.append(") GLOB ?");
args = new String[] { constraint.toString().toUpperCase() + "*" };
}
return context.getContentResolver().query(People.CONTENT_URI, null,
buffer == null ? null : buffer.toString(), args, People.NAME + " ASC");
}
}
Your column names array and Views array are empty. So column name to views mapping will never occur. That maybe the reason you are not getting callback on getView
If I remove
c.close();
dbAdapter.close();
it works...
So where am I supposed to close this cursor?...

notifyDataSetChanged does not get triggered even I called twice

I know that data is inserted in the database , and it is listed correctly but the notifydatasetchanges doesn't works , the list view doesn't get refreshed, so I have to restart my activity....
Cursor c = db.selectAll();
final ListView lv = (ListView) findViewById(R.id.listView1);
adapter=new MyContactsAdapter(this, c);
lv.setAdapter(adapter);
db.insertInTable("asd","asd");
//insertion in table works, I am sure, cause when I start the activity the new values are listed
//this the part that doesn't works
((MyContactsAdapter)lll.getAdapter()).notifyDataSetChanged();
adapter.notifyDataSetChanged();
lll.refreshDrawableState();
private class MyContactsAdapter extends CursorAdapter{
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
public MyContactsAdapter(Context context, Cursor cursor) {
super(context, cursor, true);
mInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView t = (TextView) view.findViewById(R.id.textView2);
t.setText(cursor.getString( 0 ));
t = (TextView) view.findViewById(R.id.TextView01);
t.setText(cursor.getString(1));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.item, parent, false);
return view;
}
}//adapter
Ok, something like, From your code,
Cursor c;
#Override
onCreate(.... {
setContentView(...);
final ListView lv = (ListView) findViewById(R.id.listView1);
db.insertInTable("asd","asd");
c = db.selectAll();
adapter=new MyContactsAdapter(this, c);
lv.setAdapter(adapter);
// some button click...
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
db.insertInTable("xyz","xyz");
c = db.selectAll();
adapter.notifyDataSetChanged();
}
});
Try this, Let see what happen...

Database not shown in ListView, only the image

This is the code that I use for my Listview which I got reference for some website. It shows a whole list of image. Which area do I have to place in my database to set the text for toptext and bottom text?
public class List_View extends ListActivity {
private DBAdapter db;
private TextView toptext;
private TextView bottomtext;
private ListView lv;
public void onCreate(Bundle savedInstanceState)
{
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
db = new DBAdapter(this);
toptext = (TextView) findViewById (R.id.toptext);
bottomtext = (TextView) findViewById (R.id.bottomtext);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> item;
for (int i = 0; i < 31; i++) {
item = new HashMap<String, String>();
item.put("Date:", "Saved Date" );
item.put("Title:", "Saved Title" );
list.add(item);
}
The SimpleAdapter part:
SimpleAdapter notes = new SimpleAdapter(this, list, R.layout.view_list,
new String[] { "date", "title" },
new int[] {R.id.toptext, R.id.bottomtext });
setListAdapter(notes);
} catch(Throwable e){
Log.e("DBAdapter",e.toString());
}
}
and this is the database part:
public Cursor getAllEntry()
{
return db.query(DATABASE_TABLE_2, new String[] {
KEY_ROWID2,
KEY_TITLE,
KEY_ENTRY,
KEY_MOOD,
KEY_DATE,
KEY_TIME},
null,
null,
null,
null,
null,
null);
}
some modification in your code...and you get result
first declare Cursor, Adapter class in your ListActivity class
private NCursorAdapter adapter = null;
private Cursor mNotesCursor;
mNotesCursor = db.getAllEntry();
startManagingCursor(mNotesCursor);
if (adapter == null) {
adapter = new NCursorAdapter(this, mNotesCursor);
setListAdapter(adapter);
} else {
adapter.changeCursor(mNotesCursor);
adapter.notifyDataSetChanged();
}
you must create NCursorAdapter class in your application.
public class NCursorAdapter extends CursorAdapter {
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
public NCursorAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView title = (TextView) view.findViewById(R.id.title);
title.setText(cursor.getString(cursor.getColumnIndex("title")));
TextView date = (TextView) view.findViewById(R.id.date);
date.setText(cursor.getString(cursor.getColumnIndex("date")));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
final View view = mInflater.inflate(R.layout.data, parent, false);
return view;
}
}
here data.xml file is your custom layout. which content two textview....
"title" and "date" your database table column name...

Retrieve checked CheckBoxes's items in Listview

I've got ListActivity and I am using custom CursorAdapter.
In each item of the list I've got also checkbox...
Now I have in my list screen a perm button, when you press on it,
It should find all the checkboxes which are 'checked' and do some operations on the item which it's checkbox is 'checked'.
How can I retrieve all the checked ones?
I've done focusable:false, so I can use OnClickListener, but I don't know how farther then
this..
Thanks,
some code:
This is in my ListActivity class:
final String columns[] = new String[] { MyUsers.User._ID,
MyUsers.User.MSG, MyUsers.User.LOCATION };
int[] to = new int[] { R.id.toptext, R.id.bottomtext,R.id.ChkBox,
R.id.Location};
Uri myUri = Uri.parse("content://com.idan.datastorageprovider/users");
Cursor cursor = getContentResolver().query(myUri, columns, null, null, null);
startManagingCursor(cursor);
ListCursorAdapter myCursorAdapter=new ListCursorAdapter(this,R.layout.listitem, cursor, columns, to);
this.setListAdapter(myCursorAdapter);
and this is my Custom Cursor adapter class:
public class ListCursorAdapter extends SimpleCursorAdapter
{
private Context context;
private int layout;
public ListCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to)
{
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
return v;
}
#Override
public void bindView(View v, Context context, Cursor c)
{
TextView topText = (TextView) v.findViewById(R.id.toptext);
if (topText != null)
{
topText.setText("");
}
int nameCol = c.getColumnIndex(MyUsers.User.MSG);
String name = c.getString(nameCol);
TextView buttomTxt = (TextView) v.findViewById(R.id.bottomtext);
if (buttomTxt != null)
{
buttomTxt.setText("Message: "+name);
}
nameCol = c.getColumnIndex(MyUsers.User.LOCATION);
name = c.getString(nameCol);
TextView location = (TextView) v.findViewById(R.id.Location);
if (locationLinkTxt != null)
{
locationLinkTxt.setText(name);
}
}
//Modify to meet your needs
String[] from = new String[]{ YourDbAdapter.KEY_WORDS_ROWID, YourDbAdapter.KEY_WORDS_ROWID};
int[] to = new int[]{ R.id.row_item_checkbox};
mAdapter = new SimpleCursorAdapter(this, R.layout.row_item_with_checkbox, yourDbCursor, from, to);
mAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(view.getId() == R.id.myCheckbox )
{
CheckBox cb = (CheckBox) view;
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final long id = cursor.getLong(columnIndex); //Your row id
//You can do the necessary work here such as adding to an List or somehting
}
});
return true;
}
return false;
}
});

Categories

Resources