android - Using an external xml file for custom list view adapter? -


i developing first android application. in app there should listview displaying @ left side pic , next 2 textviews.

the xml listview looks following:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"             android:layout_width="fill_parent"             android:layout_height="?android:attr/listpreferreditemheight"             android:padding="6dip" >  <imageview         android:id="@+id/icon"         android:layout_width="wrap_content"         android:layout_height="fill_parent"         android:layout_alignparentbottom="true"         android:layout_alignparenttop="true"         android:layout_marginright="6dip"         android:contentdescription="todo"         android:src="@drawable/ic_launcher" />  <textview         android:id="@+id/secondline"         android:layout_width="fill_parent"         android:layout_height="26dip"         android:layout_alignparentbottom="true"         android:layout_alignparentright="true"         android:layout_torightof="@id/icon"         android:ellipsize="marquee"         android:singleline="true"         android:text="description"         android:textsize="12sp" />  <textview         android:id="@+id/firstline"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:layout_above="@id/secondline"         android:layout_alignparentright="true"         android:layout_alignparenttop="true"         android:layout_alignwithparentifmissing="true"         android:layout_torightof="@id/icon"         android:gravity="center_vertical"         android:text="example application"         android:textsize="16sp" />  </relativelayout> 

the xml the mainactivity looking this:

<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"             android:paddingleft="@dimen/activity_horizontal_margin"             android:paddingright="@dimen/activity_horizontal_margin"             android:paddingtop="@dimen/activity_vertical_margin"             android:paddingbottom="@dimen/activity_vertical_margin"             tools:context=".mainactivity">  <listview xmlns:android="rowlayout"           android:id="@+id/list"           android:layout_width="wrap_content"           android:layout_height="wrap_content" />  </relativelayout> 

my mainactivity.java code looks looks following:

import android.app.listactivity; import android.os.bundle; import android.view.view; import android.widget.listview; import android.widget.toast;  public class mainactivity extends listactivity {     public void oncreate(bundle icicle) {     super.oncreate(icicle);     string[] values = new string[] { "android", "iphone", "windowsmobile",             "blackberry", "webos", "ubuntu", "windows7", "max os x",             "linux", "os/2" };     string[] values2 = new string[] {"good","bad","medium","not yet tested","don't know it","very cool","just windows","piece of shit","very cool","don't know too"};     listview listview = (listview) findviewbyid(r.id.listview);     listviewadapter adapter1 = new listviewadapter (values,values2,android.r.id.list);     listview.setadapter(adapter1);     } } 

my current custom adapter looks this:

import android.widget.baseadapter; import android.widget.textview;  public class listviewadapter extends baseadapter {  string[] values; string[] values2; context ctxt; layoutinflater layoutinflater;  public listviewadapter (string [] values,string [] values2, context context) {     values = values;     values2 = values2;     ctxt = context;     layoutinflater = (layoutinflater) context.getsystemservice (context.layout_inflater_service); }  @override public int getcount() {     return values.length; }  @override public object getitem(int position) {     string [] res = new string [] {values [position],values2 [position]};     return res; }  @override public long getitemid(int position) {     return 0; }  @override public view getview(int position, view convertview, viewgroup parent) {     textview name1 = (textview)convertview.findviewbyid(android.r.id.text1);     textview name2 = (textview)convertview.findviewbyid(android.r.id.text2);     name1.settext(values [position]);     name2.settext(values2 [position]);     return convertview; } } 

i following error when launching app:

process: de.gotthold.myapplication2.app, pid: 16723 java.lang.runtimeexception: unable start activity componentinfo{de.gotthold.myapplication2.app/de.gotthold.myapplication2.app.mainactivity}: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference @ android.app.activitythread.performlaunchactivity(activitythread.java:2411) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2474) @ android.app.activitythread.access$800(activitythread.java:144) @ android.app.activitythread$h.handlemessage(activitythread.java:1359) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:155) @ android.app.activitythread.main(activitythread.java:5727) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1029) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:824) caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.listview.setadapter(android.widget.listadapter)' on null object reference @ de.gotthold.myapplication2.app.mainactivity.oncreate(mainactivity.java:19) @ android.app.activity.performcreate(activity.java:5961) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1129) @ android.app.activitythread.performlaunchactivity(activitythread.java:2364)             at android.app.activitythread.handlelaunchactivity(activitythread.java:2474)             at android.app.activitythread.access$800(activitythread.java:144)             at android.app.activitythread$h.handlemessage(activitythread.java:1359)             at android.os.handler.dispatchmessage(handler.java:102)             at android.os.looper.loop(looper.java:155)             at android.app.activitythread.main(activitythread.java:5727)             at java.lang.reflect.method.invoke(native method)             at java.lang.reflect.method.invoke(method.java:372)             at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1029)             at com.android.internal.os.zygoteinit.main(zygoteinit.java:824) 02-06 12:45:27.641 16723-16723/? d/process﹕ killprocess, pid=16723 02-06 12:45:27.641 16723-16723/? d/process﹕ com.android.internal.os.runtimeinit$uncaughthandler.uncaughtexception:138 java.lang.threadgroup.uncaughtexception:693 java.lang.threadgroup.uncaughtexception:690

i explanation problem, not link tutorial because tried working trough them , happend. read multiple stackoverflow threads, none of solve problem.

thanks help, school-guy

p.s.: not app developing diffrent 1 messing around new learnt knowledge.

you have lot of errors in code. let me point them out

error 1

setting adapter. cannot call without setcontentview()

 listview listview = (listview) findviewbyid(r.id.listview);  listview.setadapter(adapter1); 

you should call

 setlistadapter(adapter1); 

better way of doing is:

use acticity not listactivity. in oncreate()

setconetentview(r.id.your_main_view); listview listview = (listview) findviewbyid(r.id.listview); listviewadapter adapter1 = new listviewadapter (values,values2,this); listview.setadapter(adapter1); 

error 2

your listviewadapter constructor has parameters

 (string[] values,string [], context) 

but sending

 (values,values2,android.r.id.list) 

you should instead send

 (values,values2,this)  

error 3

you not inflating view in adpater. without inflating listview.xml view cannot convert view type object, cannot create custom listview it

you should do

public static class holder  //holder class containing view components/widgets {      textview name1,name2; }  @override public view getview(int position, view convertview, viewgroup parent) { holder holder=new holder();  view convertview=inflater.inflate(r.layout.row_list, null); holder.name1 = convertview.(textview)convertview.findviewbyid(r.id.text1); holder.name2 = convertview.(textview)convertview.findviewbyid(r.id.text2); holder.name1.settext(values [position]); holder.name2.settext(values2 [position]); return convertview; } 

for more detail understanding have written example here


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -