dyned呆呆 2013-10-11 17:34 采纳率: 33.3%
浏览 2810
已采纳

跪求:listview如何只占布局中央位置,怎么将json数组传到list中?

紧急求问各位高手:
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();
        }

    }
  • 写回答

1条回答 默认 最新

  • JaveZh 2013-10-12 03:13
    关注

    第一: 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥15 MATLAB数据处理插值
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的
  • ¥15 硬件IIC从模式的数据发送,中断数据的接收,不能用HAL库(按照时序图)