I have a list activity that has a TextView and EditText field per row. Previously I was using simplecursoradapter but to get around the problem of getting values from EditText and using in other activities. As well as recycling moving user inputs around I'm trying to move to a custom one. I've got it to run but when it does it does not display any of my results. At this point its just showing my footer at the bottom.
// custom adapter
public class ItemsAdapter extends ArrayAdapter<Model> {
private LayoutInflater mInflater;
private Cursor cursor;
public ItemsAdapter(Context context, int layout, Cursor cursor, String[] from,
int[] to) {
super(context, layout);
this.cursor = cursor;
mInflater = LayoutInflater.from(context);
}
static class ViewHolder {
protected TextView text;
protected EditText edittext;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.edit_row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.textType);
holder.edittext = (EditText) convertView.findViewById(R.id.editText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
cursor.moveToPosition(position);
int label_index = cursor.getColumnIndex("userword");
String label = cursor.getString(label_index);
holder.text.setText(label);
return convertView;
}
}
//My list activity
public class editview extends ListActivity {
private dbadapter mydbhelper;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mydbhelper = new dbadapter(this);
mydbhelper.open();
View footer = getLayoutInflater().inflate(R.layout.footer_layout, null);
ListView listView = getListView();
listView.addFooterView(footer);
showResults();
}
private void showResults (){
Cursor cursor = mydbhelper.getUserWord();
startManagingCursor(cursor);
String[] from = new String[] {dbadapter.KEY_USERWORD};
int[] to = new int[] {R.id.textType};
ItemsAdapter adapter = new ItemsAdapter(this, R.layout.edit_row, cursor,
from, to);
adapter.notifyDataSetChanged();
this.setListAdapter(adapter);
}
//footer button
public void onClick(View footer){
final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50);
editClickSound.start();
}
}
//My cursor located in dbadapter class
// retrieves all the descriptions for the edittext fields
public Cursor getUserWord()
{
return myDataBase.query(USER_WORD_TABLE, new String[] {
KEY_ID,
KEY_CATEGORY,
KEY_SOURCE, KEY_TITLE, KEY_USERWORD, KEY_QUICK
},
KEY_CATEGORY+ "=" + categories.categoryClick + " AND " + KEY_SOURCE+ "="
+source.sourceClick + " AND " + KEY_TITLE+ "=" + title.titleClick,
null, null, null, KEY_ID);
}
To give more info on how this works. User will got through a series of Listviews, category->source->title. Then brings them to this activity with the EditText where I will take their inputs and use them in the next activity. I'm pretty new to the custom cursor adapter since I'm new to android and still learning so I'm having trouble understanding what I'm missing.
Related
I am creting music player app and I got music files from device but it is not in shorted order.
It is displaying like this (Not in sorted order)
Code which displays songs in ListView :
public class songlist extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ListView lv_songlist;
public Cursor cursor;
private MediaCursorAdapter mediaAdapter = null;
private String currentFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songlist);
lv_songlist = (ListView) findViewById(R.id.songlist);
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (null != cursor) {
cursor.moveToFirst();
mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
}
}
private class MediaCursorAdapter extends SimpleCursorAdapter {
public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,
new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
new int[]{R.id.displayname, R.id.title, R.id.duration});
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView name = (TextView) view.findViewById(R.id.displayname);
TextView duration = (TextView) view.findViewById(R.id.duration);
name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));
title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
long durationInMs = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
Duration d = new Duration();
String durationInMin = d.convertDuration(durationInMs);
duration.setText("" + durationInMin);
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listitem, parent, false);
bindView(v, context, cursor);
return v;
}
}
}
Create string like this :
private String sortOrder = MediaStore.MediaColumns.DISPLAY_NAME+"";
And pass this string to last argument in contentResolver.query method's last parameter :
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, sortOrder);
I've an ListView with data from SQLite that works correctly. Now I want to add more things to this list. To each row I want to add EditText and CheckBox. Something like this:
item1 CheckBox
EditText
item2 CheckBox
EditText
This is my List Class:
public class SeleccionarRelacionPregResp extends Activity implements, OnItemClickListener {
private ListView listaTodo;
private ListAdapter uGraduateListAdapter;
private String bundledCodigoPuntoDeControl;
private ArrayList<UndergraduateDetailsPojo> pojoArrayList;
protected DokesimApplication app;
String codigocaja;
Button btnPruebasGuardar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.seleccionartodo);
listaTodo = (ListView) findViewById(R.id.ListaTodo);
listaTodo.setOnItemClickListener(this);
pojoArrayList = new ArrayList<UndergraduateDetailsPojo>();
uGraduateListAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, populateList());
listaTodo.setAdapter(uGraduateListAdapter);
PuntosDeControl pControlSeleccionado = new PuntosDeControl();
app = (DokesimApplication) getApplicationContext();
pControlSeleccionado = app.getpuntocontrol();
codigocaja = pControlSeleccionado.codigocaja;
}
public List<String> populateList() {
List<String> uGraduateNamesList = new ArrayList<String>();
AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);
SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
Cursor cursor = sqliteDatabase.query("RelacionPregResp", null,
" relprcodigocaja = '" + codigocaja + "'",
null, null, null, null);
startManagingCursor(cursor);
while (cursor.moveToNext()) {
String numeropregunta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.numeropregunta));
String relprcodigocaja = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.relprcodigocaja));
String codigopregunta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.codigopregunta));
String pregunta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.pregunta));
String codigorespuesta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.codigorespuesta));
String respuesta = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.respuesta));
String valor = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.valor));
String tipodeguia = cursor.getString(cursor
.getColumnIndex(AndroidOpenDbHelper.tipodeguia));
UndergraduateDetailsPojo ugPojoClass = new UndergraduateDetailsPojo();
ugPojoClass.setNumeroPregunta(numeropregunta);
ugPojoClass.setRelPrCodigoCaja(relprcodigocaja);
ugPojoClass.setCodigoPregunta(codigopregunta);
ugPojoClass.setPregunta(pregunta);
ugPojoClass.setCodigoRespuesta(codigorespuesta);
ugPojoClass.setRespuesta(respuesta);
ugPojoClass.setValor(valor);
ugPojoClass.setTipoDeGuia(tipodeguia);
pojoArrayList.add(ugPojoClass);
uGraduateNamesList.add(pregunta);
}
sqliteDatabase.close();
return uGraduateNamesList;
}
#Override
protected void onResume() {
super.onResume();
uGraduateListAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, populateList());
listaTodo.setAdapter(uGraduateListAdapter);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
I need some suggestions or examples of how can I do it.
Thanks for any help.
What you basically need is Custom SimpleCursorAdapter.
I am pasting sample program which has Two TextViews in each list row.
You will have to modify it according to your requirement.
package org.sample;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyAdapter extends SimpleCursorAdapter
{
private Context context;
private Cursor cursor;
private int layout;
public MyAdapter(Context context, int layout, Cursor cursor, String[] from,
int[] to, int flags)
{
super(context, layout, cursor, from, to, flags);
this.context = context;
this.cursor = cursor;
this.layout = layout;
}
#Override
public int getCount()
{
if (cursor != null)
return cursor.getCount();
else
return 0;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view == null)
{
view = inflater.inflate(layout, null);
}
cursor.moveToPosition(position);
String title = cursor.getString(cursor
.getColumnIndex(DBHelper.BOOK_TITLE));
String author = cursor.getString(cursor
.getColumnIndex(DBHelper.BOOK_AUTHOR));
TextView titleTxt = (TextView) view.findViewById(R.id.bTitle);
TextView authorTxt = (TextView) view.findViewById(R.id.bAuthor);
titleTxt.setText(title);
authorTxt.setText(author);
return view;
}
}
Once you are ready with your cursor in MainActivity you can write following code.
myAdapter = new MyAdapter(context, R.layout.row, cursor, new String[]{ DBHelper.BOOK_TITLE, DBHelper.BOOK_AUTHOR }, new int[]{ R.id.bTitle, R.id.bAuthor }, 0);
listView.setAdapter(myAdapter);
You have to create your own class that extends from adapter what you need for example SimpleCursorAdapter, then specific with XML file, which elements will be included to your ListView and override getView() method to controling each item in ListView.
It's recommended to create and use design pattern Holder, which represents arbitrary object that holds child widgets of every row and then you will have full control over items.
So let basic example:
Declaration and init of ListView
this.contactList = new ListView(this);
this.contactList = (ListView) findViewById(R.id.contactList);
this.contactList.setAdapter(new ContactsAdapter());
This ContactsAdapter is your own created adapter.
public ContactsAdapter() {
super(getApplicationContext(),
R.layout.listview, cursor, new String[] {"name", "email", "phone"}, new int[] {R.id.name, R.id.email});
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
rowsWrapper = null;
LayoutInflater inflater = getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview, null, false);
rowsWrapper = new ListWidgetWrapper(convertView);
convertView.setTag(rowsWrapper);
}
else {
rowsWrapper = (ListWidgetWrapper) convertView.getTag();
}
Cursor tempC = cursor;
tempC.moveToPosition(position);
setActivitySettings();
rowsWrapper.getNameColumn().setText(tempC.getString(1));
rowsWrapper.getEmailColumn().setText(tempC.getString(2));
tempC = null;
return convertView;
}
}
rowsWrapper is mentioned design pattern Holder.
public class ListWidgetWrapper {
private View inView;
private TextView nameColumn = null, emailColumn = null, phoneColumn = null;
public ListWidgetWrapper(View inView) {
this.inView = inView;
}
public int getNameId() {
return inView.findViewById(R.id.name).getId();
}
public int getEmailId() {
return inView.findViewById(R.id.email).getId();
}
public TextView getNameColumn() {
if (this.nameColumn == null) {
this.nameColumn = (TextView) inView.findViewById(R.id.name);
}
return this.nameColumn;
}
public TextView getEmailColumn() {
if (this.emailColumn == null) {
this.emailColumn = (TextView) inView.findViewById(R.id.email);
}
return this.emailColumn;
}
}
So i have here TextView elemets, you have here widgets which you want have in ListView.
This require to write much more implementation but it's more faster, less energy consume when you don't use for example design pattern Holder because without it, your rows in ListView would be creating repeatedly when won't be in View and later go back.
More about 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?...
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...
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;
}
});