主要活动类别:package com.example.test;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.ArrayList;import android.app.Activity;import android.app.ProgressDialog;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.ImageView;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {
String iMages[] = {
"http://www.thebiblescholar.com/android_awesome.jpg",
"http://blogs-images.forbes.com/rogerkay/files/2011/07/Android1.jpg",
"http://cdn.slashgear.com/wp-content/uploads/2012/10/android-market-leader-smartphone.jpg",
"http://www.planmyworkshop.com/images/android.jpeg",
"http://www.androidguys.com/wp-content/uploads/2012/07/01-android2.jpg" };
String text[] = { "one", "two", "theree", "four", "five" };
ArrayList bitmap_array = new ArrayList();
private ListView listv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listv = (ListView) findViewById(R.id.listView1);
final ProgressDialog progDailog = new ProgressDialog(MainActivity.this);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
listv.setAdapter(new ListViewAdapter_test(MainActivity.this));
}
};
new Thread() {
public void run() {
for (int i = 0; i
Log.d("i-->" + i, "Url-->" + iMages[i]);
Bitmap bit = getBitmapFromURL(iMages[i]);
bitmap_array.add(bit);
}
progDailog.dismiss();
handler.sendEmptyMessage(0);
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class ListViewAdapter_test extends BaseAdapter {
private LayoutInflater mInflater;
public ListViewAdapter_test(Context con) {
// TODO Auto-generated constructor stub
mInflater = LayoutInflater.from(con);
}
public int getCount() {
// TODO Auto-generated method stub
return iMages.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
final ListContent holder;
View v = convertView;
if (v == null) {
v = mInflater.inflate(R.layout.row, null);
holder = new ListContent();
holder.name = (TextView) v.findViewById(R.id.textView1);
holder.button = (Button) v.findViewById(R.id.button1);
holder.img_p = (ImageView) v.findViewById(R.id.imageView1);
v.setTag(holder);
} else {
holder = (ListContent) v.getTag();
}
holder.name.setText(text[position]);
holder.button.setText(text[position]);
if (bitmap_array.get(position) != null) {
holder.img_p.setImageBitmap(bitmap_array.get(position));
} else {
holder.img_p.setImageDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
}
holder.button.setOnClickListener(mOnTitleClickListener_button);
holder.img_p.setOnClickListener(mOnTitleClickListener_image);
return v;
}
}
static class ListContent {
ImageView img_p;
TextView name;
Button button;
}
public OnClickListener mOnTitleClickListener_image = new OnClickListener() {
public void onClick(View v) {
final int position = listv.getPositionForView((View) v.getParent());
ImageView i = new ImageView(MainActivity.this);
i.setImageBitmap(bitmap_array.get(position));
Toast toastView = new Toast(MainActivity.this);
toastView.setView(i);
toastView.setDuration(Toast.LENGTH_LONG);
toastView.setGravity(Gravity.CENTER, 0, 0);
toastView.show();
Log.d("you are click on image view", "you are click on image view");
}
};
public OnClickListener mOnTitleClickListener_button = new OnClickListener() {
public void onClick(View v) {
final int position = listv.getPositionForView((View) v.getParent());
Log.d("you are click on image view", "you are click on image view");
Toast.makeText(MainActivity.this,
"click on button " + text[position], Toast.LENGTH_LONG)
.show();
}
};
public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url .openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}}
activity_main.xml中
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
row.xml
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/ic_launcher" />
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textAppearance="?android:attr/textAppearanceLarge" />
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click me" />
android清单文件:
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="15" />
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name=".MainActivity"
android:label="@string/title_activity_main" >