ListView单击以在ImageView中显示图像

我按照这个教程http://wptrafficanalyzer.in/blog/android-lazy-loading-images-and-text-in-listview-from-http-json-data/ 。

我不知道,我怎么能点击列表视图项目,并在ImageView中显示国旗的形象

检查下面的代码,它可以帮助你点击列表视图项目。

我检查这个链接 。

我假设你知道自定义列表视图。

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 a_product_id.size(); } public Object getItem(int position) { // TODO Auto-generated method stub // return product_id1.size(); return position; } public long getItemId(int position) { // TODO Auto-generated method stub // return product_id1.get(position).hashCode(); 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.scan_row1, null); holder = new ListContent(); holder.name = (TextView) v.findViewById(R.id.sc_textname); holder.name1 = (TextView) v.findViewById(R.id.sc_review); holder.ratings = (RatingBar) v.findViewById(R.id.sc_ratingBar1); holder.total_rate = (Button) v.findViewById(R.id.button1); holder.img_p = (ImageView) v.findViewById(R.id.image_prod); // holder.total_rate.setOnClickListener(mOnTitleClickListener1); v.setTag(holder); } else { holder = (ListContent) v.getTag(); } holder.total_rate.setOnClickListener(mOnTitleClickListener3); holder.img_p.setOnClickListener(mOnTitleClickListener_image); return v; } } static class ListContent { ImageView img_p; TextView name1; TextView name; RatingBar ratings; Button total_rate; } public OnClickListener mOnTitleClickListener3 = new OnClickListener() { public void onClick(View v) { final int position = list_v .getPositionForView((View) v.getParent()); Log.d("you are click on Ratings","you are click on Ratings"); } }; public OnClickListener mOnTitleClickListener_image = new OnClickListener() { public void onClick(View v) { final int position = list_v .getPositionForView((View) v.getParent()); Log.d("you are click on image view","you are click on image view"); /** in this listener you can show image in any other activity or in toast or in Alert. do whatever you want. */ } }; 

编辑答案:18/10/2012

完整的例子

在这里输入图像描述

在这里输入图像描述

在这里输入图像描述

主要活动等级:

 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[] = { "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.comhttp://img.dovov.comandroid.jpeg", "http://www.androidguys.com/wp-content/uploads/2012/07/01-android2.jpg" }; String text[] = { "one", "two", "theree", "four", "five" }; ArrayList<Bitmap> bitmap_array = new ArrayList<Bitmap>(); 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 < iMages.length; 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中

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> 

row.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello" android:textAppearance="?android:attr/textAppearanceLarge" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="click me" /> </LinearLayout> 

Android清单文件:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>