Domanda

In my application, I have grid view and I wrote a listener to get position of clicked item in the grid view. I tried many ways to solve it but it is noting working.I'm looking solutions or help I have checked everything but i'm not able to clear it.I don't know where I have done mistake. anyone help me out with is.

 rowitems 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" >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:background="@drawable/customborder"
            android:gravity="center_vertical"
            android:orientation="vertical" >

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

                <ImageView
                    android:id="@+id/product_img"
                    android:layout_width="350dp"
                    android:layout_height="150dp"
                    android:layout_alignParentLeft="true"
                    android:src="@drawable/ic_launcher" 
                    android:focusable="false"
    android:focusableInTouchMode="false"/>
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:layout_weight="0.5"
                android:background="#D7D7D7" >

                <TextView
                    android:id="@+id/product_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="37dp"
                    android:text="@string/pname"
                    android:textAppearance="?android:attr/textAppearanceMedium" 
                    android:focusable="false"
    android:focusableInTouchMode="false"/>
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="75dp" >

                <Button
                    android:id="@+id/product_decribe_cart"
                    style="?android:attr/buttonStyleSmall"
                    android:layout_width="75dp"
                    android:layout_height="30dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:layout_marginRight="46dp"
                    android:background="#D41A37"
                    android:text="@string/cart"
                    android:textColor="@android:color/white"
                    android:textSize="8dp" 
                    android:focusable="false"
    android:focusableInTouchMode="false"/>

                <TextView
                    android:id="@+id/product_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="40dp"
                    android:text="@string/rate"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#D41A37" 
                    android:focusable="false"
    android:focusableInTouchMode="false"/>
            </RelativeLayout>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="262dp"
            android:layout_height="113dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/customborder" >

            <TextView
                android:id="@+id/product_detail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="16dp"
                android:text="Product details"
                android:textAppearance="?android:attr/textAppearanceMedium" 
                android:focusable="false"
    android:focusableInTouchMode="false"/>
        </RelativeLayout>
    </LinearLayout>

    </ScrollView>

</LinearLayout>


        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.content.Intent;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.AdapterView;
        import android.widget.Button;
        import android.widget.GridView;
        import android.widget.Toast;
        import android.widget.AdapterView.OnItemClickListener;




        public class ProductActivity extends Activity 
        {


             JSONArray results;

             JSONObject jobj;

             CustomProdAdapter adapter1;


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

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

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

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

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

             Button add;

             String pid;

             GridView gView;
            @Override
            protected void onCreate(Bundle savedInstanceState) 
            {

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

                setContentView(R.layout.product_activity);

                a1=new ArrayList<String>();

                a2=new ArrayList<String>();

                a3=new ArrayList<String>();

                a4=new ArrayList<String>();

                Intent in=getIntent();

                pid=in.getStringExtra("Dialoog");

                System.out.println("productid"+pid);

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

               /* gView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                            long arg3) {
                        // TODO Auto-generated method stub
                        System.out.println("inside listener");
                    }
                });*/

                new Producttask().execute();


            }

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

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

                    pDialog = new ProgressDialog(ProductActivity.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("my link");

                    return json;

                }

                @Override
                protected void onPostExecute(JSONObject json)
                {

                    System.out.println("---------------return product 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);

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

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

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

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

                        }
                    } 
                    catch (JSONException e) 
                    {
                        e.printStackTrace();
                    }
                    adapter1  = new CustomProdAdapter(getApplicationContext(), R.layout.product_describe, pname, pimage,productdescription,pprice);



                    gView.setAdapter(adapter1);



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

                    }
                });



                    /*gv=(GridView)findViewById(R.id.product_decribe_cart);


                gv.setOnClickListener(new OnClickListener()
                {


                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(ProductActivity.this,product_details.class);

                        startActivity(i);   
                    }
                });
        */      }
            }
        }
È stato utile?

Soluzione

Grid view listner set in oncreate method

new Producttask().execute();
gView = (GridView)findViewById(R.id.pa_grid);
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);

                }
            });

Or set you click listener on row , like that

public View getView(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_product_activity, parent, false);

        vh=new ViewHolder();

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

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

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

    vh.pname.setText(c_pname.get(position));

    iloader.DisplayImage(c_pimage.get(position),  vh.image1);
    row.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

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

    }
});
    return row;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top