作业6
package com.example.ou; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.BaseAdapter; import android.widget.ImageView; public class MainActivity extends Activity { String []name={"京东商城","QQ","qq斗地主","新浪微博","天猫","UC浏览器","微信"}; private int[] picture = { R.drawable.asd, R.drawable.dfg, R.drawable.sdf, R.drawable.fgh, R.drawable.ghj, R.drawable.hjk,R.drawable.jkl}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.listView1); Myadapter myadapter= new Myadapter(); lv.setAdapter(myadapter); } class Myadapter extends BaseAdapter{ @Override public int getCount() { // TODO Auto-generated method stub return name.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return name[position]; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub View view=View.inflate(MainActivity.this, R.layout.list_item, null); TextView tv=(TextView)view.findViewById(R.id.name); ImageView iv=(ImageView)view.findViewById(R.id. iv); tv.setText(name[position]); iv.setBackgroundResource(picture[position]); return view; } } } <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.homework6.MainActivity" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" > </ListView> </RelativeLayout> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <ImageView android:id="@+id/iv" android:layout_width="50dp" android:layout_height="50dp" android:layout_alignParentLeft="true" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:layout_toRightOf="@+id/iv" android:layout_centerVertical="true"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#000" /> </RelativeLayout> </RelativeLayout>