简体   繁体   English

永久将列表行中的文本标记为“ READ”

[英]Marking text in the row of the list as “READ” permenatly Android

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. 我要在这里完成的工作是,如果用户单击它,则将给定行中的文本设置为已读,现在我可以通过使用onclick方法来做到这一点,它的问题是当意图被激发或用户退出应用程序。 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", coupon);
  i.putExtra("Sharable", sharable);
  startActivity(i);
}

您必须将每个文本项的读取状态存储在SQLite数据库中 ,或存储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. 这是因为每当您滚动列表,离开应用程序并返回等等时,您都将使用ListAdapter重新渲染行视图,并且由于视图回收,列表中的视图永远不会与基础数据成1:1的关系。 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. 如果您希望更改是“粘滞的”,则需要考虑以一种适配器知道如何正确呈现的方式来修改ListAdapter的基础数据,而不仅仅是更改该行的特定实例—您正在标记读取项目 ,而不仅仅是将项目渲染视图的一个实例设置为已读取。 You can think of it as an MVC thing if it helps. 如果有帮助,您可以将其视为MVC。

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 . 也就是说,真正的更改应该是对适配器的getView方法进行更改,对其数据源进行更改,然后可能是对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 (eg 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 . 如果数据不需要在活动中的这一会话之后持续存在,或者如果您正在使用的数据也是瞬态的(例如,经常更改的网络数据被临时加载到数据库中),Fredley关于SQLite或存储的答案可能会被过大杀伤。 ArrayAdapter),并且在任何情况下都会产生误导,因为仅将数据转储到磁盘并不能解决将数据视图模型混合在一起的根本问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM