android - Built a custom file in XML, Can't call it in Java by any means -
i working on listviews. decided make customized listview. it, xml file (customized) required. built cannot call means in java. following line row_layout red error "cannot resolve symbol "row_layout":
listadapter myadapter = new arrayadapter<string> (this, android.r.layout.row_layout, cars);
following code of customized xml file row_layout :
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/row_layout"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/mytextview1" android:textsize="32dp" android:textstyle="italic"/> </linearlayout>
following java file code :
package com.ranatalha.mylistapp; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.listadapter; import android.widget.listview; import android.widget.toast; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string [] cars= { "mehran", "corolla", "faw v2", "honda city" }; //to link above created array in list //listadapter myadapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, cars); listadapter myadapter = new arrayadapter<string> (this, android.r.layout.row_layout, cars); //referring listview createdin xml listview mylistview = (listview) findviewbyid(r.id.mylistview); //connecting listview adapter mylistview.setadapter(myadapter); //catching clicks on listview mylistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override //implements method public void onitemclick(adapterview<?> adapterview, view view, int position, long l) { string carspicked = "your picked car: " + string.valueof(adapterview.getitematposition(position)); toast.maketext(mainactivity.this, carspicked, toast.length_short).show(); } } ); } }
android.r uses built in android resources. try android.r.layout.simple_list_item1.
or create own layout , call id r.layout.
Comments
Post a Comment