Android 在CustomListView适配器中不断更新TextView

Android 在CustomListView适配器中不断更新TextView,android,listview,android-adapter,Android,Listview,Android Adapter,在这个应用程序中,用户点击屏幕获得积分,然后购买升级,我想更新textview的原因是因为我想显示用户的升级数量 我试过用这样的东西 handler.postDelayed(new Runnable() { @Override public void run() { for (int i = 0; i < 10; i++) { HashMap<String, String> data

在这个应用程序中,用户点击屏幕获得积分,然后购买升级,我想更新textview的原因是因为我想显示用户的升级数量

我试过用这样的东西

  handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {


                HashMap<String, String> data = new HashMap<>();
                data.put("title", bookTitles[i]);
                data.put("pages", bookPages[i]);
                data.put("author", authors[i]);


                authorList.add(data);
            } 
            handler.postDelayed(this, 1000);

        }
    }, 1000);
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
对于(int i=0;i<10;i++){
HashMap数据=新的HashMap();
数据。put(“标题”,书名[i]);
数据。放入(“页面”,书页[i]);
data.put(“作者”,作者[i]);
添加(数据);
} 
handler.postDelayed(这个,1000);
}
}, 1000);
但它说authorsList不能从内部类访问,需要声明为final。我希望文本视图每秒更新一次,以便显示升级量

package com.example.navjeevenmann.mytycoon;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class ThirdActivity extends AppCompatActivity {
private ListView listView;
private int Add;
private int countervalue;
private TextView myCount;
private String countstring;
Handler handler = new Handler();
private ImageButton Home;
private ImageButton AutoClick;
private int firstoption;
private int nigg;
private String str;
private CustomListViewAdapter customListViewAdapter;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);
    getSupportActionBar().hide();


    Bundle it = getIntent().getExtras();
    countervalue = it.getInt("Count");
    Add = it.getInt("Add");
    myCount = (TextView) findViewById(R.id.textView3);
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Display(countervalue);
            handler.postDelayed(this, 1);
        }
    }, 50);

    AutoClick = (ImageButton) findViewById(R.id.autoclick);
    AutoClick.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent autoclick = new Intent(getApplicationContext(), SecondActivity.class);
            autoclick.putExtra("Count", countervalue);
            autoclick.putExtra("Add", Add);
            startActivity(autoclick);
        }
    });
    Home = (ImageButton) findViewById(R.id.imageView2);
    Home.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View view) {
            Intent gohome = new Intent(getApplicationContext(), MainActivity.class);
            gohome.putExtra("Count", countervalue);
            gohome.putExtra("Add", Add);
            startActivity(gohome);
        }
    });
    listView = (ListView) findViewById(R.id.List);

    final String[] bookTitles = new String[]{
            "The Alchemist",
            "The Giver",
            "How to Kill a Mockingbird",
            "Lost in Paradise",
            "The Complete Android and Java Developer...",
            "Titanic",
            "The Kite Runner",
            "Lord of the Rings",
            "The Hobbit",
            "Java in a Nutshell",
            "The Social Network",
            "Game Programming All in One"

    };

    final String[] bookPages = new String[12];

    final String[] authors = new String[]{

            "Paulo Coelho",
            "Lois Lowry",
            "Harper Lee",
            "Somell Author!",
            "Paulo and Fahd",
            "Simon Adams",
            "Khaled Hosseini",
            "J. R. R. Tolkien",
            "J. R. R. Tolkien",
            "Flannagan",
            "Ben Mezrich",
            "Harbour"

    };

    ArrayList<HashMap<String, String>> authorList = new ArrayList<>();


    //Setup adapter
    customListViewAdapter = new CustomListViewAdapter(getApplicationContext(), authorList);
    listView.setAdapter(customListViewAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            firstoption = position;


        }
    });
    if (firstoption == 0) {
        nigg++;
        str = Integer.toString(nigg);
        bookPages[firstoption] = str;
    }

    for (int i = 0; i < 10; i++) {


        HashMap<String, String> data = new HashMap<>();
        data.put("title", bookTitles[i]);
        data.put("pages", bookPages[i]);
        data.put("author", authors[i]);


        authorList.add(data);
    }
}


public void Display(int countervalue) {
    countstring = String.valueOf(countervalue);
    myCount.setText("$" + countstring);
}

}
package com.example.navjeevenmann.mytycoon;
导入android.content.Intent;
导入android.os.Bundle;
导入android.os.Handler;
导入android.support.v7.app.AppActivity;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ImageButton;
导入android.widget.ListView;
导入android.widget.TextView;
导入java.util.ArrayList;
导入java.util.HashMap;
公共类第三个活动扩展了应用程序活动{
私有列表视图列表视图;
私有int-Add;
私人价值;
私有文本视图myCount;
私有字符串countstring;
Handler=newhandler();
私人住宅;
私有图像按钮自动点击;
私人选择权;
私家侦探;
私有字符串str;
私有CustomListViewAdapter CustomListViewAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_第三);
getSupportActionBar().hide();
Bundle it=getIntent().getExtras();
countervalue=it.getInt(“Count”);
Add=it.getInt(“Add”);
myCount=(TextView)findViewById(R.id.textView3);
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
显示(计数器值);
handler.postdayed(这个,1);
}
}, 50);
自动单击=(ImageButton)findViewById(R.id.AutoClick);
AutoClick.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent autoclick=newintent(getApplicationContext(),SecondActivity.class);
自动点击putExtra(“计数”,计数器值);
自动点击putExtra(“添加”,添加);
星触觉(自动点击);
}
});
Home=(ImageButton)findViewById(R.id.imageView2);
Home.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent gohome=新Intent(getApplicationContext(),MainActivity.class);
gohome.putExtra(“计数”,计数器值);
gohome.putExtra(“添加”,添加);
星触觉(gohome);
}
});
listView=(listView)findViewById(R.id.List);
最终字符串[]书名=新字符串[]{
“炼金术士”,
“给予者”,
“如何杀死一只知更鸟”,
“失乐园”,
“完整的Android和Java开发者…”,
“泰坦尼克号”,
“追风筝的人”,
“指环王”,
“霍比特人”,
“简而言之,Java”,
“社交网络”,
“游戏编程一体机”
};
最终字符串[]bookPages=新字符串[12];
最终字符串[]作者=新字符串[]{
“保罗·科埃略”,
“Lois Lowry”,
“哈珀·李”,
“Somell作家!”,
“保罗和法赫德”,
“西蒙·亚当斯”,
“哈立德·侯赛尼”,
“J.R.R.托尔金”,
“J.R.R.托尔金”,
“弗兰纳根”,
“本·梅兹里奇”,
“海港”
};
ArrayList authorList=新的ArrayList();
//安装适配器
customListViewAdapter=新的customListViewAdapter(getApplicationContext(),authorList);
setAdapter(customListViewAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
第一选项=位置;
}
});
if(firstoption==0){
nigg++;
str=整数。toString(nigg);
bookPages[firstoption]=str;
}
对于(int i=0;i<10;i++){
HashMap数据=新的HashMap();
数据。put(“标题”,书名[i]);
数据。放入(“页面”,书页[i]);
data.put(“作者”,作者[i]);
添加(数据);
}
}
公共无效显示(int计数器值){
countstring=String.valueOf(countvalue);
myCount.setText(“$”+countstring);
}
}

根据您使用的样式判断,我猜您来自C#background(??也许我离这里很远?),在这里您没有块级只读变量,并且可以在闭包中进行变异-在Java中,如果您想在匿名类中引用它们或使用全局变量,必须声明final(我建议在本例中使用最终方法)。在本例中,这很好;只需将列表声明为最终列表,您仍然可以在可运行列表中添加到列表中。若要反映UI中的更改,请在添加到列表后立即调用customListViewAdapter.notifyDataSetChanged()