how to split from [] value in simplecursoradapter in android - android

I have displaying value in list using simplecursoradapter. Nothing problem it. but how to split from [] value in simplecursoradapter in android
Means. I have in colummn this value -> John-44-2013
This value display in list. but i want to display John only.
String[] from = new String[] { column[0], column[1] };
int[] to = new int[] { R.id.colID, R.id.colDate };
TestCursorAdapter sca = new TestCursorAdapter (this,R.layout.gridrowsaveddetail, c,from, to);
public class TestCursorAdapter extends SimpleCursorAdapter {
String [] from;
int [] to;
public TestCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.from=from;
this.to=to;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title_of_list= (TextView) view.findViewById(R.id.colDate);
String temp=cursor.getString(cursor.getColumnIndex(from[1]));
String[] parse = temp.split("-");
title_of_list.setText(parse[0].trim());
super.bindView(view, context, cursor);
}
}
But not getting this. Why something wrong with this code. thank you advance.

Remove the call to super.BindView
This call will try to set the text itself, and is thereby overruling your actions.
Or, place the call at the start of the method.

Related

Populate ExpandableListView with data from SQLite

The setup: I have a local SQLite database holding showtimes for movies. Some of the columns are "Date", "ID", "Time"...
The intention:
I want to put all of these showtimes into a ExpandableListView while the dates are going to be the parent items.
The problem: Nothing is displayed. Nothing at all. And I struggle to understand why. Does anyone have an idea?
Some code: The most important stuff is up at the top, get's less and less important as you scroll down...
This is the code I'm trying to achieve this with which is called in the onCreate() of the Activity:
String sql = "SELECT rowid _id,* FROM " + Statics.DBSHOWS + ";";
Cursor movieCursor = database.rawQuery(sql,null);
movieCursor.moveToFirst();
adapter = new ExpandableListAdapter(movieCursor, this,
android.R.layout.simple_list_item_1,
android.R.layout.simple_list_item_2,
new String[]{Statics.DBSHOWS_DATE},
new int[]{android.R.id.text1},
new String[]{Statics.DBSHOWS_TIME, Statics.DBSHOWS_ID},
new int[]{android.R.id.text1, android.R.id.text2});
movieListView.setAdapter(adapter);
This is my ExpandableListAdapter:
public class ExpandableListAdapter extends SimpleCursorTreeAdapter {
Context context;
public ExpandableListAdapter(Cursor cursor, Context context, int groupLayout,
int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
this.context = context;
}
#Override
protected void bindChildView(View view, Context context, Cursor cursor, boolean isLastChild) {
super.bindChildView(view, context, cursor, isLastChild);
System.out.println("Dumping form Childview");
DatabaseUtils.dumpCurrentRow(cursor);
}
#Override
protected void bindGroupView(View view, Context context, Cursor cursor, boolean isExpanded) {
super.bindGroupView(view, context, cursor, isExpanded);
if(view==null){
System.out.println("View is null!!");
}
System.out.println("Dumping form Groupview");
DatabaseUtils.dumpCurrentRow(cursor);
}
#Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
int dateInMillis = groupCursor.getInt(groupCursor.getColumnIndex(Statics.DBSHOWS_DATE));
Cursor childCursor = DataHandler.getShowsForDate(dateInMillis);
childCursor.moveToFirst();
return childCursor;
}
}
The two Override Methods bindChildView(...) and bindGroupView(...) were made for testing. As expected, the following output was printed:
Dumping form Groupview
0 {
_id=1
Id=117451
Date=15.04.2016
Time=20:15
Movie_Id=2181
Hall=0
Cards_Sold=0
}
I have a love-hate relationship with our hobby/job: You have something working, at some point, something that seems totally independent changes, and then later your something is not working again. This is what happened here, and there was no way you could have guessed it:
With my Device updating to Marshmallow it could still read the ArrayList I used before but couldn't read the database anymore... Why it could still dump the cursor...? I don't know. But as soon as I figured out that you must request permissions on the go, and implemented that, it works... More or less anyway.

How to set background color through SimpleCursorAdapter

Here is my code:
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
String[] from = new String[]{NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_COLOR};
int[] to = new int[]{R.id.text1, R.id.text2};
SimpleCursorAdapter notes = new SimpleCursorAdapter(
this, R.layout.note_row, notesCursor, from, to);
setListAdapter(notes);
}
Which produces the following ListView: http://i.stack.imgur.com/7W1xa.jpg
What I want is to take "R.id.text2" value (which is a hex color) and set it as text color of itself, for each different row of my ListView.
The result should look like this: http://i.stack.imgur.com/wrXt8.jpg
Is that possible? Thanks.
Yes, it's possible. Create your own cursor adapter MyCursorAdapter which extend SimpleCursorAdapter, then override newView or bindView method.
public class MyCursorAdapter extends SimpleCursorAdapter {
public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
super.bindView(view, context, cursor);
TextView textView = (TextView) view.findViewById(R.id.text2);
String color = cursor.getString(cursor.getColumnIndex(NotesDbAdapter.KEY_COLOR));
textView.setBackgroundColor(Color.parseColor(color));
}
}
There's no need to create a new class. See this code.
#Override
public boolean setViewValue(View view, Cursor c, int columnIndex) {
if(columnIndex == c.getColumnIndexOrThrow(NotesDbAdapter.KEY_COLOR)) {
TextView tv = (TextView)view;
tv.setColor(Color.RED);
}
}

Set image in a ListView using SimpleCursorAdapter

I want to set image in my ListView, my intention is to take a string contain the address of the image (ex. "#drawable/image") from database and to set the images for the element of the LisView. I have already create a custom SimpleCursorAdapter to change the font.This is my custom SimpleCursorAdapter code:
public class CustomFontExampleAdapter extends SimpleCursorAdapter
{
private Typeface mCustomFont;
#SuppressWarnings("deprecation")
public CustomFontExampleAdapter(final Context context, final int layout, final Cursor c, final String[] from,
final int[] to) {
super(context, layout, c, from, to);
mCustomFont = Typeface.createFromAsset(context.getAssets(), "font/Pompiere-Regular.ttf");
}
#Override
public void bindView(final View view, final Context context, final Cursor cursor) {
super.bindView(view, context, cursor);
final TextView _TextViewTitle = (TextView) view.findViewById(R.id.record_nome);
_TextViewTitle.setTypeface(mCustomFont);
}
}
This is my code when i set the adapter:
d= new Database(getActivity());
final Cursor c = d.scelta();
CursorLoader(c);
String from[] = {Codice.DATI_ID,
Codice.DATI_NOME_DIETA};
int to[] = {R.id.record_id,
R.id.record_nome};
#SuppressWarnings("deprecation")
CustomFontExampleAdapter sca = new CustomFontExampleAdapter(rootView.getContext(),
R.layout.singolo_elemento,
c, from, to);
sceltadieta = (ListView) rootView.findViewById(R.id.scelta_dieta);
sceltadieta.setAdapter(sca);
after this line :
_TextViewTitle.setTypeface(mCustomFont);
Yous should go :
ImageView _ImageView = (ImageView) view.findViewById(R.id.your_image_id)
//Suppose you've already get your string(because it's not part of a question)
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier(yourStringName, "drawable", context.getPackageName()));
_ImageView.setImageDrawable(drawable)
Oh, and as far as i see you have no context in your adapter, putting context to adapter is common thing, you should past it throw constructor(as you did), and storing reference for it(create field and initialize it by context from constructor)

simple cursor adapter query

I have created a SimpleCursorAdapter. I would like a way to compare two rows.
lets say the columns are called realData pracData. If i want to compare two columns how would I write the statement to achieve this.
Below is my code for my SimpleCursorAdapter
db = new DBAdapter(this);
db.open();
// db.insertContact("how do you print hello world", "print 'hello world';", "print hello", "hello", 1);
// db.insertContact("what is x=2 and y=5", "5", "7", "2", 2);
db.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
db = new DBAdapter(this);
db.open();
Cursor cursor = db.getAllContacts();
startManagingCursor(cursor);
String[] from = new String[]{DBAdapter.question, DBAdapter.possibleAnsOne};
int[] to = new int[]{R.id.label, R.id.TextView01};
SimpleCursorAdapter cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
db.close();
This is from the ArrayAdapter
if (s.startsWith("how do you print hello world") || s.startsWith("iPhone")
|| s.startsWith("Solaris")) {
imageView.setImageResource(R.drawable.plus);
I want to do something similar but compare two rows in my database.
public class MyAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
// TODO Auto-generated constructor stub
}
#Override
public void bindView(View v, Context context, Cursor c) {
// do your magic inhere :) the cursor will be at the correct row position
System.out.println("please select a cursor");
// int nameCol = c.getColumnIndex(People.NAME);
// ListView listContent = (ListView)findViewById(R.id.contentlist);
// ImageView imageView = (ImageView)findViewById(R.id.ImageView01);
int nameCol = c.getColumnIndex(DBAdapter.question);
System.out.println("What is the column index" +nameCol);
}
}
Extending the SimpleCursorAdapter and overriding bindView should work. I usually extend CursorAdapter, but try something à la
public class MyAdapter extends SimpleCursorAdapter {
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView label = view.findViewById(R.id.yourid);
// and then do something with cursor.getString(columnindex);
}
}

Android: adding an extra String to TextView which populates its content from Database

I have a listview which populates its content from SQLite Database.
Here's my code:
ListView listView = (ListView) findViewById(R.id.lstText);
listView.setOnItemClickListener(this);
listView.setAdapter(new MySimpleCursorAdapter(this, R.layout.listitems,
managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
Database.Project.NAME), new String[] { BaseColumns._ID,
Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE}, null, null, null),
new String[] { Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE}, new int[] {
R.id.txt_title, R.id.image, R.id.txt_list_desc, R.id.txt_org}));
I want to put an extra String to some TextViews above when its displayed on the list. For example, I want to add a String with the word "from" on R.id.txt_org, before the populated String from the database which is Database.Project.C_ORGANIZATIONTITLE
Let's say the populated String is: New Organisation,
with an extra String "from" what will be displayed is: from New Organisation
Can anybody help me with that? Thank you very much.
EDITED:
FYI, this is my SimpleCursorAdapter method:
class MySimpleCursorAdapter extends SimpleCursorAdapter {
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
loader = new ImageLoader(context);
this.context = context;
}
Context context=null;
ImageLoader loader = null;
public void setViewImage(ImageView v, String value) {
v.setTag(value);
loader.DisplayImage(value, context, v);
}
}
Since you're already using a custom adapter, override the adapter's bindView() and newView() methods, rather than getView(). That way you will not have to manually deal with recycling the row's view.
Within these method you can get the data from the resulting Cursor and manipulate it before binding it to your row's view.
GetView Vs. BindView in a custom CursorAdapter?
How to override CursorAdapter bindView
//Edit: some more code below. Note that this is just a rough outline and by no means complete or tested.
class MySimpleCursorAdapter extends SimpleCursorAdapter {
private ImageLoader mLoader = null;
private LayoutInflater mInflater = null;
private int mBusinessNameIndex = -1;
private int mSmallImageIndex = -1;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
mLoader = new ImageLoader(context);
mInflater = getLayoutInflater();
mBusinessNameIndex = c.getColumnIndexOrThrow(Database.Project.NAME);
mSmallImageIndex = c.getColumnIndexOrThrow(Database.Project.C_SMALLIMAGE);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mInflater.inflate(R.layout.row, null);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Get your views from 'view'
TextView someTextView = (TextView) view.findViewById(R.id.xxx);
ImageView someImageView = (ImageView) view.findViewById(R.id.yyy);
// Set the data
someTextView.setText("from " + cursor.getString(mBusinessNameIndex));
mLoader.DisplayImage(cursor.getString(mSmallImageIndex ), context, someImageView);
}
}

Categories

Resources