不断更新CustomListView适配器中的TextView
问题描述:
在这个应用程序中,用户点击屏幕获得积分,然后购买升级,我想更新textview的原因是因为我想显示用户拥有的升级数量。不断更新CustomListView适配器中的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);
,但它说,authorsList是不是从内部类中访问,需要被声明为final。我希望textview每秒更新一次,以显示升级数量。
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);
}
}
答
由你使用的样式来看,我猜你来自一个C#背景(???也许我的路要走???),您不必块级只读变量和你可以在闭包内进行变异 - 在Java中,如果要在匿名类中引用它们,或者使用全局变量(id在本例中建议最后一种方法),则必须声明最终的结果。虽然在这种情况下没问题,只需将您的列表声明为final,并且您仍然可以将其添加到Runnable中。要反映UI中的更改,请在添加到列表后立即调用customListViewAdapter.notifyDataSetChanged()。