java - Android Custom ListView with ArrayList How do I open a second activity -



java - Android Custom ListView with ArrayList How do I open a second activity -

i have custom listview arraylist , trying open sec activity using icon used in folder drawable

setonitemclicklistener(new onitemclicklistener() {}

but forcefulness closes app when click on listitem cbmain.java

package com.frostistudios.circuitbasicspro; import java.util.arraylist; import java.util.list; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.adapterview; import android.widget.listview; import android.widget.adapterview.onitemclicklistener; public class cbmain extends activity { string[] listitems = {"menu one","menu two","menu three"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_cb_main); listview lv = (listview) findviewbyid(r.id.listview); //listview in xml file list<listviewitem> items = new arraylist<cbmain.listviewitem>(); lv.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view v, int position, long id) { if(position == 0){ //call custom method opening activity openitemoneactivity(); } else if(position == 1){ openitemtwoactivity(); } else if(position == 2){ openitemthreeactivity(); } } }); items.add(new listviewitem() {{ thumbnailresource = r.drawable.ic_action_call; title = listitems[0]; subtitle = ""; }}); items.add(new listviewitem() {{ thumbnailresource = r.drawable.ic_action_call; title = listitems[1]; subtitle = ""; }}); items.add(new listviewitem() {{ thumbnailresource = r.drawable.ic_action_call; title = listitems[2]; subtitle = ""; }}); customlistviewadapter adapter = new customlistviewadapter(this, items); lv.setadapter(adapter); } //custom methods open activity public void openitemoneactivity(){ intent intent = new intent(this,itemoneactivity.class); startactivity(intent); } public void openitemtwoactivity(){ intent intent = new intent(this,itemtwoactivity.class); startactivity(intent); } public void openitemthreeactivity(){ intent intent = new intent(this,itemthreeactivity.class); startactivity(intent); } class listviewitem { public int thumbnailresource; public string title; public string subtitle; } @override public boolean oncreateoptionsmenu(menu menu) { // todo auto-generated method stub //getmenuinflater().inflate(r.menu.actionbar, menu); //return super.oncreateoptionsmenu(menu); homecoming true; } }

customlistviewadapter.java custom list adapter file

package com.frostistudios.circuitbasicspro; import java.util.list; import com.frostistudios.circuitbasicspro.cbmain.listviewitem; import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; public class customlistviewadapter extends baseadapter { layoutinflater inflater; list<listviewitem> items; public customlistviewadapter(activity context, list<listviewitem> items) { super(); this.items = items; this.inflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); } @override public int getcount() { // todo auto-generated method stub homecoming items.size(); } @override public object getitem(int position) { // todo auto-generated method stub homecoming null; } @override public long getitemid(int position) { // todo auto-generated method stub homecoming 0; } @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub listviewitem item = items.get(position); view vi=convertview; if(convertview==null) vi = inflater.inflate(r.layout.item_row, null); imageview imgthumbnail = (imageview) vi.findviewbyid(r.id.imgthumbnail); textview txttitle = (textview) vi.findviewbyid(r.id.txttitle); textview txtsubtitle = (textview) vi.findviewbyid(r.id.txtsubtitle); imgthumbnail.setimageresource(item.thumbnailresource); txttitle.settext(item.title); txtsubtitle.settext(item.subtitle); homecoming vi; } }

and xml file holds listview design

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <imageview android:id="@+id/imgthumbnail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:src="@drawable/ic_action_call" android:layout_marginleft="10dp" android:layout_margintop="2dp"/> <textview android:id="@+id/txttitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/imageview1" android:layout_torightof="@+id/imageview1" android:text="large text" android:layout_marginleft="60dp" android:layout_margintop="6dp"/> <textview android:id="@+id/txtsubtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/imageview1" android:layout_alignleft="@+id/textview1" android:layout_below="@+id/textview1" android:text="" /> <imageview android:id="@+id/imageview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_below="@+id/textview1" android:src="@drawable/arrow" /> </relativelayout>

looking log cat, error seems because theme used not compat. check manifest, , create sure utilize compat theme.

example (for acitivities in apps) :

<application //other code android:theme="@style/theme.appcompat.light" >

or if utilize action bar on particular activity :

<activity //other code android:theme="@style/theme.appcompat.light" >

tips :

always pay attending log cat, caused by. know problem there.

java android listview android-intent arraylist

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -