緊急求問各位高手:
listview只能獨占一個xml文件嗎?我想做一個與微博客戶端相似的界面,上面是一個layout放各種按鈕,中間是一個listview(可以滾動),最底端是一個layout(用於提交信息)。這樣的listview應該如何設置呢?
activity又如何使用這個listview,將網絡中獲取到的json數組中的字符串一個一個地傳到listview裡面呢?
求助求助。
以下是我嘗試的activity和xml代碼,不過總是報錯:
//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="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/rl_layout"
android:layout_width="fill_parent"
android:layout_height="45dp"
android:background="#0562C2"
android:gravity="center_vertical" >
<Button
android:id="@+id/room_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="#0562c2"
android:onClick="chat_back"
android:text="返回"
android:textColor="#ffffff"
android:textSize="20sp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="思考"
android:textColor="#ffffff"
android:textSize="40dip" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal" >
<Button
android:id="@+id/roombutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/blue1"
android:gravity="center_horizontal" />
<Button
android:id="@+id/roombutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/blue2"
android:gravity="center_horizontal" />
<Button
android:id="@+id/roombutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/blue3"
android:gravity="center_horizontal" />
<Button
android:id="@+id/roombutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/blue4"
android:gravity="center_horizontal" />
<Button
android:id="@+id/roominfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/whitepoint"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="14dp"
android:clickable="true"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="8dp"
android:text="房間信息"
android:textColor="#000"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@drawable/preference_first_item"
android:clickable="true"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="房間ID"
android:textColor="#000"
android:textSize="17sp" />
<TextView
android:id="@+id/roomid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="8dp"
android:textColor="#000"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/preference_item"
android:clickable="true"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="房間主題"
android:textColor="#000"
android:textSize="17sp" />
<TextView
android:id="@+id/theme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="8dp"
android:textColor="#000"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/preference_last_item"
android:clickable="true"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="主持人"
android:textColor="#000"
android:textSize="17sp" />
<TextView
android:id="@+id/host"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="8dp"
android:textColor="#000"
android:textSize="17sp" />
</RelativeLayout>
<!-- *********************************************************************** -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_marginTop="14dp"
android:clickable="true"
android:gravity="center_vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="房間成員"
android:textColor="#000"
android:textSize="17sp" />
</RelativeLayout>
<!-- *********************************************************************** -->
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/MycreatedteamList"
android:layout_width="match_parent"
android:layout_height="350dp" >
<TextView android:id="@+id/text0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=""
android:textColor="#000000"
/>
</ListView>
</LinearLayout>
</LinearLayout>
//activity:
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import cn.edu.uibe.brainstorming.R;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.app.ListActivity;
public class RoomActivity extends ListActivity {
private CookieStore cookies;
public static String roomid;
public static String theme;
public static String host;
public static JSONArray onlineuser;
public static String[] user = new String[100];
public static String[] strs = new String[100];
public static String PHPSESSID = null;
public EditText new_roomsubject;
public Button roombutton1;
public Button roombutton2;
public Button roombutton3;
public Button roombutton4;
public Button room_back;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 設置布局
setContentView(R.layout.room);
// roombutton();
// getRoomId();
TextView room_id = (TextView) findViewById(R.id.roomid);
room_id.setText(roomid);
TextView room_theme = (TextView) findViewById(R.id.theme);
room_theme.setText(theme);
TextView room_host = (TextView) findViewById(R.id.host);
room_host.setText(host);
try {
for (int i = 0; i < 10; i++) {
user[i] = onlineuser.getJSONObject(i).getString("username");
}
} catch (JSONException e) {
e.printStackTrace();
}
try {
for (int i = 0; i < onlineuser.length(); i++) {
user[i] = onlineuser.getJSONObject(i).getString("username");
}
StringBuffer buffer = new StringBuffer();
ArrayList<HashMap<String, String>> items = new ArrayList<HashMap<String, String>>();
HashMap<String, String> item = null;
int i = 0;
while (i < onlineuser.length()) {
item = new HashMap<String, String>();
item.put(user[i],
onlineuser.getJSONObject(i).getString("username"));
items.add(item);
String[] from = {user[i]};
int[] to = { R.id.text0 };
SimpleAdapter adapter = new SimpleAdapter(this, items,
R.layout.room, from, to);
ListView listView = getListView();
listView.setAdapter(adapter);
i++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
第一: room_id.setText(roomid);這些都沒有賦初始值
第二: while (i < onlineuser.length()) {
item = new HashMap();
item.put(user[i],
onlineuser.getJSONObject(i).getString("username"));
items.add(item);
String[] from = {user[i]};
int[] to = { R.id.text0 };
SimpleAdapter adapter = new SimpleAdapter(this, items,
R.layout.room, from, to);
ListView listView = getListView();
listView.setAdapter(adapter);
i++;
}
這段代碼不是很理解,為何要寫個while,還在裡面重復的設置adapter