如何在TextView中显示ArrayList的值?

我想在 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);

            }

        });


信息不会显示在 TextView 中。


繁华开满天机
浏览 87回答 1
1回答

当年话下

这是通过 CustomAdapter 在 ListView 中的数组列表中显示国家名称和人口的完整工作代码。请按照列出的步骤操作。已在您的 MainActivity 中列出查看 lvCountries。1.Country.javapublic 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) {&nbsp; &nbsp; this.name = name;&nbsp; &nbsp; this.population = population;&nbsp; &nbsp; this.capital = capital;&nbsp; &nbsp; this.language = language;&nbsp; &nbsp; this.currentcy = currentcy;&nbsp; &nbsp; this.image = image;}public String getName() {&nbsp; &nbsp; return name;}public void setName(String name) {&nbsp; &nbsp; this.name = name;}public int getPopulation() {&nbsp; &nbsp; return population;}public void setPopulation(int population) {&nbsp; &nbsp; this.population = population;}public String getCapital() {&nbsp; &nbsp; return capital;}public void setCapital(String capital) {&nbsp; &nbsp; this.capital = capital;}public String getLanguage() {&nbsp; &nbsp; return language;}public void setLanguage(String language) {&nbsp; &nbsp; this.language = language;}public String getCurrentcy() {&nbsp; &nbsp; return currentcy;}public void setCurrentcy(String currentcy) {&nbsp; &nbsp; this.currentcy = currentcy;}public int getImage() {&nbsp; &nbsp; return image;}public void setImage(int image) {&nbsp; &nbsp; this.image = image;}}在布局中的res中添加entity_country.xml<TextView&nbsp; &nbsp; android:id="@+id/lit_item_name"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; android:textColor="#000"&nbsp; &nbsp; /><TextView&nbsp; &nbsp; android:id="@+id/lit_item_population"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; android:textColor="#89a"/><TextView&nbsp; &nbsp; android:id="@+id/lit_item_capital"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; android:textColor="#f00"/><TextView&nbsp; &nbsp; android:id="@+id/lit_item_language"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; android:textColor="#7788ff"/><TextView&nbsp; &nbsp; android:id="@+id/lit_item_currency"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:textSize="15sp"&nbsp; &nbsp; android:textColor="#007700"/>CustomAdapter.java============开始======class CustomAdapter extends BaseAdapter {private ArrayList<Country> _data;Context _c;CustomAdapter(ArrayList<Country> data, Context c) {&nbsp; &nbsp; _data = data;&nbsp; &nbsp; _c = c;&nbsp; &nbsp; Log.d("inside customAdapter", "inside customAdapter constructor...");}public int getCount() {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; return _data.size();}public Object getItem(int position) {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; return _data.get(position);}public long getItemId(int position) {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; return position;}public View getView(int position, View convertView, ViewGroup parent) {&nbsp; &nbsp; // TODO Auto-generated method stub&nbsp; &nbsp; View v = convertView;&nbsp; &nbsp; if (v == null) {&nbsp; &nbsp; &nbsp; &nbsp; LayoutInflater vi = (LayoutInflater) _c&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getSystemService(Context.LAYOUT_INFLATER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; v = vi.inflate(R.layout.entity_country, null);&nbsp; &nbsp; }&nbsp; &nbsp; TextView lit_item_name = (TextView) v.findViewById(R.id.lit_item_name);&nbsp; &nbsp; TextView lit_item_population = (TextView) v.findViewById(R.id.lit_item_population);&nbsp; &nbsp; TextView lit_item_capital = (TextView) v.findViewById(R.id.lit_item_capital);&nbsp; &nbsp; TextView lit_item_language = (TextView) v.findViewById(R.id.lit_item_language);&nbsp; &nbsp; TextView lit_item_currency = (TextView) v.findViewById(R.id.lit_item_currency);&nbsp; &nbsp; ImageView list_item_image = (ImageView) v.findViewById(R.id.list_item_image);&nbsp; &nbsp; Log.d("tvcredit==>", " "+lit_item_name.getText().toString());&nbsp; &nbsp; final Country tpj = _data.get(position);&nbsp; &nbsp; lit_item_name.setText(tpj.name+"");&nbsp; &nbsp; lit_item_population.setText(tpj.population+"");&nbsp; &nbsp; lit_item_capital.setText(tpj.capital+"");&nbsp; &nbsp; lit_item_language.setText(tpj.language+"");&nbsp; &nbsp; lit_item_currency.setText(tpj.currentcy+"");&nbsp; &nbsp; list_item_image.setImageResource(tpj.image);&nbsp; &nbsp; LinearLayout ll_parent=(LinearLayout)v.findViewById(R.id.ll_parent);&nbsp; &nbsp; ll_parent.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(_c,tpj.name+"\n"+tpj.capital,Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //======== code as per your requirement =========&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Intent intent=new Intent(_c,TargetActivity.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //intent.putExtra("name",tpj.name+"");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //intent.putExtra("name",tpj.capital+"");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //_c.startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; return v;}}===========结束=========在 MainActivity.java 里面的 onCreate 方法中只需编写&nbsp; &nbsp; 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);我已经查过了,肯定对你有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java