簡體   English   中英

如何在 TextView 中顯示 ArrayList 值?

[英]How to display ArrayList value in TextView?

我想在我的 TextView 中顯示存儲在我的 ArrayList 中的值,因此當應用程序運行時,該名稱與其他詳細信息一起出現在 TextView 中。

我可以從 ArrayList 讀取值並將它們顯示在 logcat 中。 它們也顯示在 ListView 上,但我無法讓它們顯示在 TextView 上。

private ArrayList<Country> countries;

    private ArrayList<Country> countries;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        lvCountries = findViewById(R.id.main_activity_lv_countries);
        tvDetails = findViewById(R.id.activity_country_details);
        lvCountries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //create an intent for the FlagDisplayActivity
                Intent intentBundle = new Intent(MainActivity.this, FlagDisplayActivity.class);
                Bundle bundleName = new Bundle();

                //put arg2 as an extra in the intent. arg2 represents the item clicked on
                //e.g arg2 will be 0 if the first item was clicked
                intentBundle.putExtra(countries.get(position).getName(), countries.get(position).getFlagImageResourceId());
                //start the activity
                startActivityForResult(intentBundle, DELETE_REQUEST);
            }
        });

        countries = new ArrayList<>();
        countries.add(new Country("Australia", 100, "Canberra", "English", "Dollars", R.drawable.australia));
        countries.add(new Country("China", 5771876, "Beijing", "Chinese", "Renminbi", R.drawable.china));
        countries.add(new Country("Germany", 126860301, "Berlin", "German", "Euros", R.drawable.germany));
        countries.add(new Country("India", 60550075, "New Delhi", "Indian", "Rupees", R.drawable.india));
        countries.add(new Country("UK", 100, "London", "English", "GBP", R.drawable.uk));

        for (int i = 0; i < countries.size(); i++) {
            Log.d(TAG, "onCreate: name: " + countries.get(i).getName() + " Population: " + countries.get(i).getPopulation());
        }

        //Create a String ArrayList to store the country names
        ArrayList<String> countryNames = new ArrayList<>();


        //For every Country object in the countries ArrayList
        for (Country country : countries) {
            //Add the name of the country to the String ArrayList
            countryNames.add(country.getName() + " " + country.getPopulation() + " " + country.getCapitalCity() + " " + country.getLanguage() + " " + country.getCurrency() + " " + country.getFlagImageResourceId());
        }

        //Create an ArrayAdapter using the countryNames String ArrayList
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, countryNames);
        //Set the adapter on the ListView
        lvCountries.setAdapter(adapter);

    }

TextView 中不顯示信息。

這里是 CustomAdapter 在 ListView 中顯示 arraylist 的國家/地區名稱和人口的完整工作代碼。 按照所列步驟進行操作。 已經在您的 MainActivity 中列出了 lvCountries。

1.國家.java

public class Country {

String name;int population; String capital,language,currentcy;
int image;

public Country(String name, int population, String capital, String language, String currentcy, int image) {
    this.name = name;
    this.population = population;
    this.capital = capital;
    this.language = language;
    this.currentcy = currentcy;
    this.image = image;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getPopulation() {
    return population;
}

public void setPopulation(int population) {
    this.population = population;
}

public String getCapital() {
    return capital;
}

public void setCapital(String capital) {
    this.capital = capital;
}

public String getLanguage() {
    return language;
}

public void setLanguage(String language) {
    this.language = language;
}

public String getCurrentcy() {
    return currentcy;
}

public void setCurrentcy(String currentcy) {
    this.currentcy = currentcy;
}

public int getImage() {
    return image;
}

public void setImage(int image) {
    this.image = image;
}
}
  1. 在布局中的 res 中添加 entity_country.xml

     <TextView android:id="@+id/lit_item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#000" /> <TextView android:id="@+id/lit_item_population" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#89a"/> <TextView android:id="@+id/lit_item_capital" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#f00"/> <TextView android:id="@+id/lit_item_language" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#7788ff"/> <TextView android:id="@+id/lit_item_currency" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#007700"/>

  1. CustomAdapter.java

============ 開始 ======

class CustomAdapter extends BaseAdapter {

private ArrayList<Country> _data;
Context _c;

CustomAdapter(ArrayList<Country> data, Context c) {
    _data = data;
    _c = c;
    Log.d("inside customAdapter", "inside customAdapter constructor...");
}

public int getCount() {
    // TODO Auto-generated method stub
    return _data.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return _data.get(position);
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater) _c
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.entity_country, null);
    }

    TextView lit_item_name = (TextView) v.findViewById(R.id.lit_item_name);
    TextView lit_item_population = (TextView) v.findViewById(R.id.lit_item_population);
    TextView lit_item_capital = (TextView) v.findViewById(R.id.lit_item_capital);
    TextView lit_item_language = (TextView) v.findViewById(R.id.lit_item_language);
    TextView lit_item_currency = (TextView) v.findViewById(R.id.lit_item_currency);
    ImageView list_item_image = (ImageView) v.findViewById(R.id.list_item_image);

    Log.d("tvcredit==>", " "+lit_item_name.getText().toString());



    final Country tpj = _data.get(position);
    lit_item_name.setText(tpj.name+"");
    lit_item_population.setText(tpj.population+"");
    lit_item_capital.setText(tpj.capital+"");
    lit_item_language.setText(tpj.language+"");
    lit_item_currency.setText(tpj.currentcy+"");

    list_item_image.setImageResource(tpj.image);

    LinearLayout ll_parent=(LinearLayout)v.findViewById(R.id.ll_parent);
    ll_parent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(_c,tpj.name+"\n"+tpj.capital,Toast.LENGTH_LONG).show();

            //======== code as per your requirement =========
            //Intent intent=new Intent(_c,TargetActivity.class);
            //intent.putExtra("name",tpj.name+"");
            //intent.putExtra("name",tpj.capital+"");
            //_c.startActivity(intent);
        }
    });
    return v;
}}

============結束=========

  1. MainActivity.java里面的onCreate方法只寫

     ListView lvCountries = (ListView)findViewById(R.id.lvCountries); ArrayList<Country> countries; countries = new ArrayList<>(); countries.add(new Country("Australia", 100, "Canberra", "English", "Dollars", R.drawable.australia)); countries.add(new Country("China", 5771876, "Beijing", "Chinese", "Renminbi", R.drawable.china)); countries.add(new Country("Germany", 126860301, "Berlin", "German", "Euros", R.drawable.germany)); countries.add(new Country("India", 60550075, "New Delhi", "Indian", "Rupees", R.drawable.india)); countries.add(new Country("UK", 100, "London", "English", "GBP", R.drawable.uk)); CustomAdapter customAdapter=new CustomAdapter(countries,getApplicationContext()); lvCountries.setAdapter(customAdapter);

我已經檢查過了,肯定對你有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM