Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid ListView with Simple Adapter
    text
    copied!<p>I created <code>ListView</code> activity to list some data retrieved from server. </p> <p>Here is that <strong>ListAtmActivity</strong>.</p> <pre><code>public class ListAtmActivity extends ListActivity{ private static String url ="http://10.0.2.2:8080/hello/AvailabilityResponse"; private static final String ATM_NO = "atmbrno"; private static final String ATM_PLACE = "atmbrname"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_item); String brName=getIntent().getExtras().getString("key"); // Hashmap for ListView ArrayList&lt;HashMap&lt;String, String&gt;&gt; contactList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); // Creating JSON Parser instance JSONParser jParser = new JSONParser(); // getting JSON string from URL JSONArray contacts = jParser.getJSONFromUrl(url,brName); try{ for(int i = 0; i &lt; contacts.length(); i++){ JSONObject json_data = contacts.getJSONObject(i); // Storing each json item in variable String atm_id = json_data.getString(ATM_NO); String atm_name = json_data.getString(ATM_PLACE); HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put(ATM_NO, atm_id); map.put(ATM_PLACE, atm_name); contactList.add(map); } } catch(JSONException e) { e.printStackTrace(); } /** * Updating parsed JSON data into ListView * */ ListAdapter adapter = new SimpleAdapter(this, contactList, R.layout.list_main, new String[] { ATM_NO, ATM_PLACE }, new int[] { R.id.name , R.id.email }); setListAdapter(adapter); //setContentView(R.layout.list_main); }} </code></pre> <p>My contact array is like this.</p> <pre><code>{"atmbrname":"ANURADAPURA [ATM 2]","atmbrno":"ATM084"}, {"atmbrname":"MANNAR BRANCH ","atmbrno":"ATM344"} </code></pre> <p>Here my two xml file also.</p> <p><strong>list_item.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>list_main.xml</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"&gt; &lt;!-- Name Label --&gt; &lt;TextView android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#43bd00" android:textSize="16sp" android:textStyle="bold" android:paddingTop="6dip" android:paddingBottom="2dip" /&gt; &lt;!-- Description label --&gt; &lt;TextView android:id="@+id/email" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#acacac" android:paddingBottom="2dip"&gt; &lt;/TextView&gt; &lt;/LinearLayout&gt; </code></pre> <p></p> <p>But I can't get list view. No errors came. I think problem with my xml files. can anyone help me to solve this problem..</p>
 

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