How to display name,title in ListView, but only Employee No.in SingleListItem?

1.1k views Asked by At

I have successfully produced a ListView, which takes JSON data from a remote MySQL database, and adds the 3 fields (employee no, employee name, and title) to an Output string called "employees". This displays on an Android phone, OK, but what I really want to do is display ONLY employee name and title - and then when this record in the list is clicked on, the I want to display ONLY employee no in the next Activity. Using Split String, I have managed to just show "employee no" in the next Activity, but that still leaves the problem of just showing employee name + title ONLY in the ListView. If I only add those 2 fields together in the Output string, then "employee no" would be "invisible" when single item clicked on.. Many thanks in advance! Here is the relevent code in my MainActivity..

    for (int i = 0; i < jsonMainNode.length(); i++) {
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
    String name = jsonChildNode.optString("employee name");
    String number = jsonChildNode.optString("employee no");
    String title = jsonChildNode.optString("title");
    String outPut = name + "-" + title + "-" + number;
    employeeList.add(createEmployee("employees", outPut)); 
   }

  } catch (JSONException e) {
   Toast.makeText(getApplicationContext(), "Error" + e.toString(),
     Toast.LENGTH_SHORT).show();
  }

  SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
    android.R.layout.simple_list_item_activated_1,
    new String[] { "employees" }, new int[] { android.R.id.text1 });
  listView.setAdapter(simpleAdapter);
 }
1

There are 1 answers

2
Nayra Ahmed On

You have to create an object of Employee which has three fields

private String emplyeeName;
private String employeeTitle;
private String employeeNum;

Then fill an ArrayList with the data returned from the server, and populate it using a Custom Adapter. This way you could manage what should appear in the listview item.

hope it'll help. If there is something not obvious do not hesitate to ask. :)

Edit 1:

Here is an example :

The code in onCreate in MainActivity

final ArrayList<Employee> arr = new ArrayList<Employee>();

    final ListView lv = (ListView) findViewById(R.id.listView);
    lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

            String EmployeeNum = arr.get(position).getEmployeeNum();

            Intent i = new Intent(MainActivity.this, SecondActivity.class);
            i.putExtra("EmployeeNum", EmployeeNum);
            startActivity(i);
        }
    });


    CustomAdapter adapter = new CustomAdapter(this , R.layout.activity_main , arr);
    lv.setAdapter(adapter);

The custom adapter

public class CustomAdapter extends ArrayAdapter<Employee> {

ArrayList<Employee> Info;
private Context context;

CustomAdapter(Context context, int resource, ArrayList<Employee> objects) {
    super(context, resource, objects);
    // TODO Auto-generated constructor stub
    this.context = context;
    Info = objects;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    EmployeeItemHolder employeeHolder;
    View view = convertView;
    if (view == null) {
        employeeHolder = new EmployeeItemHolder();
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        view = inflater.inflate(R.layout.custom_row, parent, false);

        employeeHolder.title = (TextView) view.findViewById(R.id.tvtextView);


        view.setTag(employeeHolder);
    } else {
        employeeHolder = (EmployeeItemHolder) view.getTag();

    }

    Employee eItem = (Employee) this.Info.get(position);
    employeeHolder.title.setText(eItem.getEmployeeTitle()+"-"+eItem.getEmplyeeName());

    return view;
}

private static class EmployeeItemHolder {
    TextView title;
}
}

custom_row.xml

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

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/tvtextView" />
</LinearLayout>

Edit 2:

To integrate with this code convert your employeeList from

ArrayList<String> 

to

ArrayList<Employee>

Here is an example :

ArrayList<Employee> employeeList = new ArrayList<Employee>();
    for (int i = 0; i < jsonMainNode.length(); i++) {
        JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
        Employee emp = new Employee();
        emp.setEmplyeeName(jsonChildNode.optString("employee name"));
        emp.setEmployeeTitle(jsonChildNode.optString("title"));
        emp.setEmployeeNum(jsonChildNode.optString("employee no"));

        employeeList.add(emp); 
    }

} catch (JSONException e) {
    Toast.makeText(getApplicationContext(), "Error" + e.toString(),
            Toast.LENGTH_SHORT).show();
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.activity_main, employeeList );
listView.setAdapter(adapter);

hope that'll help you