7

I'm using gridview inside a Listview, but I have a focusable problem.

I have set the width of the gridview, but I fill some items of gridview, on the left space of gridview (which is blank) means items not fill of gridview in listview. If I click on it, it does not perform a listitem click

Enter image description here

As given in the image, I want to perform a listview item click if it is clicked anywhere in the list item. I want to get a listitem click.

But when I click on ListView item, that is, GridView then the ListItem click is not working...

public class History extends Activity {

    String name, id, description, count, total;

    ArrayList<String> TAG_ID = new ArrayList<String>();
    ArrayList<String> TAG_COFFEESHOP_NAME = new ArrayList<String>();
    ArrayList<String> TAG_COUNT = new ArrayList<String>();
    ArrayList<String> TAG_TOTAL = new ArrayList<String>();

    Context context;
    JSONArray CoffeeUser = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listlayout);
        ListView listView = (ListView) findViewById(R.id.listnew);
        listView.setAdapter(new MyCustomAdapter());
        listView.setTextFilterEnabled(true);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> paramAnonymousAdapterView, View paramAnonymousView, int paramAnonymousInt, long paramAnonymousLong)
            {
                Intent intent = new Intent();
                intent.putExtra("coffeeshopid", ((TextView)paramAnonymousView.findViewById(R.id.hlcoffeeshopid)).getText() );
                intent.setClass(getParent(), Stamps.class);
                HistoryStack hisStack = (HistoryStack) getParent();
                hisStack.push("Stamps", intent);
            }
        });
    }

    class MyCustomAdapter extends BaseAdapter {
        Context ctx;

        public MyCustomAdapter() {
            // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();
            SharedPreferences prfs = getSharedPreferences("GUID_FILE_NAME", Context.MODE_PRIVATE);
                JSONObject json = jParser.methodhistory("http://api.com");
            try {
                // Getting Array of Employee
                CoffeeUser = json.getJSONArray("CoffeeUser");

                // Looping of List
                for (int i = 0; i < CoffeeUser.length(); i++) {
                    JSONObject c = CoffeeUser.getJSONObject(i);

                    // Storing each json item in variable
                    id = c.getString("CS_Id");
                    name = c.getString("ShopName");
                    count = c.getString("totalstamps");
                    total = c.getString("threshholdcount");

                    // Adding all get values into array
                    if (name != "null") {
                        TAG_COFFEESHOP_NAME.add(name);
                        TAG_ID.add(id);
                        TAG_TOTAL.add(total);
                        TAG_COUNT.add(count);
                    }
                }
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public int getCount() {
            return TAG_COFFEESHOP_NAME.size();
        }

        @Override
        public Object getItem(int paramInt) {
            return TAG_COFFEESHOP_NAME.get(paramInt);
        }

        @Override
        public long getItemId(int paramInt) {
            return paramInt;
        }

        public class MyCustomHolder {
            public GridView localGrid;
            public TextView CoffeeShopName,coffeeshopsdescription,coffeeshopid;
        }

        @Override
        public View getView(int paramInt, View paramView,ViewGroup paramViewGroup) {
            View localView = paramView;
            MyCustomHolder holder = null;
            if (localView == null) {
                LayoutInflater inflater = (LayoutInflater) History.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                localView = inflater.inflate(R.layout.historylist, null);
                holder = new MyCustomHolder();
                holder.CoffeeShopName = (TextView) localView.findViewById(R.id.hlcoffeeshopname);
                holder.coffeeshopid = (TextView) localView.findViewById(R.id.hlcoffeeshopid);
                holder.localGrid = (GridView) localView.findViewById(R.id.gridViewdistorylist);
                localView.setTag(holder);
            }
            else {
                holder = (MyCustomHolder) localView.getTag();
            }
            holder.CoffeeShopName.setText(TAG_COFFEESHOP_NAME.get(paramInt));
            holder.coffeeshopid.setText(TAG_ID.get(paramInt));
            holder.localGrid.setAdapter(new GridAdapterA(History.this, TAG_TOTAL.get(paramInt), TAG_COUNT.get(paramInt)));
            holder.localGrid.setFocusable(false);
            holder.localGrid.setFocusableInTouchMode(false);
            holder.CoffeeShopName.setFocusable(false);
            holder.CoffeeShopName.setFocusableInTouchMode(false);
            localView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     Intent intent = new Intent();
                        intent.putExtra("coffeeshopid", ((TextView)v.findViewById(R.id.hlcoffeeshopid)).getText() );
                          intent.setClass(getParent(), Stamps.class);
                          HistoryStack hisStack = (HistoryStack) getParent();
                          hisStack.push("Stamps", intent);
                }
            });

            return localView;
        }
    }

    public class GridAdapterA extends BaseAdapter {

        private Context context;
        String total,count;

        public GridAdapterA(Context context) {
            this.context = context;
        }

        public GridAdapterA(Context context, String total, String count) {
            // TODO Auto-generated constructor stub
            this.context = context;
            this.total = total;
            this.count = count;
        }

        public boolean areAllItemsEnabled()
        {
            return false;
        }

        public boolean isEnabled(int position)
        {
            return false;
        }

        public View getView(int position, View convertView, ViewGroup parent) {

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

            View gridView;

            if (convertView == null) {
                gridView = new View(context);
                gridView = inflater.inflate(R.layout.customgrid_row, null);
                gridView.setFocusable(false);
                ImageView imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
                imageView.setFocusable(false);
                int i = 0;
                if(count!="null")
                i = Integer.parseInt(count);

                if (position<i ) {
                    //imageView.setImageResource(R.drawable.ii);
                    //    textView.setText(gridlist[position]);
                }
                else {
                    imageView.setImageResource(R.drawable.changedcup);
                    // textView.setText("");
                }
            }
            else {
                gridView = (View) convertView;
            }
            /*gridView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.putExtra("coffeeshopid", ((TextView)v.findViewById(R.id.hlcoffeeshopid)).getText() );
                intent.setClass(getParent(), Stamps.class);
                HistoryStack hisStack = (HistoryStack) getParent();
                hisStack.push("Stamps", intent);    }
            });*/

            return gridView;
        }

        @Override
        public int getCount() {
            int j=0;
            if(total!="null"){
            j = Integer.parseInt(total);
            }
            return j;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }
    }
}

historylist

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/list_selector"
    android:orientation="horizontal"
    android:padding="5dip" >

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:background="@drawable/image_bg"
        android:padding="3dip" >

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip"
            android:src="@drawable/rcup" />
    </LinearLayout>

    <!-- hlcoffeeshopname Of Song -->

    <TextView
        android:id="@+id/hlcoffeeshopname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="Rihanna Love the way lie"
        android:textColor="#040404"
        android:textSize="20dip"
        android:textStyle="bold"
        android:typeface="sans" />

    <TextView
       android:id="@+id/hlcoffeeshopid"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:visibility="gone"
    />

    <GridView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/gridViewdistorylist"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_below="@+id/hlcoffeeshopname"
        android:layout_toRightOf="@+id/thumbnail"
        android:columnWidth="20dp"
        android:background="@null"
        android:descendantFocusability="blocksDescendants"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:numColumns="10"
        android:stretchMode="none" >
    </GridView>
</RelativeLayout>

New GetView

@Override
    public View getView(int paramInt,
                        View paramView,
                        ViewGroup paramViewGroup) {
        View localView = paramView;
        MyCustomHolder holder = null;
        if (localView == null) {
            LayoutInflater inflater = (LayoutInflater) CopyOfHistory.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            localView = inflater.inflate(R.layout.copyhistorylist, null);
            holder = new MyCustomHolder();
            holder.coffeeShopName = (TextView) localView.findViewById(R.id.hlcoffeeshopname);
            holder.coffeeshopid = (TextView) localView.findViewById(R.id.hlcoffeeshopid);
            localView.setTag(holder);
        }
        else {
            holder = (MyCustomHolder) localView.getTag();
        }
        holder.coffeeShopName.setText(TAG_COFFEESHOP_NAME.get(paramInt));
        holder.coffeeshopid.setText(TAG_ID.get(paramInt));

        int looplimit = Integer.parseInt(TAG_TOTAL.get(paramInt));
        for (int i = 0; i < looplimit; i++) {
            Log.e("loop", String.valueOf(looplimit));
            ImageView imageView = new ImageView(CopyOfHistory.this);
            if (i < Integer.parseInt(TAG_COUNT.get(paramInt))) {
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.ii));
            } else {
                imageView.setImageDrawable(getResources().getDrawable(R.drawable.iii));
            }
            RelativeLayout layout = (RelativeLayout) localView.findViewById(R.id.hlrlayout);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30,30);
            params.setMargins(i*40, 0, 0, 0);
            imageView.setLayoutParams(params);
            layout.addView(imageView);
            //holder.relativeLayout = new RelativeLayout();
        }

        holder.coffeeShopName.setFocusable(false);
        holder.coffeeShopName.setFocusableInTouchMode(false);
        localView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent();
                intent.putExtra("coffeeshopid", ((TextView) v
                        .findViewById(R.id.hlcoffeeshopid)).getText());
                intent.setClass(getParent(), Stamps.class);
                HistoryStack hisStack = (HistoryStack) getParent();
                hisStack.push("Stamps", intent);
            }
        });

        return localView;
    }
2
  • God! It's very simple. send onClickListener to adapter and set it to your inside gridView. Commented Aug 20, 2013 at 15:52
  • Do not use a GridView just to show a couple of coffee cups beside each other! It's way too complicated. Commented Aug 21, 2013 at 5:15

5 Answers 5

2

You might have solved it, but I have another solution that may help someone.

Use android:descendantFocusability="beforeDescendants" in the root layout of your list_cell XML.

Sign up to request clarification or add additional context in comments.

Comments

1

Order to have lists within other lists. You must generate a custom view. If you want one the simple, consisting of a Java class and an XML file, then in the code you only have to instantiate it and add it to a linear layout.

Here I leave a small example.

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/txtAltaPropinaTextoAyudaTipo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:text="-"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtListadoZonasNombreZona"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:layout_toLeftOf="@+id/txtAltaPropinaTextoAyudaTipo"
        android:layout_toRightOf="@+id/txtAltaPropinaTextoAyudaTipo"
        android:singleLine="true"
        android:text="TextView"
        android:textColor="#000000" />
</RelativeLayout>

Java code

public class CVTexto extends RelativeLayout {

    public TextView txtTexto;
    public int idCv;

    public CVTexto(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        IniciarCuadro();
    }

    public CVTexto(Context context, AttributeSet attrs) {

        super(context, attrs);
        IniciarCuadro();
    }

    public CVTexto(Context context, AttributeSet attrs, int defStyle) {

        super( context, attrs, defStyle );
        IniciarCuadro();
    }

    private void IniciarCuadro()
    {
        //Utilizamos el layout 'control_login' como interfaz del control
        String infService = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater li =
            (LayoutInflater)getContext().getSystemService(infService);
        li.inflate(R.layout.ll_adapter_listado_zonas_asignadas_usuario, this, true);

        AsignarElemetos();
        //Obtenemoslas referencias a los distintos control

        //Asociamos los eventos necesarios
        // asignarEventos();
    }

    private void AsignarElemetos(){
        txtTexto = (TextView)findViewById(R.id.txtListadoZonasNombreZona);
    }
}

Add to linearlayout:

cvTextp objCV = new  cvTexto(context);
LinearLayout.addView(objCv);

And delete views:

LinearLayout.removeallViews;

1 Comment

then you just have to generate the click within the CustomViews own if you want or you can even take them out,
1

I got this answer by changing the grid layout to LinearLayout. I added items dynamically into linearLayout, and then my problem was solved.

Comments

0

I have done research on it and I get to know that inside a scrollable widget which uses an adapter you should not use any other widget which is scrollable which also uses an adapter such as gridview inside listview because of touch events complexity.

So I think you should move to another approach for this. In your case, you can add a linear layout and can add cup images dynamically into that layout by setting parameters. I hope this helped you.

6 Comments

wait i post my new latest coode
application force close. i have given the id to realitive layout and replace localView click listener with layout
why does it force closed?
Hey, i got another solution. Try seting your buttons(or any other views you want to handle click inside a list item) like this: android:focusable="false" android:focusableInTouchMode="false" I didn't test this, but please let me know if this works.
Hey Rohit, i have done research on it and i get to know that inside a scrollable widget which uses adapter you should not use any other widget which is scrollable which also uses adapter such as gridview inside listview because of touch events complexity.so i think you should move to another approach for this.In your case you can add relative layout and can add dynamically cup images into that layout by setting parameters.I hope this helped you.
|
0

I don't know how your below code is working, but if I would like handle each on-click listener of each then I would do something like below.

for (int i = 0; i < looplimit; i++) {

    Log.e("loop", String.valueOf(looplimit));
    ImageView imageView = new ImageView(CopyOfHistory.this);
    if (i < Integer.parseInt(TAG_COUNT.get(paramInt))) {
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.ii));
    } 
    else {
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.iii));
    }
    RelativeLayout layout = (RelativeLayout) localView.findViewById(R.id.hlrlayout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30,30);
    params.setMargins(i*40, 0, 0, 0);
    imageView.setLayoutParams(params);
    layout.addView(imageView);
    //holder.relativeLayout = new RelativeLayout();

    // Handle your each on-click of dynamic added view while they added in getview() method
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          // Do what is necessary
        }
    });
}

I don't know how your below code is working, but if I would like handle each on-click listener of each then I would do something like below.

for (int i = 0; i < looplimit; i++) {

    Log.e("loop", String.valueOf(looplimit));
    ImageView imageView = new ImageView(CopyOfHistory.this);
    if (i < Integer.parseInt(TAG_COUNT.get(paramInt))) {
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.ii));
    } 
    else {
        imageView.setImageDrawable(getResources().getDrawable(R.drawable.iii));
    }
    RelativeLayout layout = (RelativeLayout) localView.findViewById(R.id.hlrlayout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30,30);
    params.setMargins(i*40, 0, 0, 0);
    imageView.setLayoutParams(params);
    layout.addView(imageView);
    //holder.relativeLayout = new RelativeLayout();

    // Handle your each on-click of dynamic added view while they added in getview() method
    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          // Do what is necessary
        }
    });
}

2 Comments

i dnt want to handle onclick, i want to only show the cups in row , in this all count of cups same which came last , suppose i have last item count is 7 , then all rows shows 7 cups, but actually they are dynamically generated
@Rohit Please change your question's title . it would be more easier anyone to understand your question depth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.