2017-03-02 64 views
0

我试图从在线数据库无法在一个ListView显示的另一列/ TextView的

id username 
1 aaaaa 
2 bbbbb 
3 ccccc 

我得到了上述结果显示的用户列表,现在我想补充另一列/的TextView除了那些用户名,也许是名字,还是姓,

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); 
}        

我有一个名为Config.java的独立Java类,它包含一些URL和JSON标签,目前我无法真正理解它。
下面是它的某些部分

//JSON Tags 
public static final String TAG_JSON_ARRAY="result"; 

public static final String TAG_ID = "id"; 
public static final String TAG_UNAME = "username"; 
public static final String TAG_PWORD = "password"; 
public static final String TAG_FNAME = "firstname"; 
public static final String TAG_LNAME = "lastname"; 
public static final String TAG_BDAY = "birthday"; 
public static final String TAG_GENDER = "gender"; 

下面是我的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文件

<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> 

也许是IMPOSIBLE添加另一列或TextView的;这很好,但困扰我的是我无法更改用户名显示。
例如,我想显示姓氏除了身份证号码

回答

1

LinearLayoutprovides an android:weightSum attribute。您可以将其设置为4以获得均匀分布。在这种情况下,每个TextView可以给予android:layout_weightandroid:width="0dp"以填充列的权重。

或者您可以使用GridLayout并将4个单元格放在一行中。

或者您可以使用TableRow


要点是,你有选择。但是,您不应该将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[] 

所以,你添加更多的字符串成是从layoutto数组中设置成XML的id的from阵列。

+0

我看,weightsum是,但如何在Java代码中,三江源 –

+0

@PaulBayona更新与适配器的变化。您可能需要做一些JSON更改。 –

+0

我已经尝试过了,但它的奇怪,listview是空白的,它只能读取Config.TAG_UNAME,再次thnx –

0

尝试像这样

 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); 
     } 
+0

anil,我也试过了,你可以在我的代码中看到它作为评论,它不真的工作,如果我包括除用户名以外的其他标签,我可以显示3列或莫尔,但它只包含id,uname,uname,uname .....奇怪,谢谢 –

+0

它只包含id,uname,uname ,uname .....他们的价值正是你写的。 – anil

+0

我已经做到了,相信我,杰森标签的名字和姓氏,我想我现在不得不放弃,这个应用程序是在我们班上的日期,非常感谢您的耐心。 :) –