Reputation: 31
In this app the user taps the screen to gain points and then buys upgrades and the reason I want to update a textview is because I want to show the quantity of upgrades that the user has.
I have tried to use something like this
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);
but it says that authorsList is not accessible from within inner class, needs to be declared final. I want the textview to update every second so it shows the quantity of the amount of upgrades.
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);
}
}
Upvotes: 0
Views: 77
Reputation: 109
Judging by the style you use, I'm guessing you come from a C# background (???maybe I'm way off???) where you don't have block level readonly variables and you can mutate from within a closure - in Java you must declare finals if you want to reference them inside an anonymous class OR use a global variable (id recommend the final approach in this case). That's fine though in this case; just declare your List as final and you can still add to it within your Runnable. To reflect the change in your UI, call customListViewAdapter.notifyDataSetChanged() immediately after you add to the list.
Upvotes: 1