Search code examples
androidarraysstringlistviewfill

How to add String arrays to a ListView


I've made goal_item.xml with 5 textviews.

I have Goal class, almost blank. It has ListView to be filled.

public class Goal extends Activity {

    ListView lvgoal;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("Цели");
        setContentView(R.layout.goal);
        lvgoal= (ListView) findViewById(R.id.lv_goal);
    }

goal.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"
    android:orientation="vertical" >

       <ListView
        android:id="@+id/lv_goal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

And I have 5 String arrays to fill the listview's textviews:

String[] name, sum, member, date, id.

How can I fill the ListView with these 5?


Solution

  • You have to implement your own adapter. Firstly create a new layout with the 5 textviews. (example.xml) Then implement the adapter class.

    If the 5 arrays are related, I suggest you to do the following:

    class Data {
        public String name;
        public String sum;
        public String member;
        public String date;
        public String id;
    }
    

    and save your data in a unique array like that:

    Data[] data = new Data[x];
    data[0] = new Data(); //you have naturally to initialize each element :)
    data[0].name = "abc";
    data[0].sum = "100";
    //...etc...
    

    It will be simpler to create an adapter.

    class MyAdapter extends ArrayAdapter<Data> {
        public MyAdapter(Context context, int textViewResourceId, Data[] objects) {
            super(context, textViewResourceId, objects);
        }
        public MyAdapter(Context context, int textViewResourceId, List<Data> objects) {
            super(context, textViewResourceId, objects);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getContext()
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.example, null);
            TextView t1 = (TextView)convertView.findViewById(R.id.t1);
            TextView t2 = (TextView)convertView.findViewById(R.id.t2);
            TextView t3 = (TextView)convertView.findViewById(R.id.t3);
            TextView t4 = (TextView)convertView.findViewById(R.id.t4);
            TextView t5 = (TextView)convertView.findViewById(R.id.t5);
            Data d = getItem(position);
            t1.setText(d.name);
            t2.setText(d.sum);
            t3.setText(d.member);
            t4.setText(d.date);
            t5.setText(d.id);
    
            return convertView;
        }
    
    }
    

    Then you have to fill the listview:

     Data[] data;
     ListView list = (ListView)findViewById(R.id.lv_goal);
     //...
     MyAdapter adapter = new MyAdapter(context, R.layout.example,data);
     list.setAdapter(adapter);