0

I am bringing data from rows of a DB and I want to display them on listview. Imagine that I am bringing names and phones, so I want each row of listview to have the telephone and the name.

So far this is my code: (the items are being brought up normally, I see them using system.out. ptintln). So in info[0] i have the name, and in info [1] I have the phone.

this is my adapter code.

    public class FacilitiesAdapter extends ArrayAdapter<String> {
        private final Context context;
        private String data[] = null;

        public FacilitiesAdapter(Context context, String[] data)  {
            super(context, R.layout.expand_row);
            this.context = context;
            this.data = data;

        }
         public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View rowView = inflater.inflate(R.layout.expand_row, parent, false);
                TextView textView = (TextView) rowView.findViewById(R.id.name);
                System.out.println("I am in the adapter "+data[0]);
                textView.setText(data[0]);
TextView textView2 = (TextView) rowView.findViewById(R.id.phone);
textView2.setText(data[1]);
                return rowView;
         }
}

So i suppose that with the above code, i must see in each line the data 0 and the data[1] (phone)? But I am wrong. Why is that?

This is my javacode:

JSONArray jArray = new JSONArray(result);
         for(int i=0;i<jArray.length();i++){
             //each line fetches a line of the table
                JSONObject json_data = jArray.getJSONObject(i);
                if (json_data.getString("Name")!=null)  info[0]=json_data.getString("Name");
                if (json_data.getString("Phone")!=null) info[1]=json_data.getString("Phone");

                FacilitiesAdapter adapter = new FacilitiesAdapter(this,info);
                System.out.println(info[0]);
                setListAdapter(adapter);

1 Answer 1

2

You are doing setListAdapter in a loop, which is weird and I bet that's not what you intended to do. You only need to fill the string array with your data, and initialize your FacilitiesAdapter with the string array a list of string array and do setListAdapter once.

EDIT:

I think you have misunderstood the concept behind an adapter, an adapter is for holding data for the entire AdapterView, which is ListView's parent class, not for holding data for a single item in the AdapterView.

You will need a List of String[] for your adapter, something like the following:

public FacilitiesAdapter ... {
    List<String[]> dataList;
    public FacilitiesAdapter (List<String[]> dataList) {
        this.dataList = dataList;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        String[] data = dataList.get(position);
        // set your data to the views.
    }
}

EDIT 2:

List<String[]> listData = new ArrayList<String[]>();
for(int i = 0; i < jArray.length(); ++i) {
    JSONObject json_data = jArray.getJSONObject(i);
    String name = json_data.getString("Name");
    String phone = json_data.getString("Phone");
    //... some code to check *nullity* of name and phone
    listData.add(new String[]{name, phone});
}    

The code above will fill listData with names and phones(stored in an array) gotten from the JSONObject. And now you can pass this listData as parameter to the constructor of your adapter.

If you still don't get it, you need a book on the Java programming language, mastering the language before you use the language to program Android will help you learn other things better.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Can you please tell me about the arguments in java call? I mean instead of info[] above, I understand that I must pass 2 arrays, one names[] and one phone[], right?
You don't need to pass 2 arrays, please read my answer(including the code snippet).
Ok I read your answer but I do not understand this. How I am going to define my List<String[]> in java and how I am going to "fill" it with data?
Like in the code you post, each string array will hold 2 elements(name and phone number), which represents a list item, and you put those string arrays in an ArrayList, then you will have a list of string arrays, or List<String[]>. P.S. I edited my answer, I changed the string array to a list of string arrays.
thanks for your help. I have understood exaclty what you mean but I am a java beginner and I dont know how to do the assignment in Java? How i will group by the name[0] with the phone[0], then the name[1] with the phone[1] and so on? Can you edit my Java code above?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.