package com.example.listviewdemo;
import android.R.drawable;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Im;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView listView = null;
String s[] = {"苹果","哈密瓜","桃子","芒果","草莓","菠萝","葡萄","草莓","菠萝","葡萄"};
int data[] = {R.drawable.b0,R.drawable.b1,R.drawable.b2,R.drawable.b3,R.drawable.b4,R.drawable.b5,R.drawable.b6,R.drawable.b2,R.drawable.b3,R.drawable.b4};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.two_activity);
listView = (ListView)findViewById(R.id.listView2);
myAdapter adapter = new myAdapter();
listView.setAdapter(adapter);
}
public class myAdapter extends BaseAdapter{
@Override
public int getCount() {
return s.length;
}
@Override
public Object getItem(int position) {
return s[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View converView, ViewGroup arg2) {
converView = getLayoutInflater().inflate(R.layout.two_activity, null);
ImageView im = (ImageView) converView.findViewById(R.id.imageview1);
TextView tv = (TextView) converView.findViewById(R.id.textview1);
tv.setText(s[position]);
im.setImageResource(data[position]);
return converView;
}
}
}