Note that there are some explanatory texts on larger screens.

plurals
  1. POIterate a custom ListView
    primarykey
    data
    text
    <p>I'm trying to create a custom ListView that displays the event's name, date, and the time it starts.</p> <p>I am able to create the ListView, however only the event's name is displayed. I've tried several experiments with the list adapter and making seperate ArrayLists for each item and that did not work. Can you show me how to get the data in the for loop in the <code>displaylist()</code> method to print out the "Event Date" and "Event Time"? The tutorial I'm following hard codes all the data which is not practical. Please follow this link for images of my app and what I hope to display (located at bottom of page): <a href="http://leobee.com/android/push/so/listview.html" rel="nofollow">see images</a></p> <pre><code>public class DataView extends ListActivity{ FileInputStream inputStream; String s = ""; List&lt;String&gt; eventNames; List&lt;String&gt;eventDate; List&lt;String&gt;eventStart; List&lt;String&gt;eventEnd; List&lt;String&gt;eventLocation; List&lt;String&gt;eventDelete; @Override protected void onCreate(Bundle pop) { super.onCreate(pop); setContentView(R.layout.columns); eventNames = new ArrayList&lt;String&gt;(); eventDate = new ArrayList&lt;String&gt;(); eventStart = new ArrayList&lt;String&gt;(); eventEnd = new ArrayList&lt;String&gt;(); eventLocation = new ArrayList&lt;String&gt;(); eventDelete = new ArrayList&lt;String&gt;(); File myFile = new File(getApplicationContext().getFilesDir().getAbsolutePath()+ "/Test.txt"); DownloadFileAsync FileTask = new DownloadFileAsync(); FileTask.execute(myFile); }// end of onCreate private class DownloadFileAsync extends AsyncTask&lt;File, Void, String&gt; { @Override protected String doInBackground(File... params) { // TODO Auto-generated method stub File myFile = new File(getApplicationContext().getFilesDir().getAbsolutePath()+ "/Schedule.txt"); while(!myFile.exists()){ try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } File myDir = new File(getApplicationContext().getFilesDir().getAbsolutePath()); BufferedReader br = null; try { br = new BufferedReader(new FileReader(myDir + "/Schedule.txt")); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { s = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String result = s; return result; }// end of do in background @Override protected void onPostExecute(String result) { JSONArray jsonArray; try { jsonArray = new JSONArray(result); for (int i = 0; i &lt; jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String eventname =jsonObject.getString("event_name"); String eventdate =jsonObject.getString("event_date"); String eventstart =jsonObject.getString("event_start"); String eventend =jsonObject.getString("event_end"); String eventlocation =jsonObject.getString("event_location"); String eventdelete =jsonObject.getString("event_delete_flag"); Log.v("event web address", eventname+ " "+ eventdate+ " "+eventstart+ " "+eventend+ " "+eventlocation+ " "+eventdelete); eventNames.add(eventname); eventDate.add(eventdate); eventStart.add(eventstart); eventEnd.add(eventend); eventLocation.add(eventlocation); eventDelete.add(eventdelete); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } String[] EventName = new String[ eventNames.size()]; eventNames.toArray(EventName); String[] EventDate = new String[ eventDate.size()]; eventDate.toArray( EventDate ); String[] EventStart = new String[ eventStart.size()]; eventStart.toArray(EventStart); String[] EventEnd = new String[ eventEnd.size()]; eventEnd.toArray( EventEnd); String[] EventLocation = new String[ eventLocation.size()]; eventLocation.toArray(EventLocation); String[] EventDelete = new String[ eventDelete.size()]; eventDelete.toArray( EventDelete); displaylist(EventName,EventDate,EventStart,EventEnd,EventLocation, EventDelete); }// end of onPostExecute }/// end of DownloadFileAsync public void displaylist(String[] EventName, String[] EventDate, String[] EventStart, String[] EventEnd, String[] EventLocation, String[] EventDelete){ ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylistData =new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); int x=(int)EventName.length; for(int i=0; i&lt;x; i++) { HashMap&lt;String,String&gt; map = new HashMap&lt;String, String&gt;(); //initialize row data for(int j=0; j&lt;1; j++) { map.put("Event Name", EventName[i]); map.put("Event Date", EventDate[i]); map.put("Event Time", EventStart[i]); } mylistData.add(map); } SimpleAdapter arrayAdapter = new SimpleAdapter(this, mylistData, R.layout.mylistrow,new String []{"Event Name", "Event Date","Event Time"} , new int[]{R.id.text1,R.id.text2,R.id.text3}); setListAdapter(arrayAdapter); } protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Object o = this.getListAdapter().getItem(position); String event = o.toString(); Toast.makeText(this, "You have chosen event: " + " " + event, Toast.LENGTH_LONG).show(); } } </code></pre> <p>main.xml</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="2" android:background="#000fff" android:drawSelectorOnTop="false" &gt; &lt;/ListView&gt; &lt;TextView android:id="@id/android:empty" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFff00" android:text="No data" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>mylistrow.xml</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingBottom="6dip" android:paddingTop="4dip" &gt; &lt;TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="#FFFF00" android:textSize="16sp" android:textStyle="bold" /&gt; &lt;TextView android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textSize="12sp" android:textStyle="bold" /&gt; &lt;TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="14sp" android:textStyle="italic" android:typeface="sans" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload