Jon bnb nb
Jon bnb nb

Reputation: 11

TextView and Button in a row of listview xamarin c#

I have a listview in which I have a textview, an image and a button in a row in Xamarin using c#. by clicking on button I need to open a new layout:

this is axml for listview: called "Friends"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">   
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="378.2dp"
        android:id="@+id/listView1" />
</LinearLayout>

this is axml for each row in listview: called "Row1"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="81.1dp"
        android:id="@+id/linearLayout1">
        <ImageView
            android:src="@drawable/Icon"
            android:layout_width="91.7dp"
            android:layout_height="match_parent"
            android:id="@+id/imageView5" />
        <TextView
            android:text="Text"
            android:layout_width="184.9dp"
            android:layout_height="35.6dp"
            android:id="@+id/textView1"
            android:gravity="center"
            android:textSize="20dp" />
        <Button
            android:text="Chat"
            android:layout_width="62.3dp"
            android:layout_height="37.8dp"
            android:id="@+id/chat1"
            android:layout_marginTop="32dp" />
    </LinearLayout>
</LinearLayout>

and this is the activity for listview: called "FriendsActivity"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;   
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace EJIK_Network
{
    [Activity (Label = "FriendsActivity")]          
    public class FriendsActivity : Activity
    {
        ListView listview;
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.Friends);

            listview = FindViewById<ListView> (Resource.Id.listView1);

            View rootInAnotherLayout = this.LayoutInflater.Inflate (Resource.Layout.Row1,null);
            Button chat =  rootInAnotherLayout.FindViewById<Button> (Resource.Id.chat1);
            chat.Click += (sender, e) => {           
                var CA = new Intent(this, typeof(ChatingActivity));
                StartActivity (CA); 
            };
        }
    }
}

The cod run but when I clicked on chat button it didn't open the new layout wich I defined in "ChatingActivity"??????

Upvotes: 1

Views: 2098

Answers (1)

Stam
Stam

Reputation: 2490

You have to create a Custom Adapter and inside the GetView method:

public override View GetView(int position, View convertView, ViewGroup parent) {

   var view = (convertView ??
                        this._context.LayoutInflater.Inflate(
                            Resource.Layout.Row1,
                            parent,
                            false)) as LinearLayout;
 Button chat =  view.FindViewById<Button> (Resource.Id.chat1);
    chat.Click += (sender, e) => {           
                var CA = new Intent(this, typeof(ChatingActivity));
                StartActivity (CA); 
            };
}

So clicking on the chat button of each list item, the ChatingActivity will start.

Upvotes: 2

Related Questions