简体   繁体   English

Android:更改ListView内的TextView的文本?

[英]Android: Change text of a TextView that is inside a ListView?

I know that i could just refresh the ListView but i don't want it to jump to the very top again. 我知道我可以刷新ListView,但我不希望它再次跳到最高位置。

So i have got a ListView. 所以我有一个ListView。 In it there are a few TextViews, Buttons, ImageViews, etc. I filled it using a custom adapter and everything works fine. 其中有一些TextView,Button,ImageView等。我使用自定义适配器填充了它,一切正常。 Even the OnClickListener for the Buttons. 甚至是按钮的OnClickListener。 But as soon as i try to change the content of one of the TextViews like so (Button to increase the value of the number in the tv. This is called in the getView() of my adapter): 但是,一旦我尝试像这样更改TextView之一的内容(通过按钮增加电视中数字的值。这在适配器的getView()中被调用):

btn_inc.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        count.setText(Integer.parseInt(""+count.getText())+1);
    }
});

i get the following error: 我收到以下错误:

android.content.res.Resources$NotFoundException: String resource ID #0x1

I assume this is because the TexView is only created temporary and deleted as soon as the getView() of my adapter is finished? 我认为这是因为TexView仅是临时创建的,并且在适配器的getView()完成后立即删除吗?

Thanks in advance for any of your awnsers! 预先感谢您的任何遮阳篷!

Try this: 尝试这个:

btn_inc.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        count.setText(String.valueOf(Integer.parseInt(""+count.getText())+1));
    }
});

Basically you are trying to set an int as the text of the TextView which accepts a String . 基本上,您尝试将int设置为接受String的TextView的文本。 Converting the int to a String should get rid of that error. int转换为String应该摆脱该错误。 In this case, Android is taking that integer and trying to look up a resource ID which in this case doesn't exist. 在这种情况下,Android将使用该整数并尝试查找在这种情况下不存在的资源ID。

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

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