0

I am trying to display list of users from an online database

id  username
1   aaaaa
2   bbbbb
3   ccccc

I'm getting the above result, now I want to add another column/TextView besides those username, perhaps firstname, or lastname,

id  username  lastname
1  aaaaa          please
2  bbbbb          help
3  ccccc          me

I believe this part of the code populates the 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);
}                              

I have a separate Java class named Config.java which contains some URLs and JSON tags which currently I can't really understand.
Here's some part of it

//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";

Here's the XML for my ListView

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

Another XML file for the ListView

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

Perhaps it's imposible to add another column or TextView; it's fine, but what bothers me is that I can't change the username displays.
For instance, I want to display lastname besides the id number

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

LinearLayout provides an android:weightSum attribute. You can set this to 4 to get even column distribution. Each TextView can be given a android:layout_weight and a android:width="0dp" in that case to fill the weight of the column.

Or you can use a GridLayout and put 4 cells in one row.

Or you can use a TableRow.


Point being, you have options. However, you shouldn't think of a ListView as a "spreadsheet". You can make each "row" as complex as you want.

Personally, maybe something like this in each row.

ID  Username
Lastname, FirstName

Or don't even show the id and add @ before the username...

Firstname Lastname
@Username

Just use the XML editor to create whatever layout you want.


As far as the Java code is concerned. You only have two values being set.

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

So, you add more strings into the from array which are set into the XML id's in the to array from that layout.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • i see, weightsum is it, but how about the code in java, thankyou – Paul Bayona Mar 02 '17 at 06:59
  • @PaulBayona Updated with adapter changes. There might be some JSON changes to do on your end. – OneCricketeer Mar 02 '17 at 07:03
  • i have tried it, but its wierd , listview is blank, it can only read Config.TAG_UNAME, thnx again – Paul Bayona Mar 02 '17 at 07:26
  • @PaulBayona You need to add more variables into the HashMap. `employees.put`... If you have done that and added to the adapter, then the JSON parsing is an issue, and you can post a question specifically about that. – OneCricketeer Mar 02 '17 at 07:41
  • yws i have done it, thats why i cant figure out whats wrong, i think you are right, its on the json parsing,, thank you again – Paul Bayona Mar 02 '17 at 07:48
0

Try Like This

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

Check this if not done

    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);
        }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
anil
  • 231
  • 1
  • 9
  • anil, i have tried it also, you can see it on my codes as comments, it doesnt really work if i include the other tags except username, i can display 3 columns or mor but it only contain id, uname, uname, uname ..... wierd , thank you – Paul Bayona Mar 02 '17 at 07:33
  • it only contain id, uname, uname, uname ..... values for them of exactly what u had written. – anil Mar 02 '17 at 07:42
  • i have done it, believe me, firstname and last name from jason tags, i guess i have to give up for now, this app is due tommorow on our class, thank you very much for your patience. :) – Paul Bayona Mar 02 '17 at 07:47
  • can you provide a graphical representation of what you want and a screenshot of your output – anil Mar 02 '17 at 08:11
  • im sorry brother but im not allowed to upload picture, anyway ill figure this myself, thank you everyone, – Paul Bayona Mar 02 '17 at 08:22