2017-08-12 1 views
0

このアプリケーションでは、ユーザーが画面をタップしてアップグレードを購入し、テキストビューを更新する理由は、ユーザーのアップグレード数を表示するためです。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は内部クラス内からアクセスできないことを言って、最終的に宣言する必要があります。テキストビューを毎秒更新して、アップグレード量の量を表示します。

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); 
} 

} 

答えて

1

私はあなたがブロックレベル読み取り専用変数を使用していていないC#の背景(???多分私は???道オフだ)から来る推測している、あなたが使うスタイルから判断しますクロージャ内から変更することができます - Javaでは、匿名クラス内でそれらを参照したい場合はファイナルを宣言するか、グローバル変数を使用します(idはこの場合は最終的なアプローチを推奨します)。しかし、この場合は問題ありません。最終的にリストを宣言するだけで、あなたのRunnable内でそれを追加することができます。 UIの変更を反映するには、リストに追加した直後にcustomListViewAdapter.notifyDataSetChanged()を呼び出します。

関連する問題