Search code examples
androidlistviewandroid-edittextandroid-arrayadapter

Android - Get a View (EditText) by ID from an ListView or ArrayAdapter


I have an application in android which lets you manage your school grades. It is just a project for learning so nothing serious.

Now if you open the details from a subject you can see the grades and edit them. If you then click on save I have to check if something changed or something new was added.
Now my problem is that I don't now how I can get a specific view from a ArrayAdapter or from the ListView because every one of those grade fields are generated by the ArrayAdapter and I have to check them all for changes.

So I have a ArrayAdapter which fills a ListView with the following elements (list_grades_prefab.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="wrap_content"
    android:paddingLeft="10pt"
    android:weightSum="2"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/grade_field"
        android:textSize="10pt"
        android:textAlignment="center"
        android:layout_weight="1"
        android:layout_marginRight="10pt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/weight_field"
        android:textSize="10pt"
        android:textAlignment="center"
        android:layout_weight="0.7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_weight="0.3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:textStyle="bold"
        android:text="%"/>

</LinearLayout>

Heres the getView of the ArrayAdapter:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();

        if(convertView == null){
            LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_grades_prefab, null);
            viewHolder.GradeField = (EditText) convertView.findViewById(R.id.grade_field);
            viewHolder.WeightField = (EditText)convertView.findViewById(R.id.weight_field);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.GradeField.setText(String.valueOf(Grades.get(position)[0]));
        viewHolder.GradeField.setTextColor(BasicFunctions.colorFromGrade(Grades.get(position)[0]));
        viewHolder.WeightField.setText(String.valueOf(Grades.get(position)[1]).split("\\.")[0]);

        return convertView;
    }

It just fills the different EditText views with text.

Here I set the ArrayAdapter (GradesAdapter):

ListView gradeListView = (ListView)findViewById(R.id.grade_list_view);
GradesAdapter gradesAdapter = new GradesAdapter(this, R.layout.list_grades_prefab, grades);
gradeListView.setAdapter(gradesAdapter);

grade_list_view is the ListView and grades is an two dimensional array type double.

How do I get now every EditText with the id grade_field from the ArrayAdapter when I am not inside the getView method?


Solution

  • After the user edits the data, call notifyDataSetChanged on your adapter. Since you are using a ListView instead of RecyclerView, that's your only option to inform the views that something changed.