How to customize TextViews inside SimpleCursorAdapter - Android - android

I was wanted to customize my textviews in SimpleCursorAdapter can someone help me to do that in a easiest way where too many code changes is not required...
Below is my working code where i am fetching strings from a class and inserting in SimpleCursorAdapter
code :
public class MyListActivity extends ListActivity {
/** Called when the activity is first created. */
private Cursor mCursor = null;
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weeklysettings);
getListView().addHeaderView(buildHeader(), null , false);
Cursor mCursor = getCursorDetails();
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
R.layout.listview, // Specify the row template
// to use (here, three
// columns bound to the
// two retrieved cursor
// rows).
mCursor, // Pass in the cursor to bind to.
// Array of cursor columns to bind to.
new String[] { MyClass.EVENT,
MyClass.CHANNEL_NAME,
MyClass.PROGRAM_TITLE,
MyClass.EVENTTIME },
new int[] { R.id.event, R.id.channelname, R.id.programtitle, R.id.timestamp});
// Bind to our new adapter.
setListAdapter(adapter);
//setListAdapter(new ArrayAdapter<String>(this, R.layout.listview, ynetList));
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
stopManagingCursor(mCursor);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
startManagingCursor(mCursor);
}
private ViewGroup buildHeader() {
LayoutInflater infalter = getLayoutInflater();
ViewGroup header = (ViewGroup) infalter.inflate(R.layout.listitem_header, getListView(), false);
header.setEnabled(false);
return(header);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent(this, SettingsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
finish();
super.onBackPressed();
}
private Cursor getCursorDetails() {
String sortOrder = DataExchangeFacility.TIMESTAMP
+ " COLLATE LOCALIZED DESC";
mCursor = getApplicationContext().getContentResolver().query(
DataExchangeFacility.CONTENT_URI_GRACE, null, null, null, sortOrder);
return mCursor;
}
}

Give this a shot:
ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
R.layout.listview, // Specify the row template
// to use (here, three
// columns bound to the
// two retrieved cursor
// rows).
mCursor, // Pass in the cursor to bind to.
// Array of cursor columns to bind to.
new String[] { MyClass.EVENT,
MyClass.CHANNEL_NAME,
MyClass.PROGRAM_TITLE,
MyClass.EVENTTIME },
new int[] { R.id.event, R.id.channelname, R.id.programtitle, R.id.timestamp}) {
#Override
public void setViewText(TextView v, String text) {
// v is your TextView
v.setTextColor(Color.RED);
super.setViewText(v, text);
}
};

Related

retriveing data from exisint sqlite database

I wish to store it in arraylist and then display it out in listView. Is there any solution to it ? how do I set the adapter? how can I link this two up and display it into a listview using arrayList?
DBhelper.java
public ArrayList<String> getDataarray(){
SQLiteQueryBuilder querybuilder=new SQLiteQueryBuilder();
querybuilder.setTables(DATABASE_TABLES);
String [] columns= new String[]{ KEY_ROWID,KEY_USERNAME,KEY_AGE,KEY_ClINIC,KEY_PHONE,KEY_EMAIL,KEY_DATESIGNUP
,KEY_CONDITIONID,KEY_DOCTORID,KEY_LOGINID,KEY_ACTIVITYNAME,KEY_NOTIFICATIONNAME,KEY_GROUPNAME,KEY_APPROVED
};
Cursor c= ourdatabase.query(DATABASE_TABLES, columns, null,null, null, null, null);
String result="";
ArrayList<String> resultarray = new ArrayList<String>();
int iRow=c.getColumnIndex(KEY_ROWID);
int iUserName=c.getColumnIndex(KEY_USERNAME);
int iAge=c.getColumnIndex(KEY_AGE);
int iClinic=c.getColumnIndex(KEY_ClINIC);
int iPhone=c.getColumnIndex(KEY_PHONE);
int iEmail=c.getColumnIndex(KEY_EMAIL);
int iDateSignup=c.getColumnIndex(KEY_DATESIGNUP);
int iConditionID=c.getColumnIndex(KEY_CONDITIONID);
int iDoctorID=c.getColumnIndex(KEY_DOCTORID);
int iLoginID=c.getColumnIndex(KEY_LOGINID);
int iActivityName=c.getColumnIndex(KEY_ACTIVITYNAME);
int iNotificationName=c.getColumnIndex(KEY_NOTIFICATIONNAME);
int iGroupName=c.getColumnIndex(KEY_GROUPNAME);
int iApproved=c.getColumnIndex(KEY_APPROVED);
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT value FROM " + DATABASE_TABLES, null);
if(cursor.moveToFirst()) {
do {
resultarray.add(cursor.getString(cursor.getColumnIndex("value")));
}while(cursor.moveToNext());
}
cursor.close();
db.close();
return resultarray;
}
ProfileFragment.java this is the page where i want to retrieve out at the listView how can i retrieve it from the database instead of array
public View getView( int position ,View convertView,ViewGroup parent){
LayoutInflater inflater= (LayoutInflater) context.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
View row =inflater.inflate(R.layout.profile_list,parent,false);
TextView myProfile=(TextView)row.findViewById(R.id.Profile);
TextView myCondition=(TextView)row.findViewById(R.id.condition);
myProfile.setText(ProfileArray[position]);
myCondition.setText(resultarray[position]);
return row;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.user,container,false);
Resources res=getResources();
Profile=res.getStringArray(R.array.Profile);
//how can i change the line below to let it get the data from the database array? or is there any other method?
condition=res.getStringArray(R.array.Profile_values);
list.setAdapter(adapter);
list=(ListView)rootview.findViewById(R.id.listView1);
return rootview;
}
Do you want to display all of data?
Think about using CursorLoader with SimpleCursorAdapter. It will load your data once activity is created in background. And in your activity class just implement interface LoaderManager.LoaderCallback, and in onCreate you simply initialize Loader with getSupportLoaderManager().initLoader(LOADER_ID, null, this)
Here is the link with good example http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html
EDIT:
This code use in onCreate
// init DB
db = new DB(this);
db.open();
// forming columns from DB to views
String[] from = new String[] { KEY_ROWID, KEY_USERNAME, KEY_AGE...}; //array of columns from DB
int[] to = new int[] { R.id.textviewId, R.id.textviewUserName, R.id.textviewAge }; //Array of of view components
// create adapter
scAdapter = new SimpleCursorAdapter(this, R.layout.item, null, from, to, 0);
ListView lvData = (ListView) findViewById(R.id.lvData); // listview where you want to display data
lvData.setAdapter(scAdapter);
//init loader
getSupportLoaderManager().initLoader(0, null, this);
//implement these mothods with interface `LoaderManager.LoaderCallback<Cursor>`
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle bndl) {
return new MyCursorLoader(this, db);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
scAdapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
}
This code must works for you.
EDIT2
static class MyCursorLoader extends CursorLoader {
DB db;
public MyCursorLoader(Context context, DB db) {
super(context);
this.db = db;
}
#Override
public Cursor loadInBackground() {
return db.getAllData();
}
}
One detail added. This is a class where you download your data from db and return filled cursor.

how to refresh the listView using the Cursor Adapter

I have created a ListView using CursorAdapter . Now I am Trying to update the ListView and Refresh the value to the ListView .
But I am not able to figure out . How to work with Loader or changeCursor() to refresh my ListView
Below is My code of setting the CursorAdapter :
//SucessFully done here
SQLDataSore datastore = new SQLDataSore(PrintContent.this);
Cursor cursor = datastore.getJSONData();
final CursorDemo cursorDemo = new CursorDemo(PrintContent.this, cursor);
list_View.setAdapter(cursorDemo);
My Button onClick I am updating the Value into the Database
//SucessFully Done
btn_check.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View view ) {
String editTextValue = edit_check.getText().toString();
if (editTextValue!=null) {
SQLDataSore sqlDataSore = new SQLDataSore(PrintContent.this);
Cursor cursor_update = sqlDataSore.updateData(editTextValue);
//Here How Should I update my ListView ...?
}
}
My UpdateData Method:
public Cursor updateData(String editContent){
SQLiteDatabase updateContent = getReadableDatabase();
Cursor cursor_update = updateContent.rawQuery( "update " +TABLE_NAME + " set content = '"+ editContent
+"' "+" where _id = 357", null);
return cursor_update;
}
CursorDemo Class
public class CursorDemo extends CursorAdapter{
public CursorDemo(Context context, Cursor c) {
super(context, c , false);
// TODO Auto-generated constructor stub
}
#Override
public void changeCursor(Cursor cursor) {
// TODO Auto-generated method stub
super.changeCursor(cursor);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
TextView txt_content = (TextView) view.findViewById(R.id.txt_content);
TextView txt_likes_count = (TextView) view.findViewById(R.id.txt_likescount);
TextView txt_name = (TextView) view.findViewById(R.id.txt_name);
TextView txt_display_name = (TextView) view.findViewById(R.id.txt_display_name);
txt_content.setText(cursor.getString(cursor.getColumnIndex("content")));
}
#Override
public View newView(Context context , Cursor cursor, ViewGroup viewGroup) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.message_row_view, viewGroup ,false);
return view;
}
}
Any Help is Appreciated...
});
If CursorDemo extends CursorAdapter, then you have to use adapter.swapCursor(cursor_update);
That should swap the old cursor out for the new one and reload the data. With swapCursor, the old cursor is not closed.
In your CursorDemo you have to owerwrite changeCursor() method and reset the Cursor if you have indexer you have to set it's cursor too.
#Override
public void changeCursor(Cursor cursor) {
mIndexer.setCursor(cursor);
super.changeCursor(cursor);
}
public void changeCursor (Cursor cursor)
Added in API level 1 Change the underlying cursor to a new cursor. If
there is an existing cursor it will be closed.
Parameters cursor The new cursor to be used
Also try for below method if it's apt for your requirement.
Set a FilterQueryProviderand pass your key to that filter.
final Cursor oldCursor = adapter.getCursor();
adapter.setFilterQueryProvider(myQueryProvider);
adapter.getFilter().filter(editTextValue, new FilterListener() {
public void onFilterComplete(int count) {
// assuming your activity manages the Cursor
// (which is a recommended way)
stopManagingCursor(oldCursor);
final Cursor newCursor = adapter.getCursor();
startManagingCursor(newCursor);
// safely close the oldCursor
if (oldCursor != null && !oldCursor.isClosed()) {
oldCursor.close();
}
}
});
private FilterQueryProvider myQueryProvider = new FilterQueryProvider() {
public Cursor runQuery(CharSequence searchKey) {
// assuming you have your custom DBHelper instance
// ready to execute the DB request
return sqlDataSore.updateData(searchKey);;
}
};
PS : The Cursor must include a column named _id or this class will not work see this.
btn_check.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View view ) {
String editTextValue = edit_check.getText().toString();
if (editTextValue!=null) {
SQLDataSore sqlDataSore = new SQLDataSore(PrintContent.this);
Cursor cursor_update = sqlDataSore.updateData(editTextValue);
cursorDemo.swapCursor(cursor_update);
//or cursorDemo=new CursorDemo(this,cursor_update);
list_View.setAdapter(cursorDemo);
}
}
Put this on the activity where declare the ListView . Just create a new adapter and put it in new cursor then recreate it or swap the cursor. make sure your listview or adapter not constant.
you could call
adapter.notifyDataSetChanged();
".java.lang.IllegalArgumentException: column '_id' does not exist ...althouh I have _id field in to my db table" means that the value of "cursor" in your code is wrong.
Check the code of getting the value of "cursor" please. A cursor must have a column named '_id'。
This is my suggestion
If you want to adapt/replace new cursor value to your list view , you should remove the old cursor from adapter and add new cursor value to the adapter.And finally adapt this adapter to listview using listview.setadapter(CursorAdapter) as follows.
liveTagListCursor = ctx.getContentResolver().query(
LiveTagProvider.TAG_LIST_URI, null, null, null, null);
tagCursorAdapter = new LiveTagListCursorAdapter(getActivity(),
liveTagListCursor);
tagCursorAdapter.swapCursor(liveTagListCursor);
listview.setAdapter(tagCursorAdapter);

Add listener to ListView of contacts?

I need some help to add an onItemClickListener to a ListView of contacts. I want to capture some information from the selected contacts like name, phonenumbers and other info and pass it to another activity.
I'm not sure how to add a onListItemListener to my code below and how to capture info from the selected contact? I also wonder how to pass more than one value with an Intent? I use this for one value:
Intent i = new Intent(Activity_1.this, Activity_2.class);
startActivityForResult(i, 1);
Intent intent = new Intent();
intent.putExtra("imageId", imagePath);
setResult(RESULT_OK, intent);
finish();
And here are my code in the activity with the ListView. I got some help here yesterday and I wonder if the while (cursor.moveToNext()){} part of the code is important for the ListView since it works without it?
Preciate some help! Thanks!
public class Activity_3 extends Activity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_3);
listView=(ListView)findViewById(R.id.contactList);
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone._ID};
// Get a cursor with all people
Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,null,null, null);
//Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,null,null, null);
String[] fromColumn = {ContactsContract.Contacts.DISPLAY_NAME};
int[] toView = {R.id.contactItem };
while (cursor.moveToNext())
{
String Name=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String Number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor, fromColumn, toView );
listView.setAdapter(adapter);
}
}
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view , int position,
long id) {
// TODO Auto-generated method stub
do your operations here...
}
});
use this onitemclicklistener to listen to your listview clicks .

NullPointerException when I run this code

So in this code, I am trying to retrieve a cursor based on the query passed through my showResults() and then create an adapter and loadermanager. I feel like the problem is being caused with the layout and id within my SimpleCursorAdapter constructor because the error started when I created the layout and id. I used Log and if statement to see whether it was the cursor that was null and nothing showed up on the logcat, so that must mean by cursor is fine.
public class SearchResultsActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private ListView list;
private DatabaseTable db;
private SimpleCursorAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_results);
db = new DatabaseTable(this);
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
db = new DatabaseTable(this);
Cursor cursor = db.getContactMatches(query, null);
list = (ListView)findViewById(android.R.id.list);
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
null, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
getLoaderManager().initLoader(0, null, this);
list.setAdapter(mAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent contactIntent = new Intent(getApplicationContext(), ContactActivity.class);
contactIntent.setData(getIntent().getData());
startActivity(contactIntent);
}
});
}
Since your code is crashing on the line list.setAdapter(mAdapter);, list is the only object that it makes sense to give you a null pointer.
Upon further examination, it is clear you are not assigning list after it is initially declared. You need to add something like this in onCreate(), after setContentView():
list = (ListView) findViewById(android.R.id.list);
(This is how you'd access it if your object had android:id="#android:id/list"; if you assigned your own ID, use R.id.your_id_here instead.)
this is probably your error here:
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
null, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
that adapter requires a Cursor passed into the constructor at the spot where you passed null in for the parameter, like this:
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1,
cursor, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
Try getting a readable database first before you query it:
db = new DatabaseTable(this);
Cursor cursor = db.getReadableDatabase().getContactMatches(query, null);

Multiple Loaders with LoaderManager, not getting right Loader

I have two loaders, one to populate bind returned data to 2 TextViews and another to populate a ListView. How do I make sure the correct loader loads for each situation? I am getting an error in the where first loader (WR_VIEW case) doesn't seem to be created or loading so in the onLoadFinished() it returns a "No such column found error" because it is accessing the second loader which doesn't call that column.
Here in my onCreate method I set up the adapter for the list view:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.workouts_options);
String[] uiBindFrom = { ExerciseTable.COLUMN_NAME };
int[] uiBindTo = { R.id.text1 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(),
R.layout.exercises_row,
null,
uiBindFrom,
uiBindTo,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
final ListView lv = (ListView) findViewById(android.R.id.list);
lv.setAdapter(adapter);
lv.setEmptyView(findViewById(android.R.id.empty));
lv.setClickable(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
}
});
getSupportLoaderManager().initLoader(WR_VIEW, null, this);
getSupportLoaderManager().initLoader(EXERCISE_VIEW, null, this);
}
Create my 2 different CursorLoaders:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
switch(id) {
case WR_VIEW:
String[] projection = { WorkoutRoutineTable.COLUMN_ID, WorkoutRoutineTable.COLUMN_NAME, WorkoutRoutineTable.COLUMN_DESCRIPTION };
return new CursorLoader(this, Workouts.buildWorkoutIdUri(""+mRowId), projection, null, null, null);
default:
String[] columns = {ExercisesColumns.NAME, WRExercisesColumns.COLUMN_ID };
return new CursorLoader(this, Workouts.buildWorkoutIdExerciseUri(""+mRowId), columns, null, null, null);
}
}
Bind data to my TextViews here and swapCursor for the ListView:
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
int id = loader.getId();
switch(id) {
case WR_VIEW:
if (cursor != null && cursor.moveToFirst()) {
mNameText.setText(cursor.getString(cursor.getColumnIndexOrThrow(WorkoutRoutineTable.COLUMN_NAME)));
mDescriptionText.setText(cursor.getString(cursor.getColumnIndexOrThrow(WorkoutRoutineTable.COLUMN_DESCRIPTION)));
getSupportActionBar().setTitle(mNameText.getText());
if (!TextUtils.isEmpty(mDescriptionText.getText())) {
getSupportActionBar().setSubtitle(mDescriptionText.getText());
}
cursor.close();
}
default:
adapter.swapCursor(cursor);
}
}
Resetting the Loader:
public void onLoaderReset(Loader<Cursor> loader) {
int id = loader.getId();
switch(id) {
case WR_VIEW:
break;
default:
adapter.swapCursor(null);
break;
}
}
In your onLoadFinished() you don't have a break after WR_VIEW case, so the default clause is getting run as well, passing the wrong cursor to your adapter. (onCreateLoader seems to have a similar issue as well)

Categories

Resources