Gridview监听器在android中不起作用

时间:2014-05-14 20:42:16

标签: android gridview onitemclicklistener

我在搜索活动中有一个gridview,我想为它添加监听器。我试图为网格视图设置Onitem click监听器,但它不起作用。我试过在网格适配器类中添加监听器,但我没有得到正确的值。我试过以下

1.setfocusable= true 
set Touchfocusable =true in button


2.I have set layout android:descendantFocusability="blocksDescendants" in RelativeLayout.


I don't know where I have done the mistake, anyone help me out with this.

搜索活动xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="102dp" 
        android:descendantFocusability="blocksDescendants">

        <ImageButton
            android:id="@+id/simageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/srcetxt"
            android:layout_toRightOf="@+id/srcetxt"
            android:src="@drawable/searchicon"
            android:background="@android:color/transparent"
            />

        <EditText
            android:id="@+id/srcetxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="15dp"
            android:background="@drawable/searchbar"
            android:ems="10"
            android:gravity="left"
            android:hint="@string/search"
            android:paddingLeft="7dp"
            android:paddingTop="7dp"
            android:textSize="15dp"
            android:singleLine="true"
            >


        </EditText>



    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:descendantFocusability="blocksDescendants">

        <GridView
            android:id="@+id/sgridView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:numColumns="auto_fit" 
            android:clickable="true"
             android:focusable="true">

        </GridView>

    </RelativeLayout>

</LinearLayout>

Custom_search活动xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

     <LinearLayout
        android:layout_width="150dp"
        android:layout_height="200dp"
        android:background="@drawable/customborder"
        android:layout_marginTop="5dp"        
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="150dp"
                android:layout_height="200dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_margin="5dp"
                 />

    </RelativeLayout>

     <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="30dp"
         android:layout_margin="5dp"
         android:background="#AAAAAA" >
            <ScrollView android:layout_height="match_parent"
                android:layout_width="match_parent">

         <TextView
             android:id="@+id/product_name"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="center"
             android:text="@string/pname"
             android:textAppearance="?android:attr/textAppearanceMedium"
             android:textSize="10sp" />

         </ScrollView>

    </RelativeLayout>

     <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content" >

         <TextView
             android:id="@+id/textView2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignParentLeft="true"
             android:layout_centerVertical="true"
             android:layout_margin="5dp"
             android:text="@string/rate"
             android:textAppearance="?android:attr/textAppearanceMedium"
             android:textColor="#D41A37"
             android:textSize="12sp" />

         <Button
             android:id="@+id/p_custom_tv"
             style="?android:attr/buttonStyleSmall"
             android:layout_width="wrap_content"
             android:layout_height="15dp"
             android:layout_centerVertical="true"
             android:layout_marginLeft="25dp"
             android:layout_toRightOf="@+id/textView2"
             android:background="#D41A37"
             android:text="@string/cart"
             android:textColor="@android:color/white"
             android:textSize="8dp" />

         <TextView
             android:id="@+id/catid"
             android:layout_width="1dp"
             android:layout_height="1dp"
             android:layout_alignParentLeft="true"
             android:layout_below="@+id/textView2"
             android:text="Small Text"
             android:textAppearance="?android:attr/textAppearanceSmall" />
     </RelativeLayout>

    </LinearLayout>

</LinearLayout>

gridlistimg

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/searchimg"
        android:layout_width="150dp"
        android:layout_height="200dp"

         />

    <TextView
        android:id="@+id/searchtxt"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
         />



</LinearLayout>

Searchactivity class

public class SearchActivity extends Activity 
{

    JSONArray results;

    JSONObject jobj;

    SearchCustomAdapter adapter1;


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

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

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

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

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

    Button add;

    String pid;

    GridView gsView;

    EditText src;

    int textlength = 0;

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

    @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();

        gsView =(GridView)findViewById(R.id.sgridView1);
        gsView.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "clicked at"+position,Toast.LENGTH_SHORT).show();

                System.out.println("outside on click in search position" + position);
                System.out.println("product activity");
                if(Global.language=="en")
                {

                sendingary=new ArrayList<String>();
                sendingary.add(name.get(position));

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

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

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

                System.out.println(" gridview lisetner ");

                }
                else if(Global.language=="ar")
                {
                    sendingary=new ArrayList<String>();
                    sendingary.add(name.get(position));

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

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

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

                    System.out.println(" gridview lisetner");

                }
                else
                {
                    System.out.println("check your program");
                }
                Global.cid=1;
                Intent cp=new Intent(getApplicationContext(),product_details.class);

                cp.putStringArrayListExtra("aryvalue", sendingary);

                cp.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                startActivity(cp);

            }
        });


    }

    class Searchtask extends AsyncTask<Void, Integer, Integer> {
        private ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {

            pDialog = new ProgressDialog(SearchActivity.this);

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

            pDialog.setIndeterminate(false);

            pDialog.setCancelable(true);

            pDialog.show();
        }

        @Override
        protected Integer doInBackground(Void... params) {
            try {



                JSONParser jParser = new JSONParser();

                JSONObject json = jParser.getJSONFromUrl("***link***");

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

                    System.out.println("In search Activity after JSON");

                    for (int i = 0; i < results.length(); i++) 
                    {
                        JSONObject c = results.getJSONObject(i);


                        String en = c.getString("product_name");

                        String pattern = "^[A-Za-z.0-9 '+&-]+$";
                        if (en.matches(pattern)) {

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

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

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

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


                        }
                        else {


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

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

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

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

                        } 

                        Log.d("englishtitle====>>>", ""+name.toString());
                        Log.d("englishdescription", ""+description.toString());

                        Log.d("arabictitle====>>>", ""+name1.toString()); 
                        Log.d("arabicdescription====>>>", ""+description1.toString());


                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }
            return null;

        }

        @Override
        protected void onPostExecute(Integer result) {

            pDialog.dismiss();

            if(Global.language.equals("en"))
            {
            adapter1 = new SearchCustomAdapter(getApplicationContext(), R.layout.gridlistimg, name, image, description, price);
            gsView.setAdapter(adapter1);

            }
            else if(Global.language=="ar")
            {
                adapter1 = new SearchCustomAdapter(getApplicationContext(), R.layout.gridlistimg, name, image, description, price);
                gsView.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>();

        System.out.println("<-------------In search function-------->"+name);

        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();
    }

}

我的适配器类

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.setTag(vh);
        } else {
            vh = (ViewHolder) row.getTag();
            row = convertView;
        }

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

        return row;
    }

    public static class ViewHolder {
        TextView pname, pprice;

        ImageView image1;

        Button btn;
    }

}

0 个答案:

没有答案