Android Can';t在ListView上显示另一列/文本视图

Android Can';t在ListView上显示另一列/文本视图,android,Android,我试图显示在线数据库中的用户列表 id username 1 aaaaa 2 bbbbb 3 ccccc 我得到了上面的结果,现在我想在那些用户名之外添加另一个列/文本视图,可能是firstname或lastname id username lastname 1 aaaaa please 2 bbbbb help 3 ccccc me 我相信这部分代码填充了ListView private void showEmp

我试图显示在线数据库中的用户列表

id  username
1   aaaaa
2   bbbbb
3   ccccc
我得到了上面的结果,现在我想在那些用户名之外添加另一个列/文本视图,可能是firstname或lastname

id  username  lastname
1  aaaaa          please
2  bbbbb          help
3  ccccc          me
我相信这部分代码填充了ListView

private void showEmployee(){
    JSONObject jsonObject = null;
    ArrayList<HashMap<String,String>> list = new          ArrayList<HashMap<String, String>>();
    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

        for(int i = 0; i<result.length(); i++){
            JSONObject jo = result.getJSONObject(i);
            String id = jo.getString(Config.TAG_ID);
            String username =     jo.getString(Config.TAG_UNAME);
            //String firstname = jo.getString(Config.TAG_FNAME);



            HashMap<String,String> employees = new HashMap<>();
            employees.put(Config.TAG_ID,id);
            employees.put(Config.TAG_UNAME,username);
          //  employees.put(Config.TAG_UNAME,username);




            list.add(employees);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    ListAdapter adapter = new SimpleAdapter(
            act_viewAllusers.this, list, R.layout.list_item,
            new String[]{Config.TAG_ID,Config.TAG_UNAME},
            new int[]{R.id.id, R.id.username});
  /***  ListAdapter adapter = new SimpleAdapter(
            act_viewAllusers.this, list, R.layout.list_item,
            new String[]{Config.TAG_ID,Config.TAG_UNAME,Config.TAG_FNAME},
            new int[]{R.id.id, R.id.username,R.id.fname}); ***/


    listView.setAdapter(adapter);
}                              
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_act_view_allusers"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.bayona.lesson1.act_viewAllusers">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />

</LinearLayout>
这是我的ListView的XML

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://     schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_marginRight="10dp"
        android:id="@+id/id"
        android:layout_width="40dp"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/fname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/lname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


</LinearLayout>

ListView的另一个XML文件

private void showEmployee(){
    JSONObject jsonObject = null;
    ArrayList<HashMap<String,String>> list = new          ArrayList<HashMap<String, String>>();
    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

        for(int i = 0; i<result.length(); i++){
            JSONObject jo = result.getJSONObject(i);
            String id = jo.getString(Config.TAG_ID);
            String username =     jo.getString(Config.TAG_UNAME);
            //String firstname = jo.getString(Config.TAG_FNAME);



            HashMap<String,String> employees = new HashMap<>();
            employees.put(Config.TAG_ID,id);
            employees.put(Config.TAG_UNAME,username);
          //  employees.put(Config.TAG_UNAME,username);




            list.add(employees);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    ListAdapter adapter = new SimpleAdapter(
            act_viewAllusers.this, list, R.layout.list_item,
            new String[]{Config.TAG_ID,Config.TAG_UNAME},
            new int[]{R.id.id, R.id.username});
  /***  ListAdapter adapter = new SimpleAdapter(
            act_viewAllusers.this, list, R.layout.list_item,
            new String[]{Config.TAG_ID,Config.TAG_UNAME,Config.TAG_FNAME},
            new int[]{R.id.id, R.id.username,R.id.fname}); ***/


    listView.setAdapter(adapter);
}                              
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_act_view_allusers"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.bayona.lesson1.act_viewAllusers">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listView" />

</LinearLayout>

也许添加另一列或文本视图是不可能的;这很好,但困扰我的是我无法更改用户名显示。
例如,我想在id号之外显示lastname

LinearLayout
。您可以将其设置为4,以获得均匀的列分布。每个
TextView
都可以指定一个
android:layout_weight
和一个
android:width=“0dp”
,以填充列的权重

或者您可以使用
网格布局
,将4个单元格放在一行中

或者您可以使用
表格行


关键是,你有选择。但是,您不应该将ListView视为“电子表格”。你可以让每一行变得复杂

就个人而言,也许每行都是这样

ID  Username
Lastname, FirstName
或者甚至不显示id并在用户名前添加
@

Firstname Lastname
@Username
只需使用XML编辑器创建所需的任何布局


就Java代码而言。您只设置了两个值

ListAdapter adapter = new SimpleAdapter(
        act_viewAllusers.this, 
        list,                                         // data
        R.layout.list_item,                           // layout
        new String[]{Config.TAG_ID,Config.TAG_UNAME}, // from[]
        new int[]{R.id.id, R.id.username});           // to[]
因此,您可以将更多字符串添加到
from
数组中,这些字符串将从该
布局设置到
to
数组中的XML id中

像这样试试看

      ListAdapter adapter = new SimpleAdapter(
                act_viewAllusers.this, list, R.layout.list_item,
                new String[]{Config.TAG_ID,Config.TAG_UNAME,
    Config.NextTAg,Config.NextTag2},       
                new int[]{R.id.id, R.id.username,R.id.id1,R.id.id2});
如果未完成,请检查此项

    try {
        jsonObject = new JSONObject(JSON_STRING);
        JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

        for(int i = 0; i<result.length(); i++){
            JSONObject jo = result.getJSONObject(i);
            String id = jo.getString(Config.TAG_ID);
            String username =     jo.getString(Config.TAG_UNAME);
//-----------------
            String varName=jo.getString(Config.REQ_TAG);
//-------------
            //String firstname = jo.getString(Config.TAG_FNAME);



            HashMap<String,String> employees = new HashMap<>();
            employees.put(Config.TAG_ID,id);
            employees.put(Config.TAG_UNAME,username);

//--------------
  employees.put(Config.REQ_TAG,varName);
//-----------
          //  employees.put(Config.TAG_UNAME,username);




            list.add(employees);
        }
试试看{
jsonObject=新的jsonObject(JSON_字符串);
JSONArray result=jsonObject.getJSONArray(Config.TAG\u JSON\u数组);

对于(inti=0;我看,它是weightsum,但是java中的代码呢,thankyou@PaulBayona更新了适配器更改。您可能需要对JSON进行一些更改。我已经尝试过了,但是它的wierd,listview是空的,它只能读取Config.TAG\u UNAME,thnxagain@PaulBayona您需要在HashMap中添加更多变量。
employees.put
…如果您已经这样做并添加到适配器中,那么JSON解析是一个问题,您可以发布一个关于这个问题的专门问题。是的,我已经做了,这就是为什么我无法找出错误,我认为您是对的,它是关于JSON解析的,再次感谢您,我也尝试过,您可以在我的代码中看到它作为注释,它没有如果我包括用户名以外的其他标记,我可以显示3列或更多,但它只包含id、uname、uname、uname…..wierd,谢谢你它只包含id、uname、uname、uname…..它们的值与你写的完全一样。我已经完成了,相信我,jason标记中的firstname和last name,我想我必须放弃,因为没有w、 这个应用程序将于明天在我们班上发布,非常感谢您的耐心。:)您能提供一个您想要的图形表示和输出的屏幕截图吗?对不起,兄弟,我不允许上传图片,不管怎样,我自己会考虑的,谢谢大家,