Note that there are some explanatory texts on larger screens.

plurals
  1. POHandeling adapters from list view to single item view class
    primarykey
    data
    text
    <p>I have a class viewroom.java, to list all the rooms' title from database, as follows:</p> <pre><code> package com.iwantnew.www; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import org.apache.http.NameValuePair; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.annotation.TargetApi; import android.app.ListActivity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.os.StrictMode; //import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; @TargetApi(Build.VERSION_CODES.GINGERBREAD) public class viewroom extends ListActivity { // url to make request private static String url = "http://10.0.2.2/iWant/src/android_view_all_room.php"; // JSON Node names private static final String TAG_ROOM = "room"; // private static final String TAG_SUCCESS = "success"; private static final String TAG_ID = "id"; private static final String TAG_LOCATION = "location"; private static final String TAG_TITLE = "title"; private static final String TAG_QUANTITY = "quantity"; private static final String TAG_PRICE = "price"; private static final String TAG_CONTACT = "contact"; private static final String TAG_AREA = "area"; private static final String TAG_DESCRIPTION = "description"; private static final String TAG_ADDRESS = "address"; // contacts JSONArray JSONArray room = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.view_room); if (android.os.Build.VERSION.SDK_INT &gt; 9) { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); } // Hashmap for ListView ArrayList&lt;HashMap&lt;String, String&gt;&gt; roomList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); // Creating JSON Parser instance JSONParser jParser = new JSONParser(); List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;(); // getting JSON string from URL JSONObject json = jParser.makeHttpRequest(url, "GET", params); try { // Getting Array of Contacts room = json.getJSONArray(TAG_ROOM); // looping through All Contacts for(int i = 0; i &lt; room.length(); i++){ JSONObject c = room.getJSONObject(i); // Storing each json item in variable String id = c.getString(TAG_ID); String location = c.getString(TAG_LOCATION); String quantity = c.getString(TAG_QUANTITY); String address = c.getString(TAG_ADDRESS); String price = c.getString(TAG_PRICE); String contact = c.getString(TAG_CONTACT); String area = c.getString(TAG_AREA); String description = c.getString(TAG_DESCRIPTION); String title = c.getString(TAG_TITLE); // creating new HashMap HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); // adding each child node to HashMap key =&gt; value map.put(TAG_ID, id); map.put(TAG_LOCATION, location); map.put(TAG_QUANTITY, quantity); map.put(TAG_ADDRESS, address); map.put(TAG_PRICE, price); map.put(TAG_CONTACT, contact); map.put(TAG_AREA, area); map.put(TAG_DESCRIPTION, description); map.put(TAG_TITLE, title); // adding HashList to ArrayList roomList.add(map); } } catch (JSONException e) { e.printStackTrace(); } /** * Updating parsed JSON data into ListView * */ ListAdapter adapter = new SimpleAdapter(this, roomList, R.layout.list_room, new String[] { TAG_TITLE, TAG_LOCATION, TAG_PRICE }, new int[] { R.id.title, R.id.location, R.id.price }); setListAdapter(adapter); // selecting single ListView item ListView lv = getListView(); // Launching new screen on Selecting Single ListItem lv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { // getting values from selected ListItem String title = ((TextView) view.findViewById(R.id.title)).getText().toString(); String location = ((TextView) view.findViewById(R.id.location)).getText().toString(); String price = ((TextView) view.findViewById(R.id.price)).getText().toString(); // Starting new intent Intent in = new Intent(getApplicationContext(), SingleRoomActivity.class); in.putExtra(TAG_TITLE, title); in.putExtra(TAG_LOCATION, location); in.putExtra(TAG_PRICE, price); startActivity(in); } }); } } </code></pre> <p>when one of those list item is clicked, details are shown in single Room Activity class. it is as follows:</p> <pre><code>package com.iwantnew.www; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SingleRoomActivity extends Activity { private static final String TAG_TITLE = "title"; private static final String TAG_LOCATION = "location"; private static final String TAG_PRICE = "price"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.single_room); // getting intent data Intent in = getIntent(); // Get JSON values from previous intent String title = in.getStringExtra(TAG_TITLE); String location = in.getStringExtra(TAG_LOCATION); String price = in.getStringExtra(TAG_PRICE); // Displaying all values on the screen TextView lblTitle = (TextView) findViewById(R.id.title_label); TextView lblLocation = (TextView) findViewById(R.id.location_label); TextView lblPrice = (TextView) findViewById(R.id.price_label); lblTitle.setText(title); lblLocation.setText(location); lblPrice.setText(price); } } </code></pre> <p>there remains a big gap between each items in view room activity class, when all details are shown in single room activity. </p> <p>and when list adapter is modified to</p> <pre><code> ListAdapter adapter = new SimpleAdapter(this, roomList, R.layout.list_room, new String[] { TAG_TITLE }, new int[] { R.id.title }); </code></pre> <p>in view room activity, the gap vanishes but, single room activity does not show all the description.</p> <p>in case you need my xml files: view_room.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" &gt; &lt;Button android:id="@+id/searchBtn" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/slogan" android:text="@string/Search" /&gt; &lt;EditText android:id="@+id/searchArea" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignTop="@+id/searchBtn" android:ems="10" android:hint="@string/hint" android:inputType="text" &gt; &lt;requestFocus /&gt; &lt;/EditText&gt; &lt;TextView android:id="@+id/slogan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/searchArea" android:layout_below="@+id/brand" android:text="@string/slogan" android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="italic" android:typeface="normal" /&gt; &lt;TextView android:id="@+id/brand" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/iWant" android:textSize="@dimen/iwant" /&gt; &lt;Button android:id="@+id/postBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="26dp" android:text="@string/post" /&gt; &lt;TextView android:id="@+id/room_list_heading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/searchBtn" android:text="@string/room_list_heading" android:textAppearance="?android:attr/textAppearanceMedium" android:textStyle="italic" android:typeface="normal" /&gt; &lt;ListView android:id="@android:id/list" android:layout_below="@+id/room_list_heading" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/RelativeLayout&gt; </code></pre> <p>single room.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"&gt; &lt;!-- Name Label --&gt; &lt;TextView android:id="@+id/title_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25dip" android:textStyle="bold" android:paddingTop="10dip" android:paddingBottom="10dip" android:textColor="#43bd00"/&gt; &lt;!-- Description Label --&gt; &lt;TextView android:id="@+id/quantity_label" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;TextView android:id="@+id/location_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold"/&gt; &lt;TextView android:id="@+id/price_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25dip" android:textStyle="bold" android:paddingTop="10dip" android:paddingBottom="10dip" android:textColor="#43bd00"/&gt; &lt;TextView android:id="@+id/contact_label" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;TextView android:id="@+id/description_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold"/&gt; &lt;TextView android:id="@+id/area_label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="25dip" android:textStyle="bold" android:paddingTop="10dip" android:paddingBottom="10dip" android:textColor="#43bd00"/&gt; &lt;TextView android:id="@+id/address_label" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>list_room.xml</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="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"&gt; &lt;!-- Name Label --&gt; &lt;TextView android:id="@+id/title" 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/location" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip"&gt; &lt;/TextView&gt; &lt;TextView android:id="@+id/quantity" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip"&gt; &lt;/TextView&gt; &lt;TextView android:id="@+id/price" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip"&gt; &lt;/TextView&gt; &lt;TextView android:id="@+id/contact" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip"&gt; &lt;/TextView&gt; &lt;TextView android:id="@+id/area" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip"&gt; &lt;/TextView&gt; &lt;TextView android:id="@+id/description" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip"&gt; &lt;/TextView&gt; &lt;TextView android:id="@+id/address" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingBottom="2dip"&gt; &lt;/TextView&gt; &lt;/LinearLayout&gt; </code></pre> <p><img src="https://i.stack.imgur.com/OUUkw.png" alt="this is view room&#39;s snap shot"></p> <p><img src="https://i.stack.imgur.com/rgTrs.png" alt="This is single room&#39;s snap shot"></p> <p>I want to vanish gap in the first picture and still get the second picture as it is.</p>
    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