android - How can I get view from ListView by position and set text to item? -


i trying implement custom list. there textview in each row, want set text textview oncreate in following way nullpointer exception.

my custom list

public class customlist extends arrayadapter<string>{  private final activity context; private final string[] web; private final integer[] imageid;   textview txttitle;  public customlist(activity context, string[] web, integer[] imageid) { super(context, r.layout.list_single, web); this.context = context; this.web = web; this.imageid = imageid; }   @override public view getview(int position, view view, viewgroup parent) {  layoutinflater inflater = context.getlayoutinflater(); view rowview= inflater.inflate(r.layout.list_single, null, true); imageview imageview = (imageview) rowview.findviewbyid(r.id.img);  txttitle = (textview) rowview.findviewbyid(r.id.txt);  txttitle.settext(web[position]); txttitle.settext("menjar ali");   imageview.setimageresource(imageid[position]); return rowview; }     } 

my main activity:

public class mainactivity extends activity {  listview list;    string[] web = {     "google plus",       "twitter",       "windows"    } ;    integer[] imageid = {       r.drawable.ic_launcher,       r.drawable.ic_launcher,       r.drawable.ic_launcher    }; textview mytextview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      customlist adapter = new customlist(mainactivity.this, web, imageid);      list=(listview)findviewbyid(r.id.list);      list.setadapter(adapter);       view vv = list.getchildat(0);      textview tt = (textview)vv.findviewbyid(r.id.txt);     tt.settext("momomo");                        //here error } 

my list_single.xml

<?xml version="1.0" encoding="utf-8"?> <tablelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <tablerow>     <imageview         android:id="@+id/img"         android:layout_width="50dp"         android:layout_height="50dp"/>     <textview         android:id="@+id/txt"         android:layout_width="wrap_content"         android:layout_height="50dp" /> </tablerow> </tablelayout> 

actually, trying implement single row listview edittext instead of edittext inside scrollview have better way of scrolling funtionality, , facing various problem in doing that.

so if have precise example edittext(inside listview) can accessed anywhere in main_activity , efficiently perform operatiopn settet, gettext, color change etc, helpful.

if want change color , value implement onitemclick of listview here vv null because position passing wrong.

in custom adapter comment out line txttitle.settext("menjar ali"); because before assigned value web array , again set value menjar ali. so, show menjar ali every row item.

code snippet

activity

public class mainactivity extends activity {  listview list;    string[] web = {     "google plus",       "twitter",       "windows"    } ;    integer[] imageid = {       r.drawable.ic_launcher,       r.drawable.ic_launcher,       r.drawable.ic_launcher    }; textview mytextview;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      customlist adapter = new customlist(mainactivity.this, web, imageid);      list=(listview)findviewbyid(r.id.list);      list.setadapter(adapter); } 

list_single.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="horizontal"     android:gravity="center_vertical" >      <imageview         android:id="@+id/img"         android:layout_width="50dp"         android:layout_height="50dp"         android:adjustviewbounds="true"         android:croptopadding="false"         android:scaletype="fitxy"         android:src="@drawable/ic_launcher" />      <textview         android:id="@+id/txt"         android:layout_width="0dp"         android:layout_height="wrap_content"         android:layout_weight="1"         android:layout_marginleft="12dp"         android:text="text" />  </linearlayout> 

adapter

public class customlist extends arrayadapter<string>{  private final activity context; private final string[] web; private final integer[] imageid;   textview txttitle;  public customlist(activity context, string[] web, integer[] imageid) { super(context, r.layout.list_single, web); this.context = context; this.web = web; this.imageid = imageid; }   @override public view getview(int position, view view, viewgroup parent) { viewholder viewholder; if(view == null) {   layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);   view = inflater.inflate(r.layout.list_single, parent, false);   viewholder = new viewholder();   viewholder.imageview = (imageview) view.findviewbyid(r.id.img);   viewholder.txttitle = (textview) view.findviewbyid(r.id.txt);   view.settag(viewholder); } else {   viewholder = (viewholder) view.gettag(); }  viewholder.txttitle.settext(web[position]); viewholder.imageview.setimageresource(imageid[position]); return view; }  private class viewholder {  textview txttitle;  imageview imageview; } } 

if want change textcolor can change in click event i.e click on textview or onitemclick of listview.

in case of changing data have modify values inside web array


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -