android - how to solve the following code incorrect execution of adding customlist to gridView clickable? -
i unable rectify following code problem past 2 days. don't know how add custom list items existing grid view. have completed , compiled code without errors. unfortunately clickable not going on after clicking grid view letters. requirement ex:if press grid view letter["a"] should go go layout , should display list of items[any list/icon menu/list icons]. please me out of problem soon. code have using:
gridviewactivity.java
public class gridviewactivity extends activity { gridview gridview; static final string[] numbers = new string[]{ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.content_main); gridview = (gridview) findviewbyid(r.id.gridview1); arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, numbers); gridview.setadapter(adapter); gridview.setonitemclicklistener(new adapterview.onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { toast.maketext(getapplicationcontext(), ""+position, toast.length_short).show(); intent intent = null; if(position == 0){ intent = new intent(v.getcontext(), listactivity.class); // intent.putextra("title", list.add(list)); } if(position == 1){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 2){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 3){ intent = new intent(v.getcontext(), listactivity.class); } if(position ==4){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 5){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 6){ intent = new intent(v.get context(), list activity.class);} if(position == 7){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 8){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 9){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 10){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 11){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 12) { intent = new intent(v.getcontext(), listactivity.class); } if(position == 13){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 14){ intent = new intent(v.get context(), list activity.class); } if(position == 15){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 16){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 17){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 18){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 19){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 20){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 21){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 22){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 23){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 23){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 24){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 25){ intent = new intent(v.getcontext(), listactivity.class); } if(position == 26){ intent = new intent(v.getcontext(), listactivity.class); } startactivity(intent); } }); } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:entries="@array/list" android:layout_centerhorizontal="true" > </listview> </relativelayout>
listactivity.java:
package com.example.brahmaiah.grid_view; import android.app.activity; import android.os.bundle; import android.view.menu; /** * created brahmaiah on 1/29/2016. */ public class listactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_grid_view, menu); return true; } }
content_main.xml
<?xml version="1.0" encoding="utf-8"?> <gridview xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gridview1" android:numcolumns="auto_fit" android:gravity="center" android:columnwidth="50dp" android:stretchmode="columnwidth" android:layout_width="fill_parent" android:layout_height="fill_parent" > </gridview>
i think these content enough understand problem of app code.. o/p coming ...grid view has been displaying after clicking on grid view items {a,b...}is displaying message this.i.e unfortunately list activity closed
first cleanup code little bit eliminating unnecessary "if" blocks. change following line in code:
intent = new intent(v.getcontext(), listactivity.class);
to
intent = new intent(getapplicationcontext(), listactivity.class);
edit: additionally check listactivity in manifest:
<activity android:name=".listactivity"></activity>
Comments
Post a Comment