开发者

Marking text in the row of the list as "READ" permenatly Android

开发者 https://www.devze.com 2023-01-23 07:34 出处:网络
What I\'m trying to accomplish here is to set the text in a given row as read if the user clicks on it, now I was able to do that by using the onclick method, the problem with it is that it goes away

What I'm trying to accomplish here is to set the text in a given row as read if the user clicks on it, now I was able to do that by using the onclick method, the problem with it is that it goes away when an intent is fired or the user exits the app. I want the Text to be set up as read permanently. here is my piece of code if anybody can help I would greatly appreciate it. Thank you in advance:

public void onListItemClick(ListView parent, View v, int position, long id) {
  LinearLayout ll = (LinearLayout) v;
  TextView clickedTextView = (TextView) ll.getChildAt(1);
  clickedTextView.setTextColor(Color.GREEN);
  StringTokenizer st = new StringTokenizer(strings[position],"<@>");
  for(int i=0;i<3;i++)
  {
    coupon = st.nextToken("<@>");
  }
  sharable=st.nextToken();
  Intent i = new Intent(getApplicationContext(), CouponImage.class);
  i.putExtra("The coupon", coup开发者_StackOverflow社区on);
  i.putExtra("Sharable", sharable);
  startActivity(i);
}


You'll have to store the read status for each text item, either in a SQLite Database, or in a flat file in Internal Storage.


This is because whenever you scroll the list, leave the app and come back, etc. you end up with the ListAdapter re-rendering your row view, and your views in a list are never 1:1 with underlying data due to view recycling. If you want your change to be "sticky" you need to think about modifying the underlying data for the ListAdapter in a way it the adapter knows how to render correctly, not just changing this particular instance of a row -- you're marking the item read, not just setting one instance of a rendered view of the item as read. You can think of it as an MVC thing if it helps.

That is, the real change here should be to your Adapter's getView method, with a change to its data source and then possibly a call to notifyDataSetChanged.

Fredley's answer about SQLite or storage may be overkill if that data doesn't need to persist beyond this one session in the activity, or if the data you're working with is also transient (e.g. network data that changes often that's temporarily loaded into an ArrayAdapter), and in any case it's a bit misleading because just dumping data to disk doesn't solve the fundamental issue with your conflation of views of data with your models.

0

精彩评论

暂无评论...
验证码 换一张
取 消