在这个应用程序中,用户点击屏幕获得积分,然后购买升级,我想更新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);
}
}
答案 0 :(得分:1)
根据你使用的风格来判断,我猜你来自C#背景(也许我已经离开???),你没有块级只读变量,你可以从内部变异一个闭包 - 在Java中你必须声明决赛,如果你想在匿名类中引用它们或使用全局变量(在这种情况下id建议最后的方法)。在这种情况下,这很好;只需将您的列表声明为final,您仍然可以在Runnable中添加它。要反映UI中的更改,请在添加到列表后立即调用customListViewAdapter.notifyDataSetChanged()。