{"rsdb":{"rid":"411015","subhead":"","postdate":"0","aid":"283826","fid":"98","uid":"1","topic":"1","content":"
\n

\u4e00\u3001\u8ba2\u5236ListView\u7684\u754c\u9762<\/p> \n

1.\u5148\u5efa\u7acb\u4e00\u4e2aFruit\u6c34\u679c\u7c7b<\/p> \n

 <\/p> \n

\n
package<\/span> com.example.listviewtest;\n\n?\n\n?\n\n<\/span>public<\/span> class<\/span> Fruit {\n\n  <\/span>private<\/span> String name;\n\n \n\n  <\/span>private<\/span> int<\/span> imageId;\n\n \n\n  <\/span>public<\/span> Fruit(String name,int<\/span> imageId) {\n\n    <\/span>this<\/span>.name=name;\n\n    <\/span>this<\/span>.imageId = imageId;\n\n  }\n\n  <\/span>public<\/span> String getName() {\n\n    <\/span>return<\/span> name;\n\n  }\n\n  <\/span>public<\/span> int<\/span> getImageId() {\n\n    <\/span>return<\/span> imageId;\n\n   \n\n  }\n\n}<\/span><\/pre> \n <\/div> \n 

 <\/p> \n

?<\/p> \n

2.\u7136\u540e\u5efa\u7acb\u4e00\u4e2a\u5c55\u793a\u6c34\u679c\u7684LayOut\u6587\u4ef6<\/p> \n

 <\/p> \n

\n
<?<\/span>xml version="1.0" encoding="utf-8"<\/span>?><\/span>\n\n<<\/span>LinearLayout <\/span>xmlns:android<\/span>="http:\/\/schemas.android.com\/apk\/res\/android"<\/span>\n\n    android:layout_width<\/span>="match_parent"<\/span>\n\n    android:layout_height<\/span>="match_parent"<\/span>\n\n    android:orientation<\/span>="vertical"<\/span> ><\/span>\n\n   \n\n<<\/span>ImageView\n\n    <\/span>android:id<\/span>="@+id\/fruit_image"<\/span>\n\n    android:layout_width<\/span>="wrap_content"<\/span>\n\n    android:layout_height<\/span>="wrap_content"<\/span> \/><\/span>\n\n?\n\n<\/span><<\/span>TextView\n\n    <\/span>android:id<\/span>="@+id\/fruit_name"<\/span>\n\n    android:layout_width<\/span>="wrap_content"<\/span>\n\n    android:layout_height<\/span>="wrap_content"<\/span>\n\n    android:layout_gravity<\/span>="center"<\/span>\n\n    android:layout_marginLeft<\/span>="10dip"<\/span> \/><\/span>\n\n   \n\n<\/<\/span>LinearLayout<\/span>><\/span><\/pre> \n <\/div> \n 

 <\/p> \n

?<\/p> \n

\u7136\u540e\u65b0\u5efa\u4e00\u4e2aFruitAdapter\u9002\u914d\u5668\uff0c\u6765\u7ee7\u627fArrayAdapter<\/p> \n

 <\/p> \n

\n
package<\/span> com.example.listviewtest;\n\n<\/span>import<\/span> android.widget.ArrayAdapter;\n\n<\/span>import<\/span> android.widget.ImageView;\n\n<\/span>import<\/span> android.widget.TextView;\n\n<\/span>import<\/span> android.content.Context;\n\n<\/span>import<\/span> java<\/a>.util.List;\n\n?\n\n<\/span>import<\/span> android.view.LayoutInflater;\n\n<\/span>import<\/span> android.view.View;\n\n<\/span>import<\/span> android.view.ViewGroup;\n\n?\n\n?\n\n<\/span>public<\/span> class<\/span> FruitAdapter extends<\/span> ArrayAdapter<Fruit> {\n\n  \n\n  <\/span>private<\/span> int<\/span> resourceId;\n\n \n\n  <\/span>public<\/span> FruitAdapter(Context context,int<\/span> textViewResourceId,List<Fruit> objects) {\n\n    <\/span>super<\/span>(context,textViewResourceId,objects);\n\n    resourceId <\/span>= textViewResourceId;\n\n  }\n\n \n\n  @Override\n\n  <\/span>public<\/span> View getView(int<\/span> position,View convertView,ViewGroup parent) {\n\n    Fruit fruit <\/span>= getItem(position);\/\/<\/span>\u83b7\u53d6\u5f53\u524d\u9879\u7684Fruit\u5b9e\u4f8b<\/span>\n\n    View view <\/span>= LayoutInflater.from(getContext()).inflate(resourceId,null<\/span>);\n\n    ImageView fruitImage <\/span>= (ImageView) view.findViewById(R.id.fruit_image);\n\n    TextView fruitName <\/span>= (TextView) view.findViewById(R.id.fruit_name);\n\n    fruitImage.setImageResource(fruit.getImageId());\n\n    fruitName.setText(fruit.getName());\n\n    <\/span>return<\/span> view;\n\n   \n\n  }\n\n}<\/span><\/pre> \n <\/div> \n 

 <\/p> \n

\u6700\u540e\u6211\u4eec\u5b8c\u5584\u6211\u4eec\u7684\u4e3b\u7a0b\u5e8f<\/p> \n

 <\/p> \n

\n
package<\/span> com.example.listviewtest;\n\n?\n\n<\/span>import<\/span> java<\/a>.util.List;\n\n?\n\n<\/span>import<\/span> android.app.Activity;\n\n<\/span>import<\/span> android.os.Bundle;\n\n<\/span>import<\/span> android.view.Menu;\n\n<\/span>import<\/span> android.view.MenuItem;\n\n<\/span>import<\/span> android.widget.ArrayAdapter;\n\n<\/span>import<\/span> android.widget.ListView;\n\n<\/span>import<\/span> java.util.*;\n\n<\/span>\/\/<\/span>import java.lang.ArrayAdapter;<\/span>\n\n?\n\n<\/span>public<\/span> class<\/span> MainActivity extends<\/span> Activity {\n\n \n\n  <\/span>private<\/span> List<Fruit> fruitList = new<\/span> ArrayList<Fruit>();\n\n \n\n<\/span>\/\/<\/span>  private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape","Pineapple","Strawberry","Cherry"};<\/span>\n\n \n\n  @Override\n\n  <\/span>protected<\/span> void<\/span> onCreate(Bundle savedInstanceState) {\n\n    <\/span>super<\/span>.onCreate(savedInstanceState);\n\n    setContentView(R.layout.activity_main);\n\n   \n\n    initFruits();<\/span>\/\/<\/span>\u521d\u59cb\u5316\u6c34\u679c\u6570\u636e<\/span>\n\n    FruitAdapter adapter <\/span>= new<\/span> FruitAdapter(MainActivity.this<\/span>,R.layout.fruit_item,fruitList);\n\n   \n\n<\/span>\/\/<\/span>    Ar","orderid":"0","title":"Android\u8fde\u8f7d3-\u5b9a\u5236ListView\u7684\u754c\u9762\u3001\u6027\u80fd\u4f18\u5316\u4ee5\u53ca\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6(\u4e00)","smalltitle":"","mid":"0","fname":"Android","special_id":"0","bak_id":"0","info":"0","hits":"65","pages":"3","comments":"0","posttime":"2020-04-08 06:14:51","list":"1586297691","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Android<\/A> \u8fde\u8f7d<\/A> \u5b9a\u5236<\/A> ListView<\/A> \u754c\u9762<\/A> \u6027\u80fd<\/A> \u4f18\u5316<\/A> \u4ee5\u53ca<\/A> \u7ed1\u5b9a<\/A> \u70b9\u51fb<\/A> \u4e8b\u4ef6<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"14.17.22.33","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Android\u8fde\u8f7d3-\u5b9a\u5236ListView\u7684\u754c\u9762\u3001\u6027\u80fd\u4f18\u5316\u4ee5\u53ca\u7ed1\u5b9a\u70b9\u51fb\u4e8b\u4ef6","lastview":"1591359696","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}