我有一个类viewroom.java,用于列出数据库中所有房间的标题,如下所示:
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 > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// Hashmap for ListView
ArrayList<HashMap<String, String>> roomList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
List<NameValuePair> params = new ArrayList<NameValuePair>();
// 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 < 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<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => 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<?> 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);
}
});
}
}
单击其中一个列表项时,详细信息将显示在单个Room Activity类中。它 如下:
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);
}
}
当单个房间活动中显示所有详细信息时,视图会议室活动类中的每个项目之间仍然存在很大差距。
并将列表适配器修改为
ListAdapter adapter = new SimpleAdapter(this, roomList,
R.layout.list_room,
new String[] { TAG_TITLE }, new int[] {
R.id.title });
在查看室活动中,差距消失,但单人间活动未显示所有描述。
如果您需要我的xml文件: view_room.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<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" />
<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" >
<requestFocus />
</EditText>
<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" />
<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" />
<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" />
<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" />
<ListView
android:id="@android:id/list"
android:layout_below="@+id/room_list_heading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
single room.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<!-- Name Label -->
<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"/>
<!-- Description Label -->
<TextView android:id="@+id/quantity_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/location_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<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"/>
<TextView android:id="@+id/contact_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/description_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<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"/>
<TextView android:id="@+id/address_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
list_room.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Name Label -->
<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" />
<!-- Description label -->
<TextView
android:id="@+id/location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/quantity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/contact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip">
</TextView>
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip">
</TextView>
</LinearLayout>
我想在第一张照片中消除差距,仍然可以得到第二张照片。