2

I'm trying to create put a ListView with 2 TextViews in each row on an Activity and populate it. The thing about the data ListView to be populated with is that it's not a "table" data, meaning the amount of the records can change frequently, but it's almost static, that is all the amount of rows are definite and known at compile time. So it's pretty much like this sort of data

https://i.stack.imgur.com/APn7W.jpg

What I'll need to update is only the 2nd TextView in each row when the certain menu item is pressed in a toolbar.

I can't figure out how to do that because pretty much all the examples out there assume the data to populate the ListView with is dynamic which isn't the case for me.

Do I still need to create a custom adapter inherited from BaseAdapter? And "one row xml layout" and put it into layouts?

Or should I create the ListView and all the TextViews statically in the layouts like this and then set up their values from the Activity?

<ListView .....
  <TextView ..... />
  <TextView ..... />
  <TextView ..... />

</ListView>
Syscall
  • 16,959
  • 9
  • 22
  • 41
Alan Coromano
  • 22,006
  • 44
  • 122
  • 184
  • you need a adapter to fill de listview in the adapter you set the two textview and yo fill de listview whit static array of string of resources or arraystrings in java see this link http://stackoverflow.com/questions/5070830/populating-a-listview-using-arraylist – brayan camilo villate leon Oct 26 '15 at 18:47

3 Answers3

3

I made a simple example based on what I understood from your description. Here is the full code :

MainActivity.java

package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import static com.example.myapplication.R.array.staticText1Elements;

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);

        setContentView(R.layout.activity_main);
        ListView myListView = (ListView) findViewById(R.id.listViewId);

        List<String> dynamicText2Elements;
        dynamicText2Elements = new ArrayList<String>();
        dynamicText2Elements.add("element1 of text2");
        dynamicText2Elements.add("element2 of text2");
        dynamicText2Elements.add("element3 of text2");
        dynamicText2Elements.add("element4 of text2");
        dynamicText2Elements.add("element5 of text2");

        List<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> element;

        int length = getResources().getStringArray(staticText1Elements).length;

        for (int i = 0; i < length; i++) {
            element = new HashMap<String, String>();
            element.put("text1", getResources().getStringArray(staticText1Elements)[i]);
            element.put("text2", dynamicText2Elements.get(i));
            dataList.add(element);
        }
        ListAdapter myAdapter = new SimpleAdapter(this, dataList, android.R.layout.simple_expandable_list_item_2, new String[]{"text1", "text2"}, new int[]{android.R.id.text1, android.R.id.text2});
        myListView.setAdapter(myAdapter);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

</RelativeLayout>

strings.xml

<resources>
    <string name="app_name">My Application</string>
    <array name="staticText1Elements">
        <item>GPS version</item>
        <item>Wi-FI version</item>
        <item>Bluetooth version</item>
        <item>Touch panel version</item>
        <item>Battery version</item>
    </array>
</resources>
Hichem Acher
  • 433
  • 2
  • 15
2

You need an adapter to fill the list view, inside the adapter you set the two textviews and fill the list view with a static array of strings of resources or arraystrings in java see this link Populating a ListView using an ArrayList?

aleixrr
  • 441
  • 3
  • 18
  • That's what I asked: what adapter, do I have to create my own class inherited from baseadapter or what? – Alan Coromano Oct 26 '15 at 19:34
  • And still in the link it describes 1 dimensional array list, whereas I have key-value list or dictionary. How can I adopt that example for my case? – Alan Coromano Oct 26 '15 at 19:38
1

this is the adapter

 public class AdapterAlumnos extends ArrayAdapter<Object> {

    private LayoutInflater inflate;
    private List<Object> listSolcitudes;
    Context context;

    public AdapterAlumnos(Context context, List<Object> listSolcitudes) {
        super(context, R.layout.item_estudiante, listSolcitudes);
        inflate = LayoutInflater.from(context);
        this.listSolcitudes = listSolcitudes;
        this.context = context;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = inflate.inflate(R.layout.item_estudiante, parent, false);

        TextView txtNombreEstudianteItem = (TextView) convertView.findViewById(R.id.txtNombreEstudianteItem);
        TextView txtGradoEstudianteItem = (TextView) convertView.findViewById(R.id.txtGradoEstudianteItem);

        HashMap<String, Object> obj = (HashMap<String, Object>) getItem(position);
        String genero = (String) obj.get("YourKEY");
        txtNombreEstudianteItem.setText(nombre);

in the activity

ArrayList arrayArray = new ArrayList<Object>(); // your list key-value
        adapter = new AdapterAlumnos(getApplicationContext(),arrayArray);
        ListView listEstudiantes =(ListView)findViewById(R.id.listEstudiantes);
        listEstudiantes.setAdapter(adapter);