我試圖從在線數據庫無法在一個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的;這很好,但困擾我的是我無法更改用戶名顯示。
例如,我想顯示姓氏除了身份證號碼
我看,weightsum是,但如何在Java代碼中,三江源 –
@PaulBayona更新與適配器的變化。您可能需要做一些JSON更改。 –
我已經嘗試過了,但它的奇怪,listview是空白的,它只能讀取Config.TAG_UNAME,再次thnx –