Android Layout help needed -
i having difficulty making layout work...
what want have 2 spinners , button below , once button clicked, display results based on values selected in spinners.
----------- | spinner 1 | ----------- ----------- | spinner 2 | ----------- ----------- | button | ----------- result 1 result 2 result 3 result 4 result 5
i have tried put results in listview, problem face once button clicked, can see repetition of spinner1, spinner2 , button multiple times.
the xml layout follows:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/country_arrays" android:prompt="@string/country_prompt" /> <spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/country_arrays" android:prompt="@string/country_prompt" /> <button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/done" /> <linearlayout android:id="@+id/morecontent" android:layout_width="match_parent" android:visibility="visible" android:orientation="vertical" android:layout_height="wrap_content" > <listview android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textcolor="#ff0000" android:textsize="12sp" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:contentdescription="@string/hello_world" android:gravity="center_vertical" android:text="@string/hello_world" android:textcolor="#0000ff" android:textsize="12sp" /> </linearlayout>
can please advise going wrong?
the code follows:
package ms.timetable; import java.io.filenotfoundexception; import java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.calendar; import java.util.iterator; import java.util.map; import java.util.treemap; import java.util.map.entry; import android.os.asynctask; import android.os.bundle; import android.annotation.suppresslint; import android.app.activity; import android.app.listactivity; import android.content.context; import android.util.log; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.listadapter; import android.widget.spinner; import android.widget.toast; public class mainactivity extends listactivity { final context context = this; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); addlisteneronbutton(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_main, menu); return true; } public void addlisteneronbutton() { button button = (button) findviewbyid(r.id.button1); button.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { retreivefeedtask rft = new retreivefeedtask(); rft.execute(); } }); } @suppresslint("newapi") private class retreivefeedtask extends asynctask<void, void, string> { treemap<string, string> _timetable = null; @override protected string doinbackground(void... arg0) { // todo auto-generated method stub _timetable = fetchtimetableinfo(); return null; } protected void onpostexecute(string str){ if (_timetable != null){ arraylist<timetable> tarr = new arraylist<timetable>(); iterator<entry<string, string>> = _timetable.entryset().iterator(); string toasttext = ""; while (it.hasnext()) { map.entry<string, string> pairs = (map.entry<string, string>)it.next(); string key = pairs.getkey(); string value = pairs.getvalue(); toasttext = toasttext + key + " --- " + value + "\n"; //system.err.println(key + " --- " + value); timetable tt = new timetable(); tt.set_line(value); string [] tem = key.split("---"); tt.set_arrival(tem[1]); tt.set_departure(tem[1]); tarr.add(tt); } //toast.maketext(context, toasttext, toast.length_long).show(); //tmap.add(_timetable); setlistadapter((listadapter) new timetableadapter(getapplicationcontext(), tarr)); } } private treemap<string, string> fetchtimetableinfo(){ spinner spinner1 = (spinner) findviewbyid(r.id.spinner1); spinner spinner2 = (spinner) findviewbyid(r.id.spinner2); string origin = (string) spinner1.getselecteditem(); string destination = (string) spinner2.getselecteditem(); //string val = string.valueof(spinner1.getselecteditem()); arraylist<string> linesservorigin = linestation.getservinglines(origin); arraylist<string> linesservdest = linestation.getservinglines(destination); linesservorigin.retainall(linesservdest); //arraylist<string> preferredlines = linesservorigin.re.retainall(linesservdest); log.e(this.getclass().tostring(), "selected value : " + linesservorigin.get(0)); try { (int k = 0; k < linesservorigin.size(); k++) { string url = "http://tt.ptv.vic.gov.au/tt/xslt_request?itdlpxx_linemain=" + linestation.getlinemain(linesservorigin.get(0)) + "&itdlpxx_lineid=" + linestation.getlineid(linesservorigin.get(0)) + "&itdlpxx_output=html"; datamanager dm = new datamanager(url, getapplicationcontext()); arraylist<string> stationdata = dm.getstationsdata(); arraylist<arraylist<string>> timetabledata = dm.gettimetabledata(); arraylist<string> origintimetable = null; arraylist<string> origintimetable1 = null; arraylist<string> destinationtimetable = null; arraylist<string> destinationtimetable1 = null; int origpos = 0; int destpos = 0; int ctr = 0; (int = 0; < stationdata.size(); i++) { if (stationdata.get(i).equals(origin) || (origin + " - dep").equals(stationdata.get(i))) { //origintimetable = timetabledata[i]; origpos = i; } else if (stationdata.get(i).equals( destination) || (destination + " - arr").equals(stationdata.get(i))) { //destinationtimetable = timetabledata[i]; destpos = i; } if (origpos != 0 && destpos != 0) { break; } } if (origpos > destpos) { dm.switchdirection(); stationdata = dm.getstationsdata(); timetabledata = dm.gettimetabledata(); } (int = stationdata.size() - 1; >= 0; i--) { if (stationdata.get(i).equals(origin) || (origin + " - dep").equals(stationdata.get(i))) { if (origintimetable == null) { origintimetable = timetabledata.get(i); } else if (origintimetable1 == null) { origintimetable1 = timetabledata.get(i); } } else if (stationdata.get(i).equals(destination) || (destination + " - arr").equals(stationdata.get(i))) { if (destinationtimetable == null) { destinationtimetable = timetabledata.get(i); } else if (destinationtimetable1 == null) { destinationtimetable1 = timetabledata.get(i); } } } treemap<string, string> ttable = new treemap<string, string>(); if (origintimetable != null && destinationtimetable != null) { //int curtime = integer.parseint(datetime.now.tostring("hhmm", cultureinfo.currentculture)); string temp = new simpledateformat("hhmm").format(calendar.getinstance().gettime()); int curtime =integer.parseint(temp); system.err.println("origin timetable size = " + origintimetable.size()); system.err.println("destination timetable size = " + destinationtimetable.size()); (int j = 0; j < origintimetable.size(); j++) { if (integer.parseint((origintimetable.get(j))) > curtime) { if (integer.parseint((destinationtimetable.get(j))) == -1) { if (destinationtimetable1 == null || (integer.parseint(destinationtimetable1.get(j)) == -1)) { continue; } } else if (destinationtimetable1 != null && integer.parseint(destinationtimetable1.get(j)) == -1) { if (destinationtimetable == null || (integer.parseint(destinationtimetable.get(j)) == -1)) { continue; } } ctr++; if (destinationtimetable1 != null) { if (integer.parseint(origintimetable.get(j)) < integer.parseint(destinationtimetable1.get(j))) { ttable.put(origintimetable.get(j) + " --- " + destinationtimetable1.get(j), linesservorigin.get(k)); } } else { if (integer.parseint(origintimetable.get(j)) < integer.parseint(destinationtimetable.get(j))) { ttable.put(origintimetable.get(j) + " --- " + destinationtimetable.get(j), linesservorigin.get(k)); } } if (ctr == 5) { break; } } } ctr = 0; if (origintimetable1 != null) { (int j = 0; j < origintimetable1.size(); j++) { if (origintimetable1 != null && integer.parseint(origintimetable1.get(j)) > curtime) { if (integer.parseint(destinationtimetable.get(j)) == -1) { if (destinationtimetable1 == null || (integer.parseint(destinationtimetable1.get(j)) == -1)) { continue; } } else if (destinationtimetable1 != null && integer.parseint(destinationtimetable1.get(j)) == -1) { if (destinationtimetable == null || (integer.parseint(destinationtimetable.get(j))) == -1) { continue; } } ctr++; if (destinationtimetable1 != null) { if (integer.parseint(origintimetable1.get(j)) < integer.parseint(destinationtimetable1.get(j))) { ttable.put(origintimetable1.get(j) + " --- " + destinationtimetable1.get(j), linesservorigin.get(k)); } } else { if (integer.parseint(origintimetable1.get(j)) < integer.parseint(destinationtimetable.get(j))) { ttable.put(origintimetable1.get(j) + " --- " + destinationtimetable.get(j), linesservorigin.get(k)); } } if (ctr == 5) { break; } } } } iterator<entry<string, string>> = ttable.entryset().iterator(); //foreach (keyvaluepair<string, list<string>> item in linestations) while (it.hasnext()) { map.entry<string, string> pairs = (map.entry<string, string>)it.next(); string key = pairs.getkey(); string value = pairs.getvalue(); system.err.println(key + " --- " + value); } return ttable; } } } catch (parseexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; } } class timetable{ private string _departure, _arrival, _line; public timetable(){ } public timetable(string departure, string arrival, string line){ _departure = departure; _arrival = arrival; _line = line; } public string get_departure() { return _departure; } public void set_departure(string _departure) { this._departure = _departure; } public string get_arrival() { return _arrival; } public void set_arrival(string _arrival) { this._arrival = _arrival; } public string get_line() { return _line; } public void set_line(string _line) { this._line = _line; } } }
the adapter follows:
package ms.timetable; import java.util.arraylist; import ms.timetable.mainactivity.timetable; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.textview; public class timetableadapter extends arrayadapter<timetable>{ private final context context; private final arraylist<timetable> values; public timetableadapter(context context, arraylist<timetable> values){ super(context, r.layout.activity_main, values); this.context = context; this.values = values; } public view getview(int position, view convertview, viewgroup parent) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); view rowview = inflater.inflate(r.layout.activity_main, parent, false); textview textview = (textview) rowview.findviewbyid(r.id.textview1); textview textview2 = (textview) rowview.findviewbyid(r.id.textview2); //system.err.println("position : "+position); timetable tt = values.get(position); textview.settext(tt.get_departure()); textview2.settext(tt.get_arrival()); return rowview; } }
the problem due to
view rowview = inflater.inflate(r.layout.activity_main, parent, false);
in getview()
method.
you inflating whole activity_main in row , adding listview. put row results in separate xml instead of activity_main. have new xml file say, row.xml contain:
<textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textcolor="#ff0000" android:textsize="12sp" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="fill_parent" android:contentdescription="@string/hello_world" android:gravity="center_vertical" android:text="@string/hello_world" android:textcolor="#0000ff" android:textsize="12sp" />
so have inflate as
view rowview = inflater.inflate(r.layout.row, parent, false);
and add generated rows in listview in activity_main.xml. problem solved. hope helps.
Comments
Post a Comment