本篇文章給大家分享的是有關(guān)ListView控件怎么在Android中使用,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)建站成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計(jì),響應(yīng)式網(wǎng)站,網(wǎng)頁設(shè)計(jì)師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:13518219792一、ListView的簡單用法
首先新建一個(gè)ListViewTest項(xiàng)目,并讓Android Studio自動創(chuàng)建好活動。然后修改activity_main.xml中的代碼,如下:
接下來修改MainActivity中的代碼:
public class MainActivity extends AppCompatActivity { private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapteradapter=new ArrayAdapter (MainActivity.this,android.R.layout.simple_list_item_1,data); ListView listview=(ListView)findViewById(R.id.list_view); listview.setAdapter(adapter); } }
數(shù)組中的數(shù)據(jù)無法直接傳遞給ListView,需要借助適配器來實(shí)現(xiàn)。
ArrayAdapter的構(gòu)造函數(shù)中依次傳入當(dāng)前上下文,ListView子項(xiàng)布局的id,以及要適配的數(shù)據(jù);
調(diào)用ListView的setAdapter()方法,將構(gòu)建好的適配器對象傳遞進(jìn)去,這樣ListView和數(shù)據(jù)之間的關(guān)聯(lián)就建立完成。
運(yùn)行程序,可以通過滾動的方式來查看屏幕外的數(shù)據(jù)。
二、定制ListView的界面
首先準(zhǔn)備一組圖片,分別對應(yīng)上面提供的每一種水果(注意圖片大小盡量一致),放在drawable目錄下,注意命名不能出現(xiàn)大寫字母(比如Apple不合法);
在com.example.administrator.listviewtest下新建.java文件,定義一個(gè)實(shí)體類Fruit,作為ListView適配器的適配類型。
public class Fruit{ private String name; private int imageId; public Fruit(String name,int imageId){ this.name=name; this.imageId=imageId; } public String getName(){ return name; } public int getImageId(){ return imageId; } }
然后為ListView的子項(xiàng)指定一個(gè)自定義布局,在layout目錄下新建fruit_item.xml。
接下來創(chuàng)建一個(gè)自定義的適配器,同樣在com.example.administrator.listviewtest下新建.java文件,重寫了父類的一組構(gòu)造函數(shù),用于將上下文、ListView子項(xiàng)布局的id和數(shù)據(jù)都傳遞進(jìn)來,命名為FruitAdapter,代碼如下:
public class FruitAdapter extends ArrayAdapter{ private int resourceId; public FruitAdapter(Context context, int textViewResourceId, List objects){ super(context,textViewResourceId,objects); resourceId=textViewResourceId; } @Override public View getView(int position,View convertView,ViewGroup parent){ Fruit fruit=getItem(position); //獲取當(dāng)前項(xiàng)的實(shí)例 View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false); ImageView fruitImage=(ImageView)view.findViewById(R.id.fruit_image); TextView fruitName=(TextView) view.findViewById(R.id.fruit_name); fruitImage.setImageResource(fruit.getImageId()); fruitName.setText(fruit.getName()); return view; } }
下面修改MainActivity中的代碼:
package com.example.administrator.listviewtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ListfruitList=new ArrayList<>(); // private String[] data={"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango","Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry","Mango"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initFruits(); //初始化水果數(shù)據(jù) FruitAdapter adapter=new FruitAdapter(MainActivity.this,R.layout.fruit_item,fruitList); // ArrayAdapter adapter = new ArrayAdapter (MainActivity.this, android.R.layout.simple_list_item_1, data); ListView listview = (ListView) findViewById(R.id.list_view); listview.setAdapter(adapter); } private void initFruits(){ for(int i=0;i<2;i++){ Fruit apple=new Fruit("Apple",R.drawable.apple); fruitList.add(apple); Fruit orange=new Fruit("Orange",R.drawable.orange); fruitList.add(orange); Fruit banana=new Fruit("Banana",R.drawable.banana); fruitList.add(banana); Fruit waterlenmo=new Fruit("Waterlemon",R.drawable.waterlemon); fruitList.add(waterlenmo); Fruit pear=new Fruit("Pear",R.drawable.pear); fruitList.add(pear); Fruit grape=new Fruit("Grape",R.drawable.grape); fruitList.add(grape); Fruit pineapple=new Fruit("Pineapple",R.drawable.pineapple); fruitList.add(pineapple); Fruit strawberry=new Fruit("Strawberry",R.drawable.straw); fruitList.add(strawberry); Fruit cherry=new Fruit("Cherry",R.drawable.cherry); fruitList.add(cherry); Fruit mango=new Fruit("mango",R.drawable.mango); fruitList.add(mango); } } }
以上就是ListView控件怎么在Android中使用,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。