Search code examples
c#androidlistviewxamarinlistadapter

From itemclick method in adapter how to change textview in the activity


I have a simple activity which textview and listview. My list view use custom adapter to display data also in the adapter i have itemClick method. I would like to change textview after clicking the list item. My problem is I don't know how to find the text view from list adapter? I'll try something like this:

  TextView TeamSelect = (TextView)activity.FindViewById(Resource.Id.tvSelectPlayers);
           TeamSelect.SetText("Players", TextView.BufferType.Normal);

but this give me an error:

 System.NullReferenceException: Object reference not set to an instance of an object

Here is my adapter:

public HomePlayersAdapter(Activity context)
    { 
        mInflater = LayoutInflater.From(context);
        mSelectedItemsIds = new SparseBooleanArray();
        this.context = context;
public override int Count            //return number of items in the list
    {
        get { return homePlayers.Count; }
    }
    public override Java.Lang.Object GetItem(int position)
    {
        return position;
    }

   public Player GetObject(int position)
    {
        return this.homePlayers.ElementAt(position);
    }
    public override long GetItemId(int position)
    {
        return position;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
     var y = (itemPos.IndexOf(position) + 1);
     var item = homePlayers[position];
        if (convertView == null || holder == null)
        {
            convertView = mInflater.Inflate(Resource.Layout.RowPlayers, null);
            holder = new ViewHolder();
            holder.playerName = convertView.FindViewById<TextView>(Resource.Id.tvRow); //set holder label with label list id in the view
            convertView.Tag = (holder);
     }
        else
        {
            holder = (ViewHolder)convertView.Tag;
        }
        holder.playerName.SetText(item.firstName + " " + item.lastName, TextView.BufferType.Normal);//set data label
        holder.playerName.TextSize = 30;

        if (clickCount >= 15)
        {
            subs = 1;
            Android.Widget.Toast.MakeText(context, "ok", Android.Widget.ToastLength.Short).Show();
        }
        else
        {
            subs = 0;
        }

        y = (itemPos.IndexOf(position) + 1);
        if (itemPos.Contains(position))
        {
            holder.playerName.SetTextColor(mInflater.Context.Resources.GetColor(Resource.Drawable.green));
            holder.playerName.SetText(y + ". " + item.firstName + " " + item.lastName, TextView.BufferType.Normal);//set data label
        }
        else
        {                holder.playerName.SetTextColor(mInflater.Context.Resources.GetColor(Resource.Drawable.white));
        }
        return convertView;
    }
   public void itemClick(int position)
    {

        if (!itemPos.Contains(position))
        {
            clickCount++;
            var selectFixtureActivity = new Intent(context, typeof(SelectPlayers));
            selectFixtureActivity.PutExtra("clickCount", clickCount);

            holder.playerName.SetTextColor(mInflater.Context.Resources.GetColor(Resource.Drawable.green));
            itemPos.Add(position);
            NotifyDataSetChanged();
            insertPlayer(position);
    }
        else
        {
            clickCount--;
            var selectFixtureActivity = new Intent(context, typeof(SelectPlayers));
            selectFixtureActivity.PutExtra("clickCount", clickCount);
            holder.playerName.SetTextColor(mInflater.Context.Resources.GetColor(Resource.Drawable.white));
            deletePlayer(position);
            int po = itemPos.IndexOf(position);
            itemPos.RemoveAt(po);
            NotifyDataSetChanged();
 class ViewHolder : Java.Lang.Object
{
    public TextView playerName;
}

And my activity:

 protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.SelectPlayer);

        homeListAdapter = new HomePlayersAdapter(this);
        listView = FindViewById<ListView>(Resource.Id.lvSelectPlayers);
        TextView TeamSelect = FindViewById<TextView>(Resource.Id.tvSelectPlayer);
        listView.Adapter = homeListAdapter;


        listView.ChoiceMode = ChoiceMode.Multiple;
        this.listView.ChoiceMode = ChoiceMode.Multiple;
        var nameHome = Intent.GetStringExtra("nameHome");
        var clickCount = Intent.GetStringExtra("clickCount");

        TextView homeTeam = (TextView)FindViewById(Resource.Id.tvHomeTeam);
        homeTeam.SetText(nameHome, Button.BufferType.Normal);

        this.listView.ItemClick += (sender, e) =>
        {
           homeListAdapter.itemClick(e.Position);
        }

I tried do that in different way: I send clickCount variable to the activity and there in ItemClick method i tried change the text view, but clickCount var on the begining is send to the acticity but when ItemClick method finish clickCount change value to null.How can i solve this problem?


Solution

  • You should not access the Activity directly from the Adapter. You should set a listener on the adapter (the listener can be the Activity). This is based on the Observer pattern. It decouples the components and promotes re-usability. Then the Activity, when called, can modify its own Text View.

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_layout);
        ListView myListView = (ListView)findViewById(R.id.your_list);
        final TextView myTextView = (TextView)findViewById(R.id.your_text);
        YourAdapter adapter = new YourAdapter();
        adapter.setListener(new CustomListener() { // You write this method in your adapter
            @Override
            public void onItemClicked(Item clickedItem) {
                myListView.setText( clickedItem.toString() );
            }
        });
    }
    

    Or if you aren't doing anything special in the custom adapter, you can always use the built in list view listener.

    myListView.setOnItemClickListener( new OnItemClick() { // This is not onClick, but onItemClick
        @Override
        public void onItemClicked(AdapterView<?> parent, View view, int position, long id) {
            Object clickedItem = parent.getItemAtPosition(position);
            myTextView.setText( clickedItem.toString() );
        }
    });