private String getRealName(String nickName) { String realName = ""; switch (nickName) { case "SG": realName = "SG"; break; case "AX": realName = "AX"; break; case "HX": realName = "HX"; break; default: realName = "Others"; break; } return realName; }public void reader(String epc) { ImportViewModel importViewModel=new ImportViewModel(); String upc = epc; //UpcUtils.getUPC(epc); Stock s; String abbreviation = ""; String nickName = upc.replaceAll("@+", ""); String slName = nickName.substring(0, 2); String realName = getRealName(slName); s = new Stock(upc, upc, realName, realName); s.setTime(FileUtil.getTimes()); s.setAbbreviation(abbreviation); //adapter.update(s); long count = Long.parseLong(btn_update.getText().toString()) + 1; btn_update.setText(String.valueOf(count)); }根据case更新btn_update
时间: 2023-12-25 19:06:47 浏览: 115
你可以在getRealName方法中添加更新btn_update的代码,例如:
private String getRealName(String nickName) {
String realName = "";
switch (nickName) {
case "SG":
realName = "SG";
break;
case "AX":
realName = "AX";
break;
case "HX":
realName = "HX";
break;
default:
realName = "Others";
break;
}
// 更新btn_update
long count = Long.parseLong(btn_update.getText().toString()) + 1;
btn_update.setText(String.valueOf(count));
return realName;
}
这样每次调用getRealName方法时,btn_update的值就会加1。
相关问题
ListView btn_updatepublic void reader(String epc) { String upc = epc; //UpcUtils.getUPC(epc); Stock s; String name = ""; String abbreviation = ""; String NickName = epc.substring(2, 4); String realName = getRealName(NickName); s = new Stock(epc, epc, realName, realName); s.setTime(FileUtil.getTimes()); s.setAbbreviation(abbreviation); //soundPoolHelper.play("happy1",false); adapter.update(s); btn_update.setText(String.valueOf(adapter.getCount())); }中的btn_update.setText为什么无效
可能是因为您没有在UI线程上更新UI。在Android中,只有UI线程才能更新UI元素。您可以尝试将更新UI元素的代码包装在runOnUiThread方法中,或使用Handler来更新UI元素。例如:
```
runOnUiThread(new Runnable() {
@Override
public void run() {
btn_update.setText(String.valueOf(adapter.getCount()));
}
});
```
或者:
```
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
btn_update.setText(String.valueOf(adapter.getCount()));
}
});
```
public void reader(String epc) { String upc = epc; //UpcUtils.getUPC(epc); Stock s; String abbreviation = ""; String nickName = upc.replaceFirst("^@", ""); String realName = getRealName(nickName); s = new Stock(upc, upc, realName, realName); s.setTime(FileUtil.getTimes()); s.setAbbreviation(abbreviation); //adapter.update(s); long count =Long.parseLong(btn_update.getText().toString())+1; btn_update.setText(String.valueOf(count)); }数据无法添加到btn_update
根据您提供的代码,似乎btn_update是一个按钮,您不能将数据直接添加到按钮上。您需要将数据添加到适当的数据结构(如列表或数组)中,然后更新按钮上的文本以反映所添加的项目数。
例如,您可以使用以下代码将Stock对象添加到一个列表中:
```java
List<Stock> stockList = new ArrayList<>();
stockList.add(s);
```
然后,您可以使用以下代码更新按钮的文本:
```java
long count = stockList.size();
btn_update.setText(String.valueOf(count));
```
阅读全文