The immediate answer is no, this is not possible in Java. I understand that, and this is a bit of a noob question, but i've been coding this way for some time now and i've always sort of felt it was a dirty method so i'd like to clarify what I am doing. Say for instance I have an Activity that is extending the Activity class so I can use onCreate etc etc...within that Activity, I have a SimpleCursorAdapter that populates a ListView.
String sql = "SELECT * FROM myTable";
Cursor data = database.rawQuery(sql, null);
String fields[] = {"field1", "field2", "field3"};
adapter = new CustomCursorAdapter(this, R.layout.custom_row, data, fields, new int[] { R.id.Field1, R.id.Field2, R.id.Field2 });
list.setAdapter(adapter);
I have named this CustomCursorAdapter because I am creating an entirely new class called CustomCursorAdapter that extends SimpleCursorAdapter so I can use methods such as bindView, newView etc to use buttons on my ListView objects.
public class CustomCursorAdapter extends SimpleCursorAdapter {
private Context myContext;
private myActivity parentActivity;
private Button delButton;
public CustomCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
myContext = context;
parentActivity = (myActivity) myContext;
#Override
public void bindView(View view, Context context, Cursor cursor) {
int idColumn = cursor.getColumnIndex("_id");
final int getId = cursor.getInt(idColumn);
final double increment = 0.25;
UnitsConversions convert = new UnitsConversions();
int nameColumn = cursor.getColumnIndex("name");
String getName = cursor.getString(nameColumn);
TextView name = (TextView)view.findViewById(R.id.GrainName);
name.setText(getName);
delButton = (Button)view.findViewById(R.id.DeleteButton);
delButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
parentActivity.deleteItem(getId);
}
});
#Override
public View newView(Context context, Cursor cursor, final ViewGroup parent) {
View view = View.inflate(context, R.layout.list_item, null);
return view;
}
I have scaled my code down a lot here just to use as an example so if I removed anything that makes this non working code I appologize, but non-working code isn't the purpose of my question. My question is, is this the only way to override methods unless I extend the activity itself with a SimpleCursorAdapter? It doesn't seem like a big deal, but when I have 10 different activities, all doing basically the same thing but with different ListViews and items and I have to create 10 different CustomCursorAdapter's it seems dirty and redundant. Maybe there is a way to create only 1 other activity and then pass in the items I need? It seems like it would be a lot cleaner to just use SimpleCursorAdapter instead of having to create a custom one and override the methods I need right in the Activity. If I didn't name this question properly, please feel free to edit.
Yes, there is a nicer way. You can create a subclass of Activity (and declare it abstract if you like) which implements overrided versions of every method in which you share code across your 10 classes. Then for each real Activity you need, extend that subclass of Activity and just make the changes you need to each method. If a method is different in each class but shares most of its code with the other Activities, then you can call the general super.method() and then apply the specific changes.
Related
I am trying to change only one (maybe more) ListView row based on specific condition. I have read many answers on similar questions and tons of other tutorials but I am not able to make anything.
Exactly what I want to achieve is to have row background (easier version) or row picture (I think harder version) set different than others when row from SQLite is set at specific value.
I have got Activity that extends ListActivity and I am setting the ListView adapter like this:
private void refreshList() {
mySQLiteAdapter = new MyDBAdapter(this);
mySQLiteAdapter.open();
String[] columns = { MyDBAdapter.KEY_TITLE, MyDBAdapter.KEY_GENRE,
MyDBAdapter.KEY_PRICE, MyDBAdapter.KEY_ID };
Cursor contentRead = mySQLiteAdapter.getAllEntries(false, columns,
null, null, null, null, MyDBAdapter.KEY_TITLE, null);
SimpleCursorAdapter adapterCursor = new SimpleCursorAdapter(this,
R.layout.row, contentRead, columns, new int[] {
R.id.text1, R.id.detail });
this.setListAdapter(adapterCursor);
mySQLiteAdapter.close();
}
This function is called in onCreate method and in onResume. I want to set different color/image of row where value from column MyDBAdapter.KEY_PRICE is equal to 5. The R.layout.row is my xml file with row design.
Maybe someone can help me with this? Or at least show tutorial describing it?
Simply extend SimpleCursorAdapter and override bindView():
public class MyAdapter extends SimpleCursorAdapter {
public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
if(cursor.getLong(cursor.getColumnIndex(MyDBAdapter.KEY_PRICE)) == 5)
view.setBackgroundColor(0xff00ff00);
else // this will be the default background color: transparent
view.setBackgroundColor(0x00000000);
}
}
You can try to create your custom adapter that extends on SimpleCursorAdapter and inside that, on the method bindView, you can look if the condition you look for is accomplished and make what you want on that method.
http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#bindView(android.view.View, android.content.Context, android.database.Cursor)
Hope to help :)
I'm writing a fairly complex ListView, which (among other things) requires formatting Views in each list item.
To give me full control over how the views are bound in each list item, I subclassed CursorAdapter in this manner:
public class MyAdapter extends CursorAdapter {
public final LayoutInflater mInflater;
public MyAdapter(Context context, Cursor c) {
super(context, c);
mInflater = LayoutInflater.from(context);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
final ToggleButton tButton = (ToggleButton) view.findViewById(R.id.tbutton);
tButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// start activity based on a pending intent
}
});
}
}
The issue is that my ToggleButton click listener should start an activity based on a pending intent. The pending intent is instantiated in the activity which utilises this customized adapter.
I'm aware I could have used a SimpleCursorAdapter in the main Activity with a ViewBinder so that launching the intent would only be necessary from the main Activity. But SimpleCursorAdapter is not quite right since I don't map columns straight to views.
However, the alternative I have here would suggest accessing the main Activity's data from a cursor subclass. I feel that there must be a better way to design the application.
Taking a cue from the API Demos - specifically EfficientAdapter, I have declared the CursorAdapter sublcass as an inner class of my activity.
This avoids passing the pending intent around outside of the main activity.
Source: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html
I'm trying to implement a CursorAdapter in my Android App. I read different tutorials and tried out different things but it won't work!
I found one question here very similar to mine but I didn't get the idea out of it.
Here's the thing:
I got a Database with multiple tables and foreign keys and so on. I wrote a (tested and working) Database including an
extension of the SQLiteHelper class to work properly. The DB-Class containts a lot of methods to get various operations
on that DB like: insert, update, delete, alter and some special needs....
My Problem is the following:
I have written a method which returns all Data containing (all rows) in the Database which i want to present
in a ListView. The returning object is a wrapped ArrayList> . I got the thing working all fine
with an ListViewAdapter but then i came to my problem which now almost drives me crazy:
The User shall click a random item in that specific list (which contains all rows from the DB) and then i want to
update that specific row in the DB with some new data the user put into a EditText box. Pretty simple task actually but I think I just don't understand the usage of the CursorAdapter.
My questions now:
What does my DB-Method have to return (or look alike) that the CursorAdapter can handle it....
How do I have to implement the Adapter that it just fulfill this one task (retrieving the correct rowID of the DB entry in the list)
Here is my method retrieving the data from the DB
public ArrayList<ArrayList<Object>> getAll()
{
ArrayList<ArrayList<Object>> allRows = new ArrayList<ArrayList<Object>>();
//Cursorobjekt haelt die Daten einer Zeile und dient dazu in diesen zu iterieren
Cursor myCursor1,myCursor2,myCursor3;
myCursor1 = db.query
( "Faecher",
new String[] { "id" , FACH_NAME, FACH_ART },
null, null, null, null, null
);
// Den Pointer an die erste Stelle ruecken
myCursor1.moveToFirst();
myCursor2 = db.query
(
"Ort",
new String[]{"id" , ORT_RAUM , ORT_GEBAEUDE},
null,null,null,null,null,null
);
myCursor2.moveToFirst();
myCursor3 = db.query
(
"Profs",
new String[]{"id", PROFS_NAME, PROFS_SNAME, FKEY_GENDER_ID},
null,null,null,null,null,null
);
myCursor3.moveToFirst();
for(int i=0; i < myCursor1.getCount(); i++)
{
ArrayList<Object> row1 = new ArrayList<Object>();
row1.add(myCursor1.getLong(0));
row1.add(myCursor1.getString(1));
row1.add(myCursor1.getString(2));
row1.add(myCursor2.getLong(0));
row1.add(myCursor2.getString(1));
row1.add(myCursor2.getString(2));
row1.add(myCursor3.getLong(0));
row1.add(myCursor3.getString(1));
row1.add(myCursor3.getString(2));
row1.add(myCursor3.getLong(3));
allRows.add(row1);
myCursor1.moveToNext();
myCursor2.moveToNext();
myCursor3.moveToNext();
}
myCursor1.close();
myCursor2.close();
myCursor3.close();
return allRows;
}
The Adapter is empty right now because my code was real crap and now looks like this hull:
public class SubjectListAdapter extends CursorAdapter
{
private LayoutInflater myInflater;
public SubjectListAdapter(Context context, Cursor c)
{
super(context, c);
}
#Override
public View newView(Context context, Cursor myCursor, ViewGroup parent)
{
return null;
}
#Override
public void bindView(View parent, Context context, Cursor myCursor)
{
}
}
I hope someone can help me out with my problem or give me a hint where I have to go to get this working.
If I understand your question correctly, it seems like your primary issue is determining which element has been clicked on within the adapter. You may need to switch from bindview and newview to just using getview. If you do, you can just add an onClicklistener to the view before getView returns it. This way, each row in the list has its own listener and can update the correct row in the db. Any particular information that the onClickListener needs to run the correct database update method (ie the rowId) can also be passed into the listener with a Tag on the view. Hope that helps.
You have to create one big query from DB and get one Cursor. You can use even ordinary sql selection like
String selectQuery = "SELECT id, " FACH_NAME", "+ FACH_NAME ", " + FACH_ART .... +" FROM Faecher INNER JOIN ....";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
to get requared cursor. Than create custom CursorAdapter with getted cursor
public class ExampleCursorAdapter extends CursorAdapter {
public ExampleCursorAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView something1 = (TextView)view.findViewById(R.id.something1);
something1.setText(cusror.getString(0));
TextView something2 = (TextView)view.findViewById(R.id.something2);
something1.setText(cusror.getString(1));
.......
viev.addOnClickListener(new OnClickListener{
public void onClick(...){
}
});
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.item, parent, false);
return v;
}
}
Method bindView(....) will be called for each raw of Cursor but it will be setted to the different position, you souldnt change it inside this method. Than just bind this adapter to your ListView by setAdapter(....) method.
I have an SQLite database of items where some of them contain basic HTML such as b and sub tags. I've bound the table to a ListView using a SimpleCursorAdapter. Is there a way to make the ListView format the HTML tags so it displays properly?
It seems like the way forward is to get the Cursor to deliver SpannedStrings but I can't work out how to do that.
Thanks Ian, this is my final adapter code:
private class HtmlCursorAdapter extends SimpleCursorAdapter {
public HtmlCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
#Override
public void setViewText (TextView view, String text) {
view.setText(Html.fromHtml(text),BufferType.SPANNABLE);
}
}
If your data contains only simple HTML tags, they can actually be handled by a TextView by using Html.fromHtml(yourString). That static method returns a Spanned, which can be displayed by a TextView with far less overhead than a WebView.
You could use the CursorWrapper class.
CursorWrapper cw = new CursorWrapper(myCursor) {
public String getString(int columnIndex) {
String withHTML = super.getString(columnIndex);
return Html.fromHtml(withHTML).toString();
}
};
I have list of checkboxes in list binded by Custom simpleCursorAdapter.
In my custom simpleCursorAdapter, I've overridden newView and bindView with my modifications.
I've managed somehow to do multichoice.
The wierd thing is, after I delete any item from my list, the first item's checkbox is being checked all of a sudden. How does that happen? How can I solve it?
My SimpleCursorAdapter class:
public class MyListCursorAdapter extends SimpleCursorAdapter
{
private Context context;
private int layout;
public MyCursorAdapter(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);
CheckBox chkBoxBtn = (CheckBox) v.findViewById (R.id.deleteTwittChkBox);
if (chkBoxBtn != null)
{
chkBoxBtn.setChecked(false);
}
return v;
}
#Override
public void bindView(View v, Context context, Cursor c)
{
--binding view to my textsview in my items
//now it's the importat part:
CheckBox chkBoxBtn = (CheckBox) v.findViewById(R.id.deleteTwittChkBox);
if (chkBoxBtn != null)
{
chkBoxBtn.setId(Integer.valueOf(c.getString(c
.getColumnIndex(MyUsers.User._ID))));
chkBoxBtn.setOnClickListener(new OnItemClickListener(chkBoxBtn, v));
chkBoxBtn.setChecked(false);
}
}
//i couldnt find another way of doing this, but this is how i set listeners to my checkboxses
static ArrayList<String> checkedItemsList = new ArrayList<String>();
private class OnItemClickListener implements OnClickListener
{
private int mPosition;
private CheckBox chkBox;
OnItemClickListener(CheckBox mChkBox, View v)
{
chkBox = mChkBox;
chkBox.setChecked(false);
}
#Override
public void onClick(View v)
{
if (chkBox.isChecked())
{
checkedItemsList.add(String.valueOf(chkBox.getId()));
}
else
{
checkedItemsList.remove(String.valueOf(chkBox.getId()));
}
}
}
}
Here is the code part from the ListActivity class which describes the button that deletes the checked box items:
OnClickListener btListener = new OnClickListener()
{
public void onClick(View view)
{
// long[] items = listView.getCheckItemIds();
int x = 0;
Uri myUri = Uri
.parse("content://com.idan.datastorageprovider/users");
String where = "_id" + "=?";
//here i am tatking all checkboxes which ive added from the adapter class
ArrayList<String> checkedItemsList = MySimpleCursorAdapter.checkedItemsList;
for (String itemID : checkedItemsList)
{
getContentResolver()
.delete(myUri, where, new String[] { itemID});
checkedItemsList.remove(itemID);
}
}
};
I doubt that SimpleCursorAdapter is the right class to extend here.
Is the "checked" state connected to the data XML in any way? No? So you need your own custom adapter!
Basically all adapters have to implement a way to generate a view from a given element (more precisely an element position!). This will be called at any time where the list wants to display an element. Now, the trick it uses is to re-use formerly created list view elements that cannot be seen on screen any more! Thus: when you scroll your list down and an element disappears at the top, EXACTLY this view object will be re-used for the next appearing item.
So, when this method is called with a given "old" view that should be re-used, all contained elements will have to be set according the elements data. If a checkbox is part of this game, you will have to have a storage for the checked state! It is not sufficient to have a checkbox as there will be less checkbox objects as there are list elements!
SimpleCursorAdapters are there to - yeah - represent SIMPLE things. An XML describing data (images and text, as the documentation states). Because of this simplicity all you have to do here is provide a method to create NEW element view objects - you are not intercepting the re-use process AT ALL! It basically only knows how to put the data into an existing view object - but it is lacking the knowledge of how to handle checked/unchecked boxes!
Your solution: write your own BaseAdapter extension and do what has to be done: implement "getView" (and some other methods like getItem, getItemId and getCount). It's not hard at all!
This API Demo uses a BaseAdapter and the mExpanded state here is basically identical to your checkbox states!
Good luck!
You might need to call notifyDataSetChanged when you modify the data.
The problem is probably that you're calling setChecked from within the onItemClickListener. One hacky way around this is to do the following before and after you call setChecked from within your listener:
chkBox.setClickable(false);
chkBox.setChecked(false);
checkBox.setClickable(true);
This will prevent your onItemClickListener from getting called when you manually call setChecked.