How to make redraw the list?

This question is slightly different from others of the same name. I am not trying to update the data in the list, I am trying to update the look of the list.

My application is fragment based using the support library. I have a PreferenceActivity where I allow the user to set the colors that they would like to use for the text in the list (the adapter reads the preference and sets the colors). This works for the most part.

The problem I have is as follows. When I have a list on the screen (this is a ListFragment) and pull out the menu, select "Preferences" and make changes to the color preference. Upon returning to the list from PreferenceActivity, I cannot get the list to redraw itself with the specified new color.

If I go from the list and return to it, it will be updated with a new color.

I am trying to use onResume to make changes. The code I have (which does not seem to do anything for the list, but changes the color of the title as it should):

 @Override public void onResume() { super.onResume(); header.setTextColor(MyApplication.header); line.setBackgroundColor(MyApplication.header_line); subheader.setTextColor(MyApplication.header); getListView().invalidateViews(); } 

I tried invalidateViews and invalidate . In desperation, I tried calling notifyDataSetChanged on the adapter, even if there were no changes in the data itself. Nothing seems to work.

Am I missing something obvious or is there no way to do this?

EDIT

The getView method from my adapter:

 @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) convertView = View.inflate(context, layout, null); View row = convertView; c.moveToPosition(position); TextView first = (TextView) convertView.findViewById(R.id.ListItem1); TextView second = (TextView) convertView.findViewById(R.id.ListItem2); TextView third = (TextView) convertView.findViewById(R.id.ListItem3); TextView fourth = (TextView) convertView.findViewById(R.id.ListItem4); DecimalFormat df = new DecimalFormat("0.00"); Double hold = Double.valueOf(c.getString(3)); Double qty = Double.valueOf(c.getString(1)); Double total = hold * qty; String color = "#FF00FF"; first.setText(c.getString(2)); first.setTextColor(MyApplication.shoplistitem_name); second.setText(c.getString(4)); second.setTextColor(MyApplication.shoplistitem_desc); third.setText(c.getString(1)); third.setTextColor(MyApplication.shoplistitem_qty); fourth.setText("$" + df.format(total)); fourth.setTextColor(MyApplication.shoplistitem_desc); if (strikethroughState[position] == 1) { first.setPaintFlags(first.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); second.setPaintFlags(second.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); third.setPaintFlags(third.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); fourth.setPaintFlags(third.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); row.setBackgroundColor(MyApplication.shoplistitem_checked); } else { first.setPaintFlags(first.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); second.setPaintFlags(second.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); third.setPaintFlags(third.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); fourth.setPaintFlags(third.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG); row.setBackgroundResource(R.color.black); } return (row); } 
+6
source share
3 answers

notifyDataSetChanged() must definitely work for you. How do you deal with changing preferences? If you simply prefer SharedPreferences, it never updates the static variable MyApplication. Can you post the code you use to set your preference? If you are not using it, set onPreferenceChangedListener() to color preference, to set a static variable when it changes.

+8
source

You should call notifyDataSetChanged () on your adapter.

+1
source

Invalidate, invalidateviews, notifyDatasetChanged, nothin worked for me! To just refresh the list (using the cursor), I just do:

 cur = db.rawQuery( "SELECT * FROM ssnt WHERE mid = 1000" ); //optional, initial query adapter.changeCursor(cur); // or swapCursor(), cur is the initial and the only cursor 

But in order to have the correct background color, draw, underline the text, I just do:

 cur = db.rawQuery( "SELECT * FROM ssnt WHERE mid = 1000" ); adapter.changeCursor(cur); list.setAdapter(adapter); //just like I did oncreate 

I don’t like Barack’s solution, because it seems heavy, and executes code when it’s not needed.

0
source

Source: https://habr.com/ru/post/918576/


All Articles