I have a ChatActivity, which loads its data via a CursorLoader. The CursorLoader return a cursor with two registers, but the newView and bindView methods in adapter is never called.
My activity
public class ChatActivity extends BaseActivity implements LoaderManager.LoaderCallbacks<Cursor> {
public static final String EXTRA_AMANTEID = "amanteId";
private EditText messageET;
private ListView messagesContainer;
private Button sendBtn;
private ChatAdapter adapter;
private Long amanteId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
amanteId = getIntent().getLongExtra(ChatActivity.EXTRA_AMANTEID, 0L);
messagesContainer = (ListView) findViewById(R.id.messagesContainer);
messageET = (EditText) findViewById(R.id.messageEdit);
sendBtn = (Button) findViewById(R.id.chatSendButton);
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
adapter = new ChatAdapter(this);
getLoaderManager().initLoader(0, null, this);
messagesContainer.setAdapter(adapter);
}
private void scroll() {
messagesContainer.setSelection(messagesContainer.getCount() - 1);
}
#Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return new CursorLoader(ChatActivity.this, MensagemProvider.CONTENT_URI_CONVERSA, null, null, new String[]{Long.toString(amanteId), Long.toString(amanteId)}, null);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}
My adapter
public class ChatAdapter extends CursorAdapter {
private Cursor cursor;
private int dataEnvioColumnIndex;
private int idMensagemColumnIndex;
private int idRemetenteColumnIndex;
private int idDestinatarioColumnIndex;
private int apelidoRemetenteColumnIndex;
private int apelidoDestinatarioColumnIndex;
private int textoMensagemColumnIndex;
private long idColaboradorLogado;
public ChatAdapter(Context context) {
super(context, null, false);
}
public ChatMessage getItem() {
ChatMessage message = new ChatMessage();
SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date dataEnvio = new Date(cursor.getLong(dataEnvioColumnIndex));
message.setDate(dt.format(dataEnvio));
message.setId(cursor.getLong(idMensagemColumnIndex));
Long de = cursor.getLong(idRemetenteColumnIndex);
Long logado = BaseApp.getCredentials().getId();
message.setMe(de.equals(logado));
message.setMessage(cursor.getString(textoMensagemColumnIndex));
return message;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View retView = vi.inflate(R.layout.list_item_chat_message, null);
return retView;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = createViewHolder(view);;
view.setTag(holder);
ChatMessage chatMessage = getItem();
boolean myMsg = chatMessage.getIsme() ;//Just a dummy check
holder.txtMessage.setText(chatMessage.getMessage());
holder.txtInfo.setText(chatMessage.getDate());
}
private ViewHolder createViewHolder(View v) {
ViewHolder holder = new ViewHolder();
holder.txtMessage = (TextView) v.findViewById(R.id.txtMessage);
holder.content = (LinearLayout) v.findViewById(R.id.content);
holder.contentWithBG = (LinearLayout) v.findViewById(R.id.contentWithBackground);
holder.txtInfo = (TextView) v.findViewById(R.id.txtInfo);
return holder;
}
private static class ViewHolder {
public TextView txtMessage;
public TextView txtInfo;
public LinearLayout content;
public LinearLayout contentWithBG;
}
#Override
public Cursor swapCursor(Cursor cursor) {
if(cursor!=null) {
cursor.moveToFirst();
idMensagemColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_MENSAGEMID);
idRemetenteColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_DE);
idDestinatarioColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_PARA);
apelidoRemetenteColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_APELIDO_REMETENTE);
apelidoDestinatarioColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_APELIDO_DESTINATARIO);
textoMensagemColumnIndex = cursor.getColumnIndex(MensagemProvider.COLUMN_MENSAGEM);
}
notifyDataSetChanged();
return cursor;
}
}
what I'm doing wrong ? Can anybody help me ?
Thanks!
Overriding swapCursor() is asking for trouble. The cursor won't be positioned where the adapter expects it to be positioned (before first). And you don't call super.swapCursor() so the adapter never really hears about the new cursor.
I bet you're trying to "optimize" by getting the column indexes only once each time a new cursor is swapped.
First just try getting rid of the swapCursor() override and making the getColumnIndex() calls in your getItem() method. If that works and you still really want to have getColumnIndex() called only once per cursor, you could try something like setting all your cursor indexes to -1 when you swap the cursor, then calling getColumnIndex() inside getItem() only when the index is -1.
But don't mess with swapCursor(), especially without calling super.swapCursor() and returning its result.
Related
Right now I am trying to use recyclerview with a cursorloader. I included the cursorloader within my recyclerview adapter based on my research. I do not have the desire to put my SQLite database data into an arraylist. Right now it looks like my code is correct but when I load the app I get a blank screen. Can anyone help me see my mistake in my code?
Here is my adapter:
public class PrescriptionRecyclerAdapter extends RecyclerView.Adapter<PrescriptionRecyclerAdapter.ViewHolder> {
private CursorAdapter mCursorAdapter;
private Context mContext;
private ViewHolder holder;
Cursor prescriptionCursor;
public PrescriptionRecyclerAdapter(Context context, Cursor c) {
mContext = context;
prescriptionCursor = c;
mCursorAdapter = new CursorAdapter(mContext, c, 0) {
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// Inflate the view here
View v = LayoutInflater.from(context)
.inflate(R.layout.recycle_item, parent, false);
return v;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// Extract data from the current store row and column
int nameColumnIndex = cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry.COLUMN_PRESCRIPTION_NAME);
int amountColumnIndex = cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry.COLUMN_PRESCRIPTION_AMOUNT);
int durationColumnIndex = cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry.COLUMN_PRESCRIPTION_FREQUENCY_DURATION);
final int columnIdIndex = cursor.getColumnIndex(PrescriptionContract.PrescriptionEntry._ID);
//Read the store attritubes from the Cursor for the current stores
String name = cursor.getString(nameColumnIndex);
String amount = cursor.getString(amountColumnIndex);
String duration = cursor.getString(durationColumnIndex);
String col = cursor.getString(columnIdIndex);
// Populate fields with extracted properties
holder.prescriptionName.setText(name);
holder.prescriptionAmount.setText(amount);
holder.prescriptionDays.setText(duration);
}
};
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView prescriptionName;
public TextView prescriptionAmount;
public TextView prescriptionDays;
final public Button prescriptionButton;
public ViewHolder(View itemView) {
super(itemView);
// Find fields to populate in inflated template
prescriptionName = (TextView) itemView.findViewById(R.id.name);
prescriptionAmount = (TextView) itemView.findViewById(R.id.amountlist);
prescriptionDays = (TextView) itemView.findViewById(R.id.daysList);
prescriptionButton = itemView.findViewById(R.id.scheduleButton);
}
}
#Override
public int getItemCount() {
return mCursorAdapter.getCount();
}
public Cursor swapCursor(Cursor cursor) {
if (prescriptionCursor == cursor) {
return null;
}
Cursor oldCursor = prescriptionCursor;
this.prescriptionCursor = cursor;
if (cursor != null) {
this.notifyDataSetChanged();
}
return oldCursor;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
// Passing the binding operation to cursor loader
mCursorAdapter.getCursor().moveToPosition(position);
mCursorAdapter.bindView(holder.itemView, mContext, mCursorAdapter.getCursor());
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Passing the inflater job to the cursor-adapter
View v = mCursorAdapter.newView(mContext, mCursorAdapter.getCursor(), parent);
holder = new ViewHolder(v);
return holder;
}
}
Here is my display activity.
public class DisplayActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor>{
private static final int PRESCRIPTION_LOADER = 0;
PrescriptionRecyclerAdapter mCursorAdapter;
private RecyclerView.LayoutManager mLayoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(DisplayActivity.this, EditorActivity.class);
startActivity(intent);
}
});
RecyclerView prescriptionRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(getApplicationContext());
prescriptionRecyclerView.setLayoutManager(mLayoutManager);
mCursorAdapter = new PrescriptionRecyclerAdapter(this, null);
prescriptionRecyclerView.setAdapter(mCursorAdapter);
//Kick off the loader
getLoaderManager().initLoader(PRESCRIPTION_LOADER,null,this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
// Respond to a click on the "Delete all entries" menu option
case R.id.action_delete_all_entries:
deleteAllPrescriptions();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Helper method to delete all items in the database.
*/
private void deleteAllPrescriptions() {
int rowsDeleted = getContentResolver().delete(PrescriptionEntry.CONTENT_URI, null, null);
Log.v("CatalogActivity", rowsDeleted + " rows deleted from prescription database");
}
#Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
// Since the editor shows all store attributes, define a projection that contains
// all columns from the store table
String[] projection = {
PrescriptionEntry._ID,
PrescriptionEntry.COLUMN_PRESCRIPTION_NAME,
PrescriptionEntry.COLUMN_PRESCRIPTION_AMOUNT,
PrescriptionEntry.COLUMN_PRESCRIPTION_FREQUENCY_HOURS,
PrescriptionEntry.COLUMN_PRESCRIPTION_FREQUENCY_TIMES,
PrescriptionEntry.COLUMN_PRESCRIPTION_FREQUENCY_DURATION,
PrescriptionEntry.COLUMN_PRESCRIPTION_REFILL,
PrescriptionEntry.COLUMN_PRESCRIPTION_EXPIRATION,
PrescriptionEntry.COLUMN_PRESCRIPTION_PHARMACIST_NAME,
PrescriptionEntry.COLUMN_PRESCRIPTION_PHARMACIST_NUMBER,
PrescriptionEntry.COLUMN_PRESCRIPTION_PHYSICIAN_NAME,
PrescriptionEntry.COLUMN_PRESCRIPTION_PHYSICIAN_NUMBER};
// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this, // Parent activity context
PrescriptionEntry.CONTENT_URI, // Query the content URI for the current store
projection, // Columns to include in the resulting Cursor
null, // No selection clause
null, // No selection arguments
null); // Default sort order
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
mCursorAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
mCursorAdapter.swapCursor(null);
}
}
I think i got your problem. After getting new Cursor in onLoadFinished method you are calling PrescriptionRecyclerAdapter's swapCursor() method this method is updating prescriptionCursor Cursor reference. Thats OK. But updating prescriptionCursor will not effect your CursorAdapter. You are actually dependent on CursorAdapter. So you have to update theCursor of your CursorAdapter. Because your mCursorAdapter still holding the old reference of the Cursor you have provided in constructor.
So use this method to update Cursor reference mCursorAdapter.swapCursor(prescriptionCursor).
public Cursor swapCursor(Cursor cursor) {
if (prescriptionCursor == cursor) {
return null;
}
Cursor oldCursor = prescriptionCursor;
this.prescriptionCursor = cursor;
if (cursor != null) {
this.notifyDataSetChanged();
// update your Cursor for CursorAdapter
mCursorAdapter.swapCursor(prescriptionCursor);
}
return oldCursor;
}
I think you have made it complex by maintaining two Adapter. You can use RecyclerView.Adapter with List or Cursor. There is not need to make it complex.
Hope it will help you. Let me know it solve your problem.
I checked a lot of stack overflow question, and none of them helped.
How to refresh my listView after deleting item with onLongClickListener?
As you'll see nor adapter.notifyDataSetChanged(), nor listView.invalidateViews() don't work.
This is implementation with just necessary methods to figure out idea.
public class MainActivity extends AppCompatActivity {
public FloatingActionButton fabAddWord;
public Toolbar toolbar;
public ListView listView;
private RjecnikCursorAdapter adapter;
private RjecnikDB dbRjecnik;
private SQLiteDatabase db;
private Cursor cursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fabAddWord = (FloatingActionButton) findViewById(R.id.fabAddWord);
listView = (ListView) findViewById(R.id.listView);
dbRjecnik = new RjecnikDB(this);
db = dbRjecnik.getWritableDatabase();
String query = "SELECT * FROM " + RjecnikDB.TABLE;
cursor = db.rawQuery(query, null);
adapter = new RjecnikCursorAdapter(this, cursor);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
deleteOnLongClick(cursor.getString(cursor.getColumnIndex(RjecnikDB.COLUMN_RIJEC)));
adapter.notifyDataSetChanged();
listView.invalidateViews();
return true;
}
});
}
public void deleteOnLongClick(String rijec) {
SQLiteDatabase db = dbRjecnik.getWritableDatabase();
db.delete(RjecnikDB.TABLE, RjecnikDB.COLUMN_RIJEC + " = ?", new String[] {rijec} );
this.adapter.notifyDataSetChanged();
this.listView.invalidateViews();
db.close();
}
CustomAdapter
public class RjecnikCursorAdapter extends CursorAdapter {
public RjecnikCursorAdapter (Context context, Cursor cursor) {
super(context, cursor, 0);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item_word, parent, false);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tvSingleLineWord = (TextView) view.findViewById(R.id.tvSingleLineWord);
// Extract properties from cursor
String rijec = cursor.getString(cursor.getColumnIndex(RjecnikDB.COLUMN_RIJEC));
// Populate fields with extracted properties
tvSingleLineWord.setText(rijec);
}
}
The Adapter have a data cache, so data in ListView will not change when data in database changed. You should change the cursor.
#Override
public void changeCursor(Cursor cursor) {
mIndexer.setCursor(cursor);
super.changeCursor(cursor);
}
put code below to your OnItemLongClickListener
cursor = db.rawQuery(query, null);
adapter.changeCurosr(cursor);
You have to use just notifyDataSetChanged() to refresh your listView data, invalidateViews() will only redraw the visible items yet nothing on them has changed (changing font for example).
Note that is recommanded to run your notifyDataSetChanged() on UI thread.
EDIT 2 : you can use BaseAdapter instead of CursorAdapter like below
public class RjecnikCursorAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<String> rjecnikList;
public RjecnikCursorAdapter(Activity activity, List<String> rjecnikList) {
this.activity = activity;
this.rjecnikList = rjecnikList;
}
#Override
public int getCount() {
return rjecnikList.size();
}
#Override
public Object getItem(int location) {
return rjecnikList.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.item_word, null);
TextView tvSingleLineWord = (TextView) view.findViewById(R.id.tvSingleLineWord);
tvSingleLineWord.setText(String.valueOf(rjecnikList.get(position)));
return convertView;
}
}
And in your activity :
public class MainActivity extends AppCompatActivity {
public FloatingActionButton fabAddWord;
public Toolbar toolbar;
public ListView listView;
public List<String> mylist;
private RjecnikCursorAdapter adapter;
private RjecnikDB dbRjecnik;
private SQLiteDatabase db;
private Cursor cursor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fabAddWord = (FloatingActionButton) findViewById(R.id.fabAddWord);
listView = (ListView) findViewById(R.id.listView);
dbRjecnik = new RjecnikDB(this);
db = dbRjecnik.getWritableDatabase();
String query = "SELECT * FROM " + RjecnikDB.TABLE;
cursor = db.rawQuery(query, null);
mylist = new ArrayList<>();
if (cursor.moveToFirst()){
do{
//change this with your column data
String data = cursor.getString(cursor.getColumnIndex("data");
mylist.add(data);
}while(cursor.moveToNext());
}
cursor.close();
adapter = new RjecnikCursorAdapter(this, mylist);
listView.setAdapter(adapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
deleteOnLongClick(cursor.getString(cursor.getColumnIndex(RjecnikDB.COLUMN_RIJEC)));
mylist.remove(position);
adapter.notifyDataSetChanged();
listView.invalidateViews();
return true;
}
});
}
public void deleteOnLongClick(String rijec) {
SQLiteDatabase db = dbRjecnik.getWritableDatabase();
db.delete(RjecnikDB.TABLE, RjecnikDB.COLUMN_RIJEC + " = ?", new String[] {rijec} );
this.adapter.notifyDataSetChanged();
this.listView.invalidateViews();
db.close();
}
first of all thank you all for your answers and sorry for the bad English used, have a controller class to populate a listview and an adapter class that extends CursorAdapter, my problem is that when I setAdapter, my class adapter is not running the Newview method BindView certainly not running. Here is the code used for this, they hope can help. regards
public class ListaAdaptador extends CursorAdapter {
public static class ContenedorView
{
TextView nombre;
TextView apellidos;
TextView dni;
TextView telefono;
ImageView img;
}
public ListaAdaptador(Context a, Cursor c)
{
super(a, c);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.list_item,parent,false);
ContenedorView contenedor = null;
contenedor = new ContenedorView();
contenedor.nombre = (TextView) view.findViewById(R.id.txt_item_nombre);
contenedor.apellidos =(TextView) view.findViewById(R.id.txt_item_apellidos);
contenedor.dni = (TextView) view.findViewById(R.id.txt_item_dni);
contenedor.telefono = (TextView) view.findViewById(R.id.txt_item_telefono);
contenedor.img = (ImageView) view.findViewById(R.id.ImageViewItem);
view.setTag(contenedor);
return view;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
ContenedorView contenedor = (ContenedorView) view.getTag();
contenedor.nombre.setText(cursor.getString(cursor.getColumnIndex("nombre")));
contenedor.apellidos.setText(cursor.getString(cursor.getColumnIndex("apellidos")));
contenedor.dni.setText(cursor.getString(cursor.getColumnIndex("dni")));
contenedor.telefono.setText(cursor.getString(cursor.getColumnIndex("telefono")));
contenedor.img.setImageResource(R.drawable.img_nodisp);
}
}
clase ListaControlador
public class ListadoControlador extends ListActivity {
EditText aproximacion;
ListView lv;
ImageButton buscar;
SQLiteDatabase db;
Cursor cursor;
ListaAdaptador adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listado);
aproximacion = (EditText) findViewById(R.id.EditText01);
buscar =(ImageButton)findViewById(R.id.btnBuscar);
lv=getListView();
prepararBaseDeDatos();
obtenemosValores();
}
public void prepararBaseDeDatos()
{
AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this,"registro",null,1);
db = admin.getWritableDatabase();
}
public void obtenemosValores()
{
cursor = db.rawQuery("select _id,nombre,apellidos,dni,telefono from paciente", null);
adapter = new ListaAdaptador(ListadoControlador.this,cursor);
lv.setAdapter(adapter);
}
Remove the three Override methods getCount(), getItem() and getItemId() from your ListaAdaptador class. You don't need them since you're using a CursorAdapter.
I'm in some trouble with custom cursorAdapter and AsyncTask. I wanted to setup listView and adapter first and after that in asyctask to query data from database. But for a first time cursor in adapter is null and it throws an a exception. How can be handled this such on situation in my custom cursor adapter? Relocation of setAdapter to onPostExecute didn't helped also. Any suggestions will be also great.
Main activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConversationsListCursorAdapter conversationsListCursorAdapter = new ConversationsListCursorAdapter(context, R.layout.conversations_list_item, null, 0);
conversationsListView = (ListView) findViewById(android.R.id.list);
conversationsListView.setAdapter(conversationsListCursorAdapter);
}
#Override
protected void onResume() {
super.onResume();
new GetConversationsTask().execute((Object[]) null);
}
#Override
protected void onStop() {
Cursor cursor = conversationsListCursorAdapter.getCursor();
if (cursor != null) {
cursor.close();
}
conversationsListCursorAdapter.changeCursor(null);
super.onStop();
}
private class GetConversationsTask extends AsyncTask<Object, Object, Cursor> {
#SuppressLint("NewApi")
#Override
protected Cursor doInBackground(Object... params) {
Uri uri = ClouContentProvider.CONVERSATIONS_CONTENT_URI;
Cursor cursor = null;
if (android.os.Build.VERSION.SDK_INT < 11) {
cursor = getContentResolver().query(uri, null, null, null, null);
} else {
CursorLoader cursorLoader = new CursorLoader(context, uri, null, null, null, null);
cursor = cursorLoader.loadInBackground();
}
return cursor;
}
#Override
protected void onPostExecute(Cursor cursor) {
conversationsListCursorAdapter.changeCursor(cursor);
cursor.close();
}
}
Cursor adapter:
public class ConversationsListCursorAdapter extends CursorAdapter {
private final Cursor mCursor;
private final Context mContext;
private final int mLayout;
private final int mSnippetIndex;
private final int mDateIndex;
private final int mMessageCount;
private final int mRead;
private final LayoutInflater mLayoutInflater;
//private static final String TAG = ConversationsListCursorAdapter.class.getSimpleName();
static class ViewHolder {
TextView tvBody;
TextView tvPerson;
TextView tvCount;
TextView tvDate;
QuickContactBadge ivPhoto;
View vRead;
}
public ConversationsListCursorAdapter(Context context, int layout, Cursor cursor, int flags) {
super(context, cursor, flags);
this.mContext = context;
this.mLayout = layout;
this.mCursor = cursor;
this.mSnippetIndex = mCursor.getColumnIndex(ConversationsProviderMetaData.ConversationsTableMetaData.SNIPPET);
this.mDateIndex = mCursor.getColumnIndex(ConversationsProviderMetaData.ConversationsTableMetaData.DATE);
this.mMessageCount = mCursor.getColumnIndex(ConversationsProviderMetaData.ConversationsTableMetaData.MESSAGE_COUNT);
this.mRead = mCursor.getColumnIndex(ConversationsProviderMetaData.ConversationsTableMetaData.READ);
this.mLayoutInflater = LayoutInflater.from(mContext);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View rowView = mLayoutInflater.inflate(mLayout, parent, false);
ViewHolder holder = new ViewHolder();
holder.tvBody = (TextView) rowView.findViewById(R.id.body);
holder.tvPerson = (TextView) rowView.findViewById(R.id.addr);
holder.tvCount = (TextView) rowView.findViewById(R.id.count);
holder.tvDate = (TextView) rowView.findViewById(R.id.date);
holder.ivPhoto = (QuickContactBadge) rowView.findViewById(R.id.photo);
holder.vRead = (View) rowView.findViewById(R.id.read);
rowView.setTag(holder);
return rowView;
}
#Override
public void bindView(View v, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder) v.getTag();
String cBody = cursor.getString(mSnippetIndex);
Integer cCount = cursor.getInt(mMessageCount);
Long cDate = cursor.getLong(mDateIndex);
Integer cRead = cursor.getInt(mRead);
holder.tvBody.setText(cBody);
if (cCount < 0) {
holder.tvCount.setText("");
} else {
holder.tvCount.setText("(" + cCount + ")");
}
if (cRead == 0) {
holder.vRead.setVisibility(View.VISIBLE);
} else {
holder.vRead.setVisibility(View.INVISIBLE);
}
holder.tvDate.setText(ClouUtils.getDate(context, cDate));
holder.tvPerson.setText(cursor.getString(mCursor.getColumnIndex(ConversationsProviderMetaData.ConversationsTableMetaData.CANONICAL)));
holder.ivPhoto.setImageResource(R.drawable.ic_contact_picture);
holder.ivPhoto.setVisibility(View.VISIBLE);
}
#Override
protected void onPostExecute(Cursor cursor) {
// First check if it's null
if(cursor !=null){
// use swap to get the old cursor and close it
Cursor oldC = conversationsListCursorAdapter.swapCursor(cursor);
if(oldC != null){
oldC.close();
}
// DO NOT CLOSE THE NEW CURSOR
// this one you must close whenever the list goes onPause(); but not before
}
}
Good day, please could somebody help me find out what's wrong with my code. I am probably missing something. Nothing is displayed from my CursorAdapter.
Have been struggling with here for some time now.
Any help will be highly appreciated!!
public class ManageTagFragment extends Fragment implements LoaderCallbacks<Cursor> {
private static String TAG = "Image_Debugger";
private static final int IMAGE_LOADER = 0;
TextView text;
GridView grid;
CustomGridImageAdapter imageAdapter;
Cursor cursor;
ImageDB dbadapter;
private Cursor c;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dbadapter = new ImageDB(getActivity().getApplicationContext());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.wallpaper_images, container, false);
grid = (GridView)view.findViewById(R.id.gridview);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
imageAdapter = new CustomGridImageAdapter(getActivity(), null, 0);
grid.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
getActivity().getSupportLoaderManager().initLoader(IMAGE_LOADER, null, this);
}
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CustomCursorLoader(getActivity().getApplicationContext(), dbadapter, ImageDB.IMAGES);
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
imageAdapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> args) {
imageAdapter.swapCursor(null);
}
public static final class CustomCursorLoader extends SimpleCursorLoader {
String columnname;
ImageDB dbadapter;
ArrayList<String> imageuri = new ArrayList<String>();
public CustomCursorLoader(Context context, ImageDB dbadapter, String columnname){
super(context);
this.dbadapter = dbadapter;
this.columnname = columnname;
}
public CustomCursorLoader(Context context,String columnname){
super(context);
this.columnname = columnname;
}
#Override
public Cursor loadInBackground() {
Cursor cursor = null;
dbadapter.open();
Log.d(TAG, "retrieving Images from database now");
cursor = dbadapter.retrieveTag(columnname);
if(cursor.moveToFirst()){
final int index = cursor.getColumnIndex(columnname);
do {
final String val = cursor.getString(index); // this is just
// for test
if (val != null) {
Log.d(TAG, "files are" + val);
imageuri.add(val);
}
} while (cursor.moveToNext());
}
return cursor;
}
}
}
CustomGridImageAdapter.java:
public class CustomGridImageAdapter extends CursorAdapter {
private static String TAG = "Image_Debugger";
private LayoutInflater inflater;
private int count;
public CustomGridImageAdapter(Context context, Cursor cursor, int flags) {
super(context, cursor, flags);
inflater = LayoutInflater.from(context);
Log.d(TAG, "calling grid imageadapter now");
}
#Override
public int getCount() {
return count;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
Log.d(TAG, "calling bind view");
ViewHolder holder = (ViewHolder) view.getTag();
String name = cursor.getString(cursor
.getColumnIndexOrThrow(ImageDB.IMAGES));
Log.d(TAG, "string name in gridimageadapter is" + name);
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(name);
Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 120, 120, false);
holder.image.setImageBitmap(newbitmap);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup container) {
Log.d(TAG, "calling new view here");
ViewHolder holder = new ViewHolder();
View view = inflater.inflate(R.layout.media_view, container, false);
holder.image = (ImageView) view.findViewById(R.id.media_image_id);
holder.image.setLayoutParams(new GridView.LayoutParams(100, 100));
holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
view.setTag(holder);
return view;
}
class ViewHolder {
ImageView image;
TextView item_name;
}
}
The problem might be here. Its just an assumption. Sorry if I am wrong.
#Override
public int getCount() {
return count;
}
What is the value of count here. I don't find any place where you have passed the value for count.So my assumption is that, the default value for int is returned here (i.e count=0) and hence you don't find any data in your GridView.
Could you check that and comment back.