android - Editing list view items dynamically when using cursor adapters and loaders -


I am writing a small messaging app and I have had this problem recently.

I have a ListFragment subclass, which implements the cursor loader callback. The built-in ListView is linked to a SimpleCursorAdapter . Each line view in this list has a layer illustration in the form of a background, I want to switch between the levels based on the database information (whether the message was sent or not).

My question is, how do you change a line background in this case? Do I have to access it through the adapter? I'm pretty sure that this should be done in the onLoadFinished method. But this code gives me the NullPointerException you can talk me in the right direction, I would be really grateful < / P>

  Public Class MessagesFragment ListFragment LoaderManager.LoaderCallbacks Extends the apply. Cursor & gt; {Private Static MessagesFragment piece; Private stable main activity activity; Private Static SimpleCursor Adapter Adapter; Public static messagesFragment getInstance () {if (fragment == null) fragment = new messagesFragment (); Return piece; } @ Override Crate on Public Zero (Bundle Saved Instantstate) {Super.Nought (Saved Instantstate); Activity = (main activity) getActivity (); Adapter = New SimpleCursorAdapter (Activity R.layout.list_item, Null, New String [] {DataProvider.KEY_NUMBER, DataProvider.KEY_MESSAGE, DataProvider.KEY_TIME}, New Integer [] {R.id.item_field_number, R.id.item_field_message, R .id.item_field_time}, 0); SetListAdapter (adapter); GetLoaderManager (). InitLoader (0, empty, this); } @Override Public View onCreateView (LayoutInflater inflater, ViewGroup container, bundled savedInstanceState) {return inflater.inflate (R.layout.fragment_messages, faucet); } @ Override Public Wide OnListEightClick (ListView L, View V, Int Position, Long ID) {Super. Analystimeclic (L, V, Position, ID); Activity.showDetails (Long.valueOf (ID) .intValue ()); } @ Override Public Loader & lt; Cursor & gt; OnCreateLoader (integer ID, bundle args) {New CursorLoader (Activity DataProvider.CONTENT_URI, Null, Null, Null, Faucet) Return; } @ Override Public Wide Onload Load (Loader & lt; Cursor> Loader, Cursor Data) {adapter.swapCursor (Data); Whereas (data.moveToNext ()) {int id = data.getInt (data.getColumnIndex (DataProvider.KEY_ID)); Int sent = data. GetInt (data.getColumnIndex (DataProvider.KEY_SENT)); . This.getListView () getChildAt (ID) .getBackground () setLevel (sent). }} @ Override Public Wide Onloader Reset (Loader & lt; Cursor> Loader) {adapter.swapCursor (null); }   

}

This should help! Sample Adapter SimpleCursorAdapter {Private LayoutInflater inflater; Private cursor cursor; Personal reference references; Public sample adapter (reference references, int layout, cursor c, string [], int [], int flags) {super (context, layout, c, from, flag); This.context = context; Cursor = c; Inflater = LayoutInflator. From (context); } @ Override public int getCount () {return cursor == is empty? 0: cursor.gatecount (); } @OverWide Public Object Receipts (return status) {return tap; } @ Override public long ITMID (status of int) {return 0; } @ Override public view getView (integer status, see convertview view, ViewGroup parent) {if (cursor! = Null) {ViewHolder holder; Cursor.moveToPosition (status); If (convertView == empty) {convertView = inflater.inflate (R.layout.list_item, guardian, incorrect); Holder = New View Holder (ConvertView); ConvertView.setTag (holder); } And {holder = (seeholder) ConvertView.gettag (); } ConvertView.getBackground () SetLevel (cursor.getInt (cursor.gate codmax (DataProvider.KEY_SENT))); Holder.number.setText (cursor.getString (cursor.getColumnIndex (DataProvider.KEY_NUMBER))); Holder.message.setText (cursor.getColumnIndex (DataProvider.KEY_MESSAGE))); Holder.time.setText (cursor.getColumnIndex (DataProvider.KEY_TIME))); } Return Convertview; } @ Override Public cursor swap cursor (cursor c) {cursor = c; Return super.swapCursor (c); } Private class viewer {textview number; Textview message; Text view time; Public viewer (see view) {number = (TextView) view.findViewById (R.id.item_field_number); Message = (TextView) view.findViewById (R.id.item_field_message); Time = (text view) view.findViewById (R.id.item_countries_time); }}}

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -