Question

I'm new to Android and I'm trying to understand why the following code doesn't work:

...

    matList = (ListView) findViewById(android.R.id.list);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, mats);
    matList.setAdapter(adapter);
    matList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    matList.setOnItemClickListener(this);

    CheckedTextView v =(CheckedTextView) adapter.getView(1, null, null);
                    v.setText("n1");

The text in the particular line never gets changed. I found another way to do it by performing a Click on the item like this(continues from above):

    matList.performItemClick(adapter.getView(i, null, matList), 2, 2);
}

public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3) {

    CheckedTextView mat = (CheckedTextView) v;
    mat.setText("n2");
}

It works... the second line of the ListView becomes "n2" while the first line stays the same... but for some reason it doesn't seem as the right way to do it. I've searched as much I could but I didn't manage to find anything on it. Please help me to understand why the first way doesn't work.

Was it helpful?

Solution

CheckedTextView v =(CheckedTextView) adapter.getView(1, null, null);
                v.setText("n1");

Is not correct for your purposes, you don't call getView, getView is called by the Adapter to return you the view to showed in the ListView; if you like you can override getView if you create a custom adapter by extending ArrayAdapter (for example).

You can have a look at this link

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top