3

我遵循本教程http://wptrafficanalyzer.in/blog/android-lazy-loading-images-and-text-in-listview-from-http-json-data/

我想知道,我如何单击列表视图项并在 imageview 中显示标志的图像

4

1 回答 1

8

检查下面的代码它可能会帮助您单击列表视图项目。

我检查这个链接

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

    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. */

        }
    };

编辑答案:2012 年 10 月 18 日

完整示例

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

主要活动类:

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> 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>

行.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>

安卓清单文件:

<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>
于 2012-10-10T07:12:59.347 回答