如何实现搜索功能中的GridView在android系统 [英] how to implement search functionality in Gridview in android

查看:313
本文介绍了如何实现搜索功能中的GridView在android系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我有一个网格视图中显示他们的名字形象,现在我想要实现搜索功能的搜索选项卡。我在谷歌一直在寻找过去两天,但我没有得到任何解决方案。任何想法或教程0R我怎么能做到这一点?

任何人帮助我​​。

先谢谢了。

mainactivity

 进口的java.util.ArrayList;进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.GridView;
进口android.widget.ImageButton;进口com.example.diceapp.R;公共类SearchActivity扩展活动
{
     JSONArray结果;     的JSONObject jobj;     SearchCustomAdapter适配器1;     的ImageButton IB;     ArrayList的<串GT; A1,A2,A3,A4;     ArrayList的<串GT; NAME =新的ArrayList<串GT;();     ArrayList的<串GT;说明=新的ArrayList<串GT;();     ArrayList的<串GT;图像=新的ArrayList<串GT;();     ArrayList的<串GT;价格=新的ArrayList<串GT;();     按钮添加;     串PID;     GridView控件gView;     SRC的EditText;     INT长度限制:Textlength = 0;     ArrayList的<串GT; p_name =新的ArrayList<串GT;();
     ArrayList的<串GT; p_image =新的ArrayList<串GT;();
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState)
    {        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);        的setContentView(R.layout.search_activity);        A1 =新的ArrayList<串GT;();        A2 =新的ArrayList<串GT;();        A3 =新的ArrayList<串GT;();        A4 =新的ArrayList<串GT;();        SRC =(EditText上)findViewById(R.id.srcetxt);        /*src.addTextChangedListener(new TextWatcher()
        {            @覆盖
            公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){
                //当用户更改文本
                。SearchActivity.this.adapter1.getFilter()过滤器(CS);
            }            @覆盖
            公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,
                    INT ARG3){
                // TODO自动生成方法存根            }            @覆盖
            公共无效afterTextChanged(编辑为arg0){
                // TODO自动生成方法存根
            }
        }); * /        新Searchtask()执行();        gView =(GridView控件)findViewById(R.id.gridView1);        gView.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,ARG1查看,INT位置,
                    长ARG3){
                // TODO自动生成方法存根
                的System.out.println(外面点击+位置);            }
        });       IB =(的ImageButton)findViewById(R.id.imageButton1);    }    私有类Searchtask扩展的AsyncTask<字符串,字符串,JSONObject的>
    {
        私人ProgressDialog pDialog;        @覆盖
        在preExecute保护无效()
        {
            super.on preExecute();            pDialog =新ProgressDialog(SearchActivity.this);            pDialog.setMessage(加载数据......);            pDialog.setIndeterminate(假);            pDialog.setCancelable(真);            pDialog.show();
        }        @覆盖
        受保护的JSONObject doInBackground(字符串参数... args)
        {
            JSONParser jParser =新JSONParser();            JSONObject的JSON = jParser.getJSONFromUrl(HTTP:link.php);            返回JSON;        }        @覆盖
        保护无效onPostExecute(JSON的JSONObject)
        {            的System.out.println(---------------回报名单JSON ------------+ JSON);            pDialog.dismiss();            尝试
            {
                jobj = json.getJSONObject(回应);
                //获取联系人的数组
                结果= jobj.getJSONArray(obejects);                的System.out.println(在JSON后产品的活动);
                //通过所有联系人循环
                的for(int i = 0; I< results.length();我++)
                {
                    JSONObject的C = results.getJSONObject(I)                    name.add(c.getString(名字));                    image.add(c.getString(图像));                     price.add(c.getString(价格));                    description.add(c.getString(描述));                }
            }
            赶上(JSONException E)
            {
                e.printStackTrace();
            }
            适配器1 =新SearchCustomAdapter(getApplicationContext(),R.layout.gridlistimg,名称,图片,说明价格);            gView.setAdapter(适配器1);        }
    }}

baseadapter类

 包com.example.diceapp.search;
进口的java.util.ArrayList;进口android.content.Context;
进口android.content.Intent;
进口android.sax.StartElementListener;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.TextView;
进口android.widget.AdapterView.OnItemClickListener;进口com.example.diceapp.AAAMainActivity;
进口com.example.diceapp.R;
进口com.example.diceapp.category.ImageLoader;公共类SearchCustomAdapter延伸BaseAdapter
{
    上下文语境;    LayoutInflater吹气;    ArrayList的<串GT; aaaa1,bbbb1,cccc1;
    ArrayList的<串GT; c_pname;    ArrayList的<串GT; c_pimage;    ArrayList的<串GT; c_pprice;    ArrayList的<串GT; c_pdescription;    的String [] data_text;
    的String [] data_image;    ImageLoader的iloader;    公共SearchCustomAdapter(上下文C中,int productDescribe,ArrayList的<串GT; PNAME,ArrayList的<串GT; pimage,
            ArrayList的<串GT;产品描述,ArrayList的<串GT; pprice)
    {
        // TODO自动生成构造函数存根        this.context = C;        this.c_pname = PNAME;        this.c_pprice = pprice;        this.c_pdescription =产品描述;        this.c_pimage = pimage;         吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         iloader =新ImageLoader的(context.getApplicationContext());
    }
    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回c_pname.size();
        //返回data_text.length;
    }@覆盖
    公共对象的getItem(INT为arg0){
        // TODO自动生成方法存根
        返回null;    }    @覆盖
    众长getItemId(INT POS){
        // TODO自动生成方法存根
        //返回c_pname.indexOf(的getItem(POS)); //为arg0;
        返回POS;
    }    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
    {
        // TODO自动生成方法存根
        查看排= convertView;        ViewHolder VH;        如果(行== NULL)
        {
            行= inflater.inflate(R.layout.custom_search_activity,父母,假);            VH =新ViewHolder();            vh.pname =(TextView中)row.findViewById(R.id.product_name);            vh.pprice =(TextView中)row.findViewById(R.id.textView2);            vh.image1 =(ImageView的)row.findViewById(R.id.imageView1);            vh.btn =(按钮)row.findViewById(R.id.p_custom_tv);
            /*row.setOnClickListener(new OnClickListener(){                @覆盖
                公共无效的onClick(查看为arg0){                     的System.out.println(外面点击+位置);
                     ArrayList的<串GT; sendingary =新的ArrayList<串GT;();                     sendingary.add(c_pname.get(位置));                     sendingary.add(c_pprice.get(位置));                     sendingary.add(c_pimage.get(位置));                     sendingary.add(c_pdescription.get(位置));                     意图CP =新意图(context.getApplicationContext(),product_details.class);                     cp.putStringArrayListExtra(aryvalue,sendingary);                     cp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                     context.startActivity(CP);                }
            }); * /            row.setTag(VH);
        }
        其他
        {
            VH =(ViewHolder)row.getTag();
            排= convertView;
        }        vh.pname.setText(c_pname.get(位置));
        vh.pprice.setText(KD+ c_pprice.get(位置));
        iloader.DisplayImage(c_pimage.get(位置),vh.image1);        返回行;
    }    公共静态类ViewHolder
    {
         TextView的PNAME,pprice;         ImageView的image1的;         按钮BTN;
    }    公共SearchCustomAdapter用getFilter(){
        // TODO自动生成方法存根
        返回null;
    }
}


解决方案

您好我只是编辑code请更换,并检查

SearchActivity.java

 进口的java.util.ArrayList;
进口java.util.Iterator的;进口org.json.JSONArray;
进口org.json.JSONException;
进口org.json.JSONObject;进口android.app.Activity;
进口android.app.ProgressDialog;
进口android.os.AsyncTask;
进口android.os.Bundle;
进口android.text.Editable;
进口android.text.TextWatcher;
进口android.view.View;
进口android.widget.AdapterView;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.GridView;
进口android.widget.ImageButton;公共类SearchActivity延伸活动{    JSONArray结果;    的JSONObject jobj;    SearchCustomAdapter适配器1;    的ImageButton IB;    ArrayList的<串GT; A1,A2,A3,A4;    ArrayList的<串GT; NAME =新的ArrayList<串GT;();    ArrayList的<串GT;说明=新的ArrayList<串GT;();    ArrayList的<串GT;图像=新的ArrayList<串GT;();    ArrayList的<串GT;价格=新的ArrayList<串GT;();    按钮添加;    串PID;    GridView控件gView;    SRC的EditText;    INT长度限制:Textlength = 0;    ArrayList的<串GT; p_name =新的ArrayList<串GT;();
    ArrayList的<串GT; p_image =新的ArrayList<串GT;();    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){        // TODO自动生成方法存根
        super.onCreate(savedInstanceState);        的setContentView(R.layout.search_activity);        A1 =新的ArrayList<串GT;();        A2 =新的ArrayList<串GT;();        A3 =新的ArrayList<串GT;();        A4 =新的ArrayList<串GT;();        SRC =(EditText上)findViewById(R.id.srcetxt);        src.addTextChangedListener(新TextWatcher(){            @覆盖
            公共无效onTextChanged(CharSequence的CS,INT ARG1,ARG2 INT,INT ARG3){            }            @覆盖
            公共无效beforeTextChanged(CharSequence的为arg0,ARG1 INT,INT ARG2,诠释ARG3){            }            @覆盖
            公共无效afterTextChanged(编辑为arg0){
                如果(。src.getText()的toString()修剪()长()0){
                    applySearch(src.getText()的toString()修剪());
                }其他{
                    adapter1.setC_pname(名);
                    adapter1.setC_pdescription(介绍);
                    adapter1.setC_pimage(图片);
                    adapter1.setC_pprice(价);
                    adapter1.notifyDataSetChanged();
                }
            }
        });        新Searchtask()执行();        gView =(GridView控件)findViewById(R.id.gridView1);        gView.setOnItemClickListener(新OnItemClickListener(){            @覆盖
            公共无效onItemClick(适配器视图<>为arg0,ARG1查看,INT位置,长ARG3){
                // TODO自动生成方法存根
                的System.out.println(外面点击+位置);            }
        });        IB =(的ImageButton)findViewById(R.id.imageButton1);    }    私有类Searchtask扩展的AsyncTask<字符串,字符串,JSONObject的> {
        私人ProgressDialog pDialog;        @覆盖
        在preExecute保护无效(){
            super.on preExecute();            pDialog =新ProgressDialog(SearchActivity.this);            pDialog.setMessage(加载数据......);            pDialog.setIndeterminate(假);            pDialog.setCancelable(真);            pDialog.show();
        }        @覆盖
        受保护的JSONObject doInBackground(字符串参数... args){
            JSONParser jParser =新JSONParser();            JSONObject的JSON = jParser.getJSONFromUrl(HTTP:link.php);            返回JSON;        }        @覆盖
        保护无效onPostExecute(JSON的JSONObject){            的System.out.println(---------------回报名单JSON ------------+ JSON);            pDialog.dismiss();            尝试{
                jobj = json.getJSONObject(回应);
                //获取联系人的数组
                结果= jobj.getJSONArray(obejects);                的System.out.println(在JSON后产品的活动);
                //通过所有联系人循环
                的for(int i = 0; I< results.length();我++){
                    JSONObject的C = results.getJSONObject(I)                    name.add(c.getString(名字));                    image.add(c.getString(图像));                    price.add(c.getString(价格));                    description.add(c.getString(描述));                }
            }赶上(JSONException E){
                e.printStackTrace();
            }
            适配器1 =新SearchCustomAdapter(getApplicationContext(),R.layout.gridlistimg,名称,图片,说明价格);            gView.setAdapter(适配器1);        }
    }    私人无效applySearch(字符串searchStr){
        ArrayList的<串GT; sea​​rchname =新的ArrayList<串GT;();
        ArrayList的<串GT; sea​​rchdescription =新的ArrayList<串GT;();
        ArrayList的<串GT; sea​​rchimage =新的ArrayList<串GT;();
        ArrayList的<串GT; sea​​rchprice =新的ArrayList<串GT;();        的for(int i = 0; I< name.size();我++){
            如果(name.get(ⅰ)。载(searchStr)){
                sea​​rchname.add(name.get(I));
                sea​​rchdescription.add(description.get(I));
                sea​​rchimage.add(image.get(I));
                sea​​rchprice.add(price.get(I));
            }
        }        adapter1.setC_pname(searchname);
        adapter1.setC_pdescription(searchdescription);
        adapter1.setC_pimage(searchimage);
        adapter1.setC_pprice(searchprice);
        adapter1.notifyDataSetChanged();
    }}

SearchCustomAdapter.java

 进口的java.util.ArrayList;进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.TextView;公共类SearchCustomAdapter延伸BaseAdapter {
    上下文语境;    LayoutInflater吹气;    ArrayList的<串GT; aaaa1,bbbb1,cccc1;    ArrayList的<串GT; c_pname;
    ArrayList的<串GT; c_pprice;
    ArrayList的<串GT; c_pdescription;
    ArrayList的<串GT; c_pimage;    公共无效setC_pname(ArrayList的<串GT; c_pname){
        this.c_pname = c_pname;
    }    公共无效setC_pimage(ArrayList的<串GT; c_pimage){
        this.c_pimage = c_pimage;
    }    公共无效setC_pprice(ArrayList的<串GT; c_pprice){
        this.c_pprice = c_pprice;
    }    公共无效setC_pdescription(ArrayList的<串GT; c_pdescription){
        this.c_pdescription = c_pdescription;
    }    的String [] data_text;
    的String [] data_image;    ImageLoader的iloader;    公共SearchCustomAdapter(上下文C中,int productDescribe,ArrayList的<弦乐> PNAME,ArrayList的<弦乐> pimage,ArrayList的<弦乐>产品描述,ArrayList的<弦乐> pprice){
        // TODO自动生成构造函数存根        this.context = C;        this.c_pname = PNAME;        this.c_pprice = pprice;        this.c_pdescription =产品描述;        this.c_pimage = pimage;        吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        iloader =新ImageLoader的(context.getApplicationContext());    }    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回c_pname.size();
        //返回data_text.length;
    }    @覆盖
    公共对象的getItem(INT为arg0){
        // TODO自动生成方法存根
        返回null;    }    @覆盖
    众长getItemId(INT POS){
        // TODO自动生成方法存根
        //返回c_pname.indexOf(的getItem(POS)); //为arg0;
        返回POS;
    }    @覆盖
    公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
        // TODO自动生成方法存根
        查看排= convertView;        ViewHolder VH;        如果(行== NULL){
            行= inflater.inflate(R.layout.custom_search_activity,父母,假);            VH =新ViewHolder();            vh.pname =(TextView中)row.findViewById(R.id.product_name);            vh.pprice =(TextView中)row.findViewById(R.id.textView2);            vh.image1 =(ImageView的)row.findViewById(R.id.imageView1);            vh.btn =(按钮)row.findViewById(R.id.p_custom_tv);            / *
             * row.setOnClickListener(新OnClickListener(){
             *
             * @覆盖公共无效的onClick(查看为arg0){
             *
             *的System.out.println(关于境外点击+位置);
             *
             *
             * ArrayList的<串GT; sendingary =新的ArrayList<串GT;();
             *
             * sendingary.add(c_pname.get(位置));
             *
             * sendingary.add(c_pprice.get(位置));
             *
             * sendingary.add(c_pimage.get(位置));
             *
             * sendingary.add(c_pdescription.get(位置));
             *
             *意图CP =新
             *意图(context.getApplicationContext(),product_details.class);
             *
             * cp.putStringArrayListExtra(aryvalue,sendingary);
             *
             * cp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             *
             * context.startActivity(CP);
             *
             *}});
             * /            row.setTag(VH);
        }其他{
            VH =(ViewHolder)row.getTag();
            排= convertView;
        }        vh.pname.setText(c_pname.get(位置));
        vh.pprice.setText(KD+ c_pprice.get(位置));
        iloader.DisplayImage(c_pimage.get(位置),vh.image1);        返回行;
    }    公共静态类ViewHolder {
        TextView的PNAME,pprice;        ImageView的image1的;        按钮BTN;
    }}

In my application I have search tab which display images with their name in grid view and now I want to implement search functionality. I have been searching in google for past two days but I didn't get any solution. Any idea or tutorial 0r how can I achieve this?

Anyone help me with this.

Thanks in advance.

mainactivity

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;

import com.example.diceapp.R;

public class SearchActivity extends Activity 
{


     JSONArray results;

     JSONObject jobj;

     SearchCustomAdapter adapter1;

     ImageButton ib;

     ArrayList<String> a1,a2,a3,a4;

     ArrayList<String> name=new ArrayList<String>();

     ArrayList<String> description=new ArrayList<String>();

     ArrayList<String> image=new ArrayList<String>();

     ArrayList<String> price=new ArrayList<String>();

     Button add;

     String pid;

     GridView gView;

     EditText src;

     int textlength = 0;

     ArrayList<String> p_name = new ArrayList<String>();
     ArrayList<String> p_image = new ArrayList<String>();


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.search_activity);

        a1=new ArrayList<String>();

        a2=new ArrayList<String>();

        a3=new ArrayList<String>();

        a4=new ArrayList<String>();

        src=(EditText)findViewById(R.id.srcetxt);

        /*src.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                SearchActivity.this.adapter1.getFilter().filter(cs);  
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub                         
            }
        });*/

        new Searchtask().execute();

        gView = (GridView)findViewById(R.id.gridView1);

        gView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                // TODO Auto-generated method stub
                System.out.println("outside on click"+position);

            }
        });

       ib=(ImageButton)findViewById(R.id.imageButton1);

    }

    private class Searchtask extends AsyncTask<String, String, JSONObject> 
    {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();

            pDialog = new ProgressDialog(SearchActivity.this);

            pDialog.setMessage("Loading Data ...");

            pDialog.setIndeterminate(false);

            pDialog.setCancelable(true);

            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... args) 
        {
            JSONParser jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl("http:link.php");

            return json;

        }

        @Override
        protected void onPostExecute(JSONObject json)
        {

            System.out.println("---------------return name list json------------"+json);

            pDialog.dismiss();

            try 
            {
                jobj=json.getJSONObject("response");
                // Getting Array of Contacts
                results = jobj.getJSONArray("obejects");

                System.out.println("In product Activity after JSON");
                // looping through All Contacts
                for(int i = 0; i < results.length(); i++)
                {
                    JSONObject c = results.getJSONObject(i);

                    name.add(c.getString("name"));

                    image.add(c.getString("image"));

                     price.add(c.getString("price"));

                    description.add(c.getString("Description"));

                }
            } 
            catch (JSONException e) 
            {
                e.printStackTrace();
            }
            adapter1  = new SearchCustomAdapter(getApplicationContext(), R.layout.gridlistimg, name, image,description,price);

            gView.setAdapter(adapter1);

        }
    }

}

baseadapter class

package com.example.diceapp.search;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.example.diceapp.AAAMainActivity;
import com.example.diceapp.R;
import com.example.diceapp.category.ImageLoader;

public class SearchCustomAdapter extends BaseAdapter
{
    Context context;

    LayoutInflater inflater;

    ArrayList<String> aaaa1,bbbb1,cccc1;


    ArrayList<String> c_pname;

    ArrayList<String> c_pimage;

    ArrayList<String> c_pprice;

    ArrayList<String> c_pdescription;

    String[] data_text;
    String[] data_image;

    ImageLoader iloader;

    public SearchCustomAdapter(Context c, int productDescribe,ArrayList<String> pname, ArrayList<String> pimage,
            ArrayList<String> productdescription, ArrayList<String> pprice)
    {
        // TODO Auto-generated constructor stub



        this.context=c;

        this.c_pname=pname;

        this.c_pprice=pprice;

        this.c_pdescription=productdescription;

        this.c_pimage=pimage;

         inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

         iloader= new ImageLoader(context.getApplicationContext());


    }


    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return c_pname.size();
        //return data_text.length;
    }

@Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;

    }

    @Override
    public long getItemId(int pos) {
        // TODO Auto-generated method stub
        //return  c_pname.indexOf(getItem(pos));//arg0;
        return pos;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        View row=convertView; 

        ViewHolder vh;

        if(row==null)
        {
            row=inflater.inflate(R.layout.custom_search_activity, parent, false);

            vh=new ViewHolder();

            vh.pname=(TextView)row.findViewById(R.id.product_name);

            vh.pprice=(TextView)row.findViewById(R.id.textView2);

            vh.image1=(ImageView) row.findViewById(R.id.imageView1);

            vh.btn=(Button)row.findViewById(R.id.p_custom_tv);


            /*row.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {

                     System.out.println("outside on click"+position);


                     ArrayList<String> sendingary=new ArrayList<String>();

                     sendingary.add(c_pname.get(position));

                     sendingary.add(c_pprice.get(position));

                     sendingary.add(c_pimage.get(position));

                     sendingary.add(c_pdescription.get(position));

                     Intent cp=new Intent(context.getApplicationContext(),product_details.class);

                     cp.putStringArrayListExtra("aryvalue", sendingary);

                     cp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                     context.startActivity(cp);

                }
            });*/

            row.setTag(vh);
        }
        else 
        {
            vh = (ViewHolder) row.getTag();
            row=convertView;
        }

        vh.pname.setText(c_pname.get(position));
        vh.pprice.setText("KD "+c_pprice.get(position));        
        iloader.DisplayImage(c_pimage.get(position),  vh.image1);

        return row;
    }



    public static class ViewHolder
    {
         TextView pname,pprice;

         ImageView image1;

         Button btn;
    }

    public SearchCustomAdapter getFilter() {
        // TODO Auto-generated method stub
        return null;
    }
}

解决方案

Hi I just edit your code please replace and check it

SearchActivity.java

import java.util.ArrayList;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageButton;

public class SearchActivity extends Activity {

    JSONArray results;

    JSONObject jobj;

    SearchCustomAdapter adapter1;

    ImageButton ib;

    ArrayList<String> a1, a2, a3, a4;

    ArrayList<String> name = new ArrayList<String>();

    ArrayList<String> description = new ArrayList<String>();

    ArrayList<String> image = new ArrayList<String>();

    ArrayList<String> price = new ArrayList<String>();

    Button add;

    String pid;

    GridView gView;

    EditText src;

    int textlength = 0;

    ArrayList<String> p_name = new ArrayList<String>();
    ArrayList<String> p_image = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.search_activity);

        a1 = new ArrayList<String>();

        a2 = new ArrayList<String>();

        a3 = new ArrayList<String>();

        a4 = new ArrayList<String>();

        src = (EditText) findViewById(R.id.srcetxt);

        src.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {

            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

            @Override
            public void afterTextChanged(Editable arg0) {
                if (src.getText().toString().trim().length() > 0) {
                    applySearch(src.getText().toString().trim());
                } else {
                    adapter1.setC_pname(name);
                    adapter1.setC_pdescription(description);
                    adapter1.setC_pimage(image);
                    adapter1.setC_pprice(price);
                    adapter1.notifyDataSetChanged();
                }
            }
        });

        new Searchtask().execute();

        gView = (GridView) findViewById(R.id.gridView1);

        gView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                // TODO Auto-generated method stub
                System.out.println("outside on click" + position);

            }
        });

        ib = (ImageButton) findViewById(R.id.imageButton1);

    }

    private class Searchtask extends AsyncTask<String, String, JSONObject> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(SearchActivity.this);

            pDialog.setMessage("Loading Data ...");

            pDialog.setIndeterminate(false);

            pDialog.setCancelable(true);

            pDialog.show();
        }

        @Override
        protected JSONObject doInBackground(String... args) {
            JSONParser jParser = new JSONParser();

            JSONObject json = jParser.getJSONFromUrl("http:link.php");

            return json;

        }

        @Override
        protected void onPostExecute(JSONObject json) {

            System.out.println("---------------return name list json------------" + json);

            pDialog.dismiss();

            try {
                jobj = json.getJSONObject("response");
                // Getting Array of Contacts
                results = jobj.getJSONArray("obejects");

                System.out.println("In product Activity after JSON");
                // looping through All Contacts
                for (int i = 0; i < results.length(); i++) {
                    JSONObject c = results.getJSONObject(i);

                    name.add(c.getString("name"));

                    image.add(c.getString("image"));

                    price.add(c.getString("price"));

                    description.add(c.getString("Description"));

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            adapter1 = new SearchCustomAdapter(getApplicationContext(), R.layout.gridlistimg, name, image, description, price);

            gView.setAdapter(adapter1);

        }
    }

    private void applySearch(String searchStr) {
        ArrayList<String> searchname = new ArrayList<String>();
        ArrayList<String> searchdescription = new ArrayList<String>();
        ArrayList<String> searchimage = new ArrayList<String>();
        ArrayList<String> searchprice = new ArrayList<String>();

        for (int i = 0; i < name.size(); i++) {
            if (name.get(i).contains(searchStr)) {
                searchname.add(name.get(i));
                searchdescription.add(description.get(i));
                searchimage.add(image.get(i));
                searchprice.add(price.get(i));
            }
        }

        adapter1.setC_pname(searchname);
        adapter1.setC_pdescription(searchdescription);
        adapter1.setC_pimage(searchimage);
        adapter1.setC_pprice(searchprice);
        adapter1.notifyDataSetChanged();
    }

}

SearchCustomAdapter.java

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class SearchCustomAdapter extends BaseAdapter {
    Context context;

    LayoutInflater inflater;

    ArrayList<String> aaaa1, bbbb1, cccc1;

    ArrayList<String> c_pname;
    ArrayList<String> c_pprice;
    ArrayList<String> c_pdescription;
    ArrayList<String> c_pimage;

    public void setC_pname(ArrayList<String> c_pname) {
        this.c_pname = c_pname;
    }

    public void setC_pimage(ArrayList<String> c_pimage) {
        this.c_pimage = c_pimage;
    }

    public void setC_pprice(ArrayList<String> c_pprice) {
        this.c_pprice = c_pprice;
    }

    public void setC_pdescription(ArrayList<String> c_pdescription) {
        this.c_pdescription = c_pdescription;
    }

    String[] data_text;
    String[] data_image;

    ImageLoader iloader;

    public SearchCustomAdapter(Context c, int productDescribe, ArrayList<String> pname, ArrayList<String> pimage, ArrayList<String> productdescription, ArrayList<String> pprice) {
        // TODO Auto-generated constructor stub

        this.context = c;

        this.c_pname = pname;

        this.c_pprice = pprice;

        this.c_pdescription = productdescription;

        this.c_pimage = pimage;

        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        iloader = new ImageLoader(context.getApplicationContext());

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return c_pname.size();
        // return data_text.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;

    }

    @Override
    public long getItemId(int pos) {
        // TODO Auto-generated method stub
        // return c_pname.indexOf(getItem(pos));//arg0;
        return pos;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row = convertView;

        ViewHolder vh;

        if (row == null) {
            row = inflater.inflate(R.layout.custom_search_activity, parent, false);

            vh = new ViewHolder();

            vh.pname = (TextView) row.findViewById(R.id.product_name);

            vh.pprice = (TextView) row.findViewById(R.id.textView2);

            vh.image1 = (ImageView) row.findViewById(R.id.imageView1);

            vh.btn = (Button) row.findViewById(R.id.p_custom_tv);

            /*
             * row.setOnClickListener(new OnClickListener() {
             * 
             * @Override public void onClick(View arg0) {
             * 
             * System.out.println("outside on click"+position);
             * 
             * 
             * ArrayList<String> sendingary=new ArrayList<String>();
             * 
             * sendingary.add(c_pname.get(position));
             * 
             * sendingary.add(c_pprice.get(position));
             * 
             * sendingary.add(c_pimage.get(position));
             * 
             * sendingary.add(c_pdescription.get(position));
             * 
             * Intent cp=new
             * Intent(context.getApplicationContext(),product_details.class);
             * 
             * cp.putStringArrayListExtra("aryvalue", sendingary);
             * 
             * cp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             * 
             * context.startActivity(cp);
             * 
             * } });
             */

            row.setTag(vh);
        } else {
            vh = (ViewHolder) row.getTag();
            row = convertView;
        }

        vh.pname.setText(c_pname.get(position));
        vh.pprice.setText("KD " + c_pprice.get(position));
        iloader.DisplayImage(c_pimage.get(position), vh.image1);

        return row;
    }

    public static class ViewHolder {
        TextView pname, pprice;

        ImageView image1;

        Button btn;
    }

}

这篇关于如何实现搜索功能中的GridView在android系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆