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

问题描述

跪求: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();
        }

    }

解决方案

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

时间: 2024-08-18 20:38:37

listview布局设置-跪求:listview如何只占布局中央位置,怎么将json数组传到list中?的相关文章

跪求C#下如何实现自定义结构或类与BYTE数组之间的相互转换(即数据的打包与解包)?

问题描述 请教C#下如何实现自定义结构或类与BYTE数组之间的相互转换(即数据的打包与解包)?在C#定义类似如下的结构和类:[Serializable][StructLayout(LayoutKind.Sequential,Pack=1)]publicstructSTCord{publicdoublex;//publicdoubley;publicbytetype;publicintnum;};[Serializable][StructLayout(LayoutKind.Sequential,P

加强51 卡尔曼滤波-[51单片机] 有没有大神做基于加强51的MPU6050啊~~~跪求指导

问题描述 [51单片机] 有没有大神做基于加强51的MPU6050啊~~~跪求指导 本人只会用串口和用1602读出数据... 不会卡尔曼滤波.等对数据的处理和进一步转换 解决方案 http://bbs.elecfans.com/jishu_364692_1_1.html

Beyond Compare文本怎么进行页面布局设置?

  说到Beyond Compare,这是一款很专业好用的文件夹以及文件比较工具.当我们用Beyond Compare软件进行文本比较时,为了方便用户快速查看文本差异部分,符合用户个人查看习惯,接下来就和大家讲解一下,Beyond Compare文本怎么进行页面布局设置. 边并排布局 步骤一:打开Beyond Compare软件,选择文本比较会话,打开会话操作界面,单击"浏览文件"按钮,选择需要比较的文本文件. 步骤二:其中默认的会话操作界面视图的布局方式为"边并排布局&qu

java代码 如何将图片转化成矩阵数组的形式! 跪求~~

问题描述 java代码 如何将图片转化成矩阵数组的形式! 跪求-- java代码 如何将图片转化成矩阵数组的形式! 跪求-- 像素数据和二进制数据都可以! java代码 如何提取函数图像的数据,最大值,最小值,变化趋势等等... 请教各位大神们!! 解决方案 http://stchou.iteye.com/blog/819191 参考这个代码,可以读取每个像素的颜色,之后你想怎么处理就怎么处理

spring mvc-各位大神,跪求解答啊 springMVC+spring+mybatis

问题描述 各位大神,跪求解答啊 springMVC+spring+mybatis springMVC4.2.4+spring4.2.4+mybatis3 整合过程中tomcat启动过程中打印各种no url 求解啊 各位大神 [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping]Rejected bean name 'userController': no URL paths identified [org.spri

android-求指导!BaseAdapter监听的ListView如何设置长点击删除item

问题描述 求指导!BaseAdapter监听的ListView如何设置长点击删除item 希望长点击一个item后会弹出AlertDialog,确定的话就会删除该ListView的item, 监听器是BaseAdapter 解决方案 listview.setOnItemLongClickedListener(),方法里面可以使用removeItem(position):或者弹出Dialog,选择是否删除 解决方案二: http://android.tgbus.com/Android/androi

C# listview 如何设置某个值可编辑

问题描述 LabelEdit设置为True,只能第一列可已编辑,我需要是其他列比如listview1.Items[0].SubItems[1].Text第一行的第二例. 解决方案 解决方案二:这种需求一般用gridview解决方案三:引用1楼shoppo0505的回复: 这种需求一般用gridview listview上就无法实现吗?只想在listview上面实现.解决方案四:this.listView1.LabelEdit=true;然后将想要编辑的列插入可编辑的控件,比如label,比如bu

nginx+PHP利用php-fpm中php_admin_value 设置php变量的问题 【跪求大侠帮助】

问题描述 nginx+PHP利用php-fpm中php_admin_value 设置php变量的问题 [跪求大侠帮助] nginx+PHP利用php-fpm中php_admin_value 设置php变量,修改后,php.ini配置文件中对应的参数值没有变化. php_admin_value[post_max_size] = 4M //修改后,php-fpm中post_max_size = 4M.php.ini配置文件的值仍为原值,没有改变. 小弟菜鸟一枚,请大侠们帮忙看看!

c++-跪求帮忙,win7运行不了C++,在CSDN上下载之后只运行一次再新建工程就运行不了

问题描述 跪求帮忙,win7运行不了C++,在CSDN上下载之后只运行一次再新建工程就运行不了 RT谢了,拜托了····55555 刚在CSDN上下载的C++,说好的能在win7下运行,可是安装后只运行一次,再新建工程调试就错误 Compiling... c.cpp Linking... LINK : fatal error LNK1104: cannot open file "Debug/c.exe" 就显示这个····5555···肿么回事啊··· 解决方案 不知道您的版本是vc+