1

I have a custom list view, here is its layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/playerToken"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/playerName"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/playerMoney"/>

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

And here is the custom Adapter which takes an array of a Player Object which stores information like the players name, his token to represent him, and his money balance. the adapter takes that information and populates my custom list, as the layout above.

public class MyAdapter extends ArrayAdapter<Player> {

    public MyAdapter(Context context, Player[] values) {

        super(context, R.layout.activity_banking_layout, values);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater theInflater = LayoutInflater.from(getContext());

        View theView = theInflater.inflate(R.layout.activity_banking_layout, parent, false);

        Player player = getItem(position);

        TextView playerNameText = (TextView) theView.findViewById(R.id.playerName);
        TextView playerMoneyText = (TextView) theView.findViewById(R.id.playerMoney);
        ImageView playerToken = (ImageView) theView.findViewById(R.id.playerToken);

        playerNameText.setText(player.getName());
        playerMoneyText.setText(Integer.toString(player.getMoney()));


        int rId = theView.getResources().getIdentifier(player.getToken(), "drawable",
                                                       getContext().getPackageName());
        playerToken.setImageResource(rId);

        return theView;
    }
}

This is just the layout showing the listView that us being adapted:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</LinearLayout>

So basically, my list is created and adapter in the onCreate method of the activity showing the list. After that my list items can open a context menu and depending on what is selected the player objects are manipulated. I want my list to reflect these changes, so i was wondering how i can access a specific part of my custom list and edit it. For example my custom list has a player icon, and to the right, the players name and below the name a money amount. Lets say i want to change the money amount of a specific player and reflect that change on the list, how do i access that specific TextView, within that specific position in that ListView ?

matiszac
  • 115
  • 2
  • 11

3 Answers3

3

Since your adapter 'MyAdapter' extends ArrayAdapter, you can try using getItem(int position) which return the list item at the specified position; a Player object in your case. From there, you can modify your object's data member (amount of money) and then refresh the list to reflect your changes with notifyDataSetChanged().

In case you wanted to know the index/position of the clicked list item, then your question would be a duplicate of this one.

Community
  • 1
  • 1
yazjisuhail
  • 357
  • 3
  • 6
1

instead of manipulating the textview, consider the following approach:

make the Player[] array a global variable, instead of passing it to constructor,. then manipulate the Player object directly, then call notifyDataSetChanged() on your adapter which will automatically update your correct textview.

private Player[] values;

public void onCreate(Bundle s){
     /*your onCreate things*/

     MyAdapter adapter = new MyAdapter(this);

     // update player 5 from array
     values[4].money = newMoneyVal;

     // after updating, call notifyDataSetChanged()
     adapter.notifyDataSetChanged();
}

public class MyAdapter extends ArrayAdapter<Player> {

    public MyAdapter(Context context) {

        super(context, R.layout.activity_banking_layout, values);

    }

    /*rest of your adapter class*/
}

let me know if this works or if you have any questions

Firoze Rakib
  • 132
  • 1
  • 4
  • This is very dangerous and not the correct way to use an ArrayAdapter. If a filter operation were to occur, external modifications would fail. – Ifrit Jul 28 '14 at 13:39
0

Yuo just have to implement a onlistitemclick listener, like this:

@Override
protected void onListItemClick(ListView l, View v, int pos, long id) {
    super.onListItemClick(l, v, pos, id);
    l[pos].findViewById(R.id.yourTagName).setText("ChangedValue");//to be adapted
}
user2212461
  • 3,105
  • 8
  • 49
  • 87
  • This will break the next time the data set updates, as the adapter has no information about this change. You need to change the underlying data as @yazjisuhail suggested. – Kevin Coppock Jul 27 '14 at 23:40