I've tried all the examples in this site that talk about binding data to a TextView
but nothing solve my problem .
I have a dataBase, and TextView , I bind them together with a simple cursor adapter
MyDataBase mDB = new MyDataBase(this);
Cursor cursor = mDB.all(this);
String[] from = new String[] {mDB.VALUE};
int[] to = new int[] {R.id.text1View1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.main, cursor, from , to );
adapter.setViewBinder(new myViewBinder());
and here is the code of the myViewBinder
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
TextView txtWelcome = (TextView) view;
String name = cursor.getString(VALUE_ID);
txtWelcome.setText(name);
}
can any one help me to know why there's no change in the main activity when it's lunched?
what extra code I need to add ?
Try adding adapter.notifyDataSetChanged() after setting the adapter?
I believe for this to work you would have to be able to set the SimpleCursorAdapter as the adapter on the TextView but the TextView class doesn't support that operation out of the box.
Related
I've created a ListView and bind it to an adaptor to display data from a table. It works great. I can select on a row and it display the product name that I'm selecting. Now I've added an ImageView in the layout that will act as delete buttons on each row. My problem is I can't figure out how to add the code to make it so that when a user select the ImageView (delete button), it will delete the row. I've searched and found a lot of articles on this topic and tried a lot of them and none really work my code. Do I need to create a getView function? Also I've tried inserting the getTag(), but I couldn't make it work. Can you help me with a sample code that may work with my simple code or point me to the right direction? Here is my code:
private void displayListView() {
prodinputHelper = new DBAdaptorProductInput(this);
prodinputHelper.open();
Cursor cursor = prodinputHelper.fetchAllProductInput();
// The desired columns to be bound
String[] columns = new String[] {
DBAdaptorProductInput.KEY_PRODUCTTYPE,
DBAdaptorProductInput.KEY_PRODUCTNAME,
DBAdaptorProductInput.KEY_MANUFACTURER,
DBAdaptorProductInput.KEY_VISC40,
DBAdaptorProductInput.KEY_VISC100,
DBAdaptorProductInput.KEY_VI,
DBAdaptorProductInput.KEY_DEN15C,
DBAdaptorProductInput.KEY_VISCTEXT,
DBAdaptorProductInput.KEY_BASEOILTYPE,
DBAdaptorProductInput.KEY_BASEOIL,
DBAdaptorProductInput.KEY_ADDITIVES,
DBAdaptorProductInput.KEY_OTHERADDITIVES,
DBAdaptorProductInput.KEY_THICKENER,
DBAdaptorProductInput.KEY_NLGI,
DBAdaptorProductInput.KEY_COMMENT,
DBAdaptorProductInput.KEY_PACKAGES,
DBAdaptorProductInput.KEY_AREA,
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.code,
R.id.name,
R.id.manufacturer,
R.id.visc40,
R.id.visc100,
R.id.viscindex,
R.id.den15c,
R.id.visctext,
R.id.baseoiltype,
R.id.baseoil,
R.id.additives,
R.id.otheradditives,
R.id.thickener,
R.id.nlgi,
R.id.comments,
R.id.packages,
R.id.area,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.activity_product_review_info, cursor, columns, to, 0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
//SetOnItemClickListener for the ListView
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the Customer Name from this row in the database.
String countryCode = cursor.getString(cursor.getColumnIndexOrThrow("ProductName"));
Toast.makeText(getApplicationContext(), countryCode, Toast.LENGTH_SHORT).show();
}
});
}
You need to have a custom adapter that extends BaseAdapter or SimpleCursorAdapter.
In the adapter's getView() method set the onClickListener for your ImageView.
I have a listview that I want to change the font size of.
Of course, doing this works with some hardcoded data:
setListAdapter(new ArrayAdapter<SimpleCursorAdapter>(this, R.layout.list_view));
But I would like it to take my cursor, so I have
mContactList.setAdapter(adapter);
But in that case, I can't change my layout to be my list_view. Is there any way to go about this oro do I have to take my cursor data and put it into an array first.
This is where I have my adapter:
private void populateContactList() {
Cursor cursor = getContacts();
fields = new String[] { ContactsContract.Data.DISPLAY_NAME };
adapter = new SimpleCursorAdapter(this,
R.layout.entry, cursor, fields,
new int[] { R.id.contactEntryText });
mContactList.setAdapter(adapter);
}
EDIT: Got it. Turns out, I just needed to edit the contactEntryText TextView.
Maybe this will help you:
ListAdapter adapter = new SimpleCursorAdapter(
this,
android.R.layout.two_line_list_item,
mCursor,
new String[] {"String1", "String2"},
new int[] {android.R.id.text1, android.R.id.text2});
lv.setAdapter(adapter);
You could use a CursorAdapter.
for custom listviews you need to extend baseadapter link here
and make your own adapter where you can specify the layout xml to be used for each or all elements.
My cursor is fetch data from sqlite.
msgsdbadapter = new MSGSDBAdapter(this);
msgsdbadapter.open();
cons_cursor = msgsdbadapter.fetchConversations();
startManagingCursor(cons_cursor);
After that I create a SimpleCursorAdapter and assign it to ListView. The ListView now can display some records well.
String[] from = new String[] { MSGSDBAdapter.KEY_FROM, MSGSDBAdapter.KEY_MSG, MSGSDBAdapter.KEY_DATE};
int[] to = new int[] { R.id.lblFrom, R.id.lblMsgExcerpt, R.id.lblDate };
cons_cursor_adapter = new SimpleCursorAdapter(context, R.layout.conversation_item, cons_cursor, from, to);
lvConversations.setAdapter(cons_cursor_adapter);
Next, I insert new row into table and notify dataset changed, but the ListView is not update
msgsdbadapter.createMsg(msg);
cons_cursor_adapter.notifyDataSetChanged();
And when I should close the db connection?
You need to either requery() the existing Cursor, or run the query again to get a fresh Cursor and swap the new Cursor into your CursorAdapter.
The best solution if your using the ListFragment is to put this in inside this sample:
public SimpleCursorAdapter myNote;
public class NOteLIST_MAIN extends ListFragment {
#SuppressWarnings("deprecation")
#Override
public void onResume() {
super.onResume();
myNote.getCursor().requery();//Don't forget to put these on onResume }
private void DataLoader(){private void DataFiller(){
myNote = new SimpleCursorAdapter(getActivity(), R.layout.notes_row_line, notesCursor, from, to,0);
setListAdapter(myNote);}
}
this code is working fine - i'm loading a bunch of rows into the SimpleCursorAdapter and the ListView displays them. nice.
SimpleCursorAdapter adapter;
Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.main_lv_projects);
lv.setOnItemClickListener(this);
String[] columns = new String[] { CompassDataProvider.ORG_NAME };
int[] names = new int[] { R.id.organisation_row_name};
cursor = this.managedQuery(CompassDataProvider.CONTENT_URI_ORGANISATIONS, null, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.organisation_row, cursor, columns, names);
lv.setAdapter(adapter);
startManagingCursor(cursor);
}
But when i'm inserting a new row via
long rowID = db.getWritableDatabase().insert(CompassDBHelper.ORGS_TABLE_NAME, "", values);
getContext().getContentResolver().notifyChange(CONTENT_URI_ORGANISATIONS, null);
return Uri.withAppendedPath(CONTENT_URI_ORGANISATIONS, ""+rowID);
the list view is not updateing itself - i thought the SimpleCursorAdapter is notified an can then reload its view - not?
the new row is created - i checked on this
when i'm using
cursor.requery();
adapter.notifyDataSetChanged();
in my UI threat the new data gets loaded correctly... whats the observer/listener pattern here that i did not get? :)
thanks,
Martin
The cursor is being passed into the SimpleCursorAdapter by value and not reference. So, calling cursor.requery has no effect. To get this to work the way you want, you're really going to need to implement a listAdapter and process your cursor in there.
I have a main activity that takes elements from a database and displays them in a clickable listview. I use this method to accomplish the task:
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = RequestManager.getRequests(getApplicationContext());
startManagingCursor(c);
String[] from = new String[] { DataBase.KEY_TITLE, DataBase.KEY_BODY };
int[] to = new int[] { R.id.text1, R.id.text2 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
I am wondering, is it possible to access a boolean field inside the database, and bold the specific element is the field is marked as unread? The elements are each in a textview, and are then placed in a listview.
Thanks
Edit: Used the suggestion to extend the CursorAdapter Class, but when any element in the list is bolded, the first element is also bolded. Once all the elements are marked as read, the first element goes back to unbolded. Any ideas?
public void bindView(View view, Context context, Cursor cursor) {
TextView textRequestNo = (TextView) view.findViewById(R.id.text1);
TextView textMessage = (TextView) view.findViewById(R.id.text2);
StringBuilder requestNo = new StringBuilder(cursor.getString(cursor
.getColumnIndex("requestNo")));
StringBuilder message = new StringBuilder(cursor.getString(cursor
.getColumnIndex("Message")));
textRequestNo.setText(requestNo);
textMessage.setText(message);
if (cursor.getString(cursor.getColumnIndex("Read")).equals("false"))
{
textRequestNo.setTypeface(null, Typeface.BOLD);
textMessage.setTypeface(null, Typeface.BOLD);
}
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.notes_row, parent, false);
bindView(view, context, cursor);
return view;
}
Unfortunately I think you're going to have to implement your own CursorAdapter in order to achieve this functionality. It's not so bad though; you can make subclass of ResourceCursorAdapter, and then all you need to do is write your own implementation of bindView(). Inside of this implementation, you can read the Cursor to determine if you should bold the row or not.