【问题标题】:How can i get the last inserted record in Android?如何在 Android 中获取最后插入的记录?
【发布时间】:2015-11-23 11:02:26
【问题描述】:

我创建了一个活动,其中我有一个textView。在这个textView 中,我附加了来自mysql 数据库的一些记录。我想为最后插入的记录设置不同的颜色,而不是JSONArray 的最后一条记录。我怎样才能做到这一点? 这是我的代码:

JSONArray users = response.getJSONArray("statistics");
for (int i = 0; i < users.length(); i++) {
    JSONObject student = users.getJSONObject(i);

    String userName = student.getString("username");
    String score = student.getString("score");

    statistics.append(userName + " " + score + " \n");
}

谢谢!

【问题讨论】:

  • 每当您将某些内容插入数据库时​​,您都会获得 _id 作为返回值。 final long id = db.insert(Receipt.TABLE_NAME, null, receipt.getContent()); 所以本质上,应该是你的服务器告诉你。
  • “我想给最后插入的记录设置不同的颜色”,最后插入的记录不是json数组中的最后一条记录吗?
  • @Skynet:在我的情况下,如何为单个记录设置不同的颜色? @helldawg13:不,不是 json 数组中的最后一条记录。我这么说是因为当我想使用 getJSONObject(users.length()-1) 显示记录时,它会显示 json 数组中的最后一条记录,而不是数据库中最后插入的记录。如果有帮助,请在 way 中添加记录。
  • 这很简单,就像拍手一样:if(isLastInsert) {yourtextView.setBackgroundColor(0xFFFF0000);}
  • 不,伙计,isLastInsert 应该作为您的 JSOn 中的标志从服务器发送。

标签: java android mysql json


【解决方案1】:

从数据库中获取最后一条记录,在将该记录附加到 textview 之前,请执行以下操作以更改文本颜色。

Spannable word = new SpannableString("record to be inserted"); 
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

statistics.append(word);

【讨论】:

  • 好的,但我可以通过什么方式做到这一点?你能告诉我怎么做吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-13
  • 1970-01-01
  • 2018-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多