multithreading - Custom ListView Item with Thread in Android -
i created custom listview custom list view adapter. want set current time in every list item in list. items in list box must update current time every second.
mainactivity.java
package com.example.examapp; import java.sql.time; import java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.calendar; import java.util.date; import java.util.list; import java.util.locale; import com.example.exambp.*; import android.r.string; import android.os.bundle; import android.app.activity; import android.content.intent; import android.content.pm.labeledintent; import android.content.res.configuration; import android.database.sqlite.sqlitedatabase; import android.text.format.dateformat; import android.util.log; import android.view.menu; import android.view.view; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.listadapter; import android.widget.listview; import android.widget.textview; public class mainactivity extends activity { listview lv; activity act=this; list<listviewitem> items; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); items=new arraylist<listviewitem>(); lv=(listview)findviewbyid(r.id.listview1); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); homecoming true; } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); } public void showdata(view v) { intentintegrator i=new intentintegrator(this); i.initiatescan(); } string[] onedata; public void onactivityresult(int requestcode, int resultcode, intent intent) { intentresult scanresult = intentintegrator.parseactivityresult(requestcode, resultcode, intent); if (scanresult != null) { string x=scanresult.getcontents(); edittext et=(edittext)findviewbyid(r.id.edittext1); et.settext(x); addtolist(x); } } listviewitem li; customlistviewadapter adapter; public void addtolist(string str) { onedata=str.split("/"); li=new listviewitem(); li.enrollid="enrollmentid: "+onedata[0].tostring(); li.examid="examid: "+onedata[1].tostring(); li.userid="userid: "+onedata[2].tostring(); li.startedtime=onedata[3].tostring(); li.duration=onedata[4].tostring(); li.availabletime="jiukjh"; items.add(li); adapter=new customlistviewadapter(this, items); lv.setadapter(adapter); } }
activity_main.xml
<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" tools:context=".mainactivity" > <edittext android:id="@+id/edittext1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentright="true" android:layout_marginleft="60dp" android:ems="10" android:inputtype="text" > <requestfocus /> </edittext> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:onclick="showdata" android:text="@string/btn_val" /> <linearlayout android:id="@+id/linearlayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true" android:orientation="vertical" > </linearlayout> <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textview9" android:layout_torightof="@+id/linearlayout2" > </listview> <textview android:id="@+id/textview9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/button1" android:text="started exams" android:textappearance="?android:attr/textappearancelarge" /> </relativelayout>
item_row.xml
<?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" > <textview android:id="@+id/txtavailability" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="textview" android:textappearance="?android:attr/textappearancelarge" /> <textview android:id="@+id/txtenrollmentid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/txtavailability" android:text="textview" /> <textview android:id="@+id/txtuserid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/txtenrollmentid" android:layout_alignbottom="@+id/txtenrollmentid" android:layout_marginleft="78dp" android:layout_torightof="@+id/txtavailability" android:text="textview" /> <textview android:id="@+id/txtexamid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/txtenrollmentid" android:text="textview" /> <textview android:id="@+id/txtstarttime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/txtexamid" android:layout_alignbottom="@+id/txtexamid" android:layout_alignleft="@+id/txtuserid" android:text="textview" /> </relativelayout>
customlistviewadapter.java
package com.example.examapp; import java.util.date; import java.util.list; import com.example.exambp.*; 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; activity act; public customlistviewadapter(activity context, list<listviewitem> items) { super(); this.items = items; this.inflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); this.act=context; } @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; } view vi; @override public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub listviewitem item=items.get(position); vi=convertview; if(convertview==null) vi = inflater.inflate(r.layout.item_row, null); //imageview imgthumbnail=(imageview)vi.findviewbyid(r.id.imgthumbnail); textview txtavailabletime1=(textview)vi.findviewbyid(r.id.txtavailability); textview txtenrollmentid1=(textview)vi.findviewbyid(r.id.txtenrollmentid); textview txtuserid1=(textview)vi.findviewbyid(r.id.txtuserid); textview txtexamid1=(textview)vi.findviewbyid(r.id.txtexamid); textview txtstartedtime1=(textview)vi.findviewbyid(r.id.txtstarttime); //imgthumbnail.setimageresource(item.thumbnailresource); txtavailabletime1.settext(item.availabletime.tostring()); txtenrollmentid1.settext(item.enrollid.tostring()); txtuserid1.settext(item.userid.tostring()); txtexamid1.settext(item.examid.tostring()); txtstartedtime1.settext(item.startedtime.tostring()); thread mythread = null; runnable runnable = new countdownrunner(); mythread= new thread(runnable); mythread.start(); homecoming vi; } public void dowork() { act.runonuithread(new runnable() { public void run() { try{ textview txtcurrenttime=(textview)vi.findviewbyid(r.id.txtavailability); date dt = new date(); int hours = dt.gethours(); int minutes = dt.getminutes(); int seconds = dt.getseconds(); string curtime = hours + ":" + minutes + ":" + seconds; txtcurrenttime.settext(curtime); }catch (exception e) { //textview txtcurrenttime=(textview)vi.findviewbyid(r.id.txtavailability); //txtcurrenttime.settext(e.getmessage()); } } }); } class countdownrunner implements runnable{ // @override public void run() { while(true){ seek { dowork(); thread.sleep(1000); } grab (interruptedexception e) { thread.currentthread().interrupt(); }catch(exception e){ } } } } }
this run in 1 time. want alter current time in list items continuously.
every sec have loop through each item in list<listviewitem>
items , update each item's time. after phone call listview.notifydatasetchanged()
.
android multithreading multiprocessing
No comments:
Post a Comment