Create listview with button using Xamarin and SQL Server - c#

I have created a project in which I am retrieving data from the database and display it into a ListView. See image below.
Here is the code for retrieving data for the ListView
public class MainActivity : Activity
{
public static Context context;
public static List<UserInfo> UserInfoList = new List<UserInfo>();
public static ListView ListView;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
ListView = FindViewById<ListView>(Resource.Id.Listview);
GetList list = new GetList();
list.Execute();
}
public class GetList : AsyncTask
{
Context con;
protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] #params)
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient( );
var _WebApiUrl = string.Format("URL");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage messge = client.GetAsync(_WebApiUrl).Result;
var Return_EventList = messge.Content.ReadAsStringAsync().Result;
var EventList = JsonConvert.DeserializeObject<List<UserInfo>>(Return_EventList);
foreach (var data in EventList)
{
UserInfoList.Add(data);
}
return true;
}
protected override void OnPreExecute()
{
base.OnPreExecute();
}
protected override void OnPostExecute(Java.Lang.Object result)
{
base.OnPostExecute(result);
ListView.Adapter = new UserInfoListAdapter(context, UserInfoList);
}
}
class UserInfoListAdapter : BaseAdapter<UserInfo>
{
private List<UserInfo> mItem = new List<UserInfo>();
private Context context;
public UserInfoListAdapter(Context mcontext, List<UserInfo> mItems)
{
mItem.Clear();
mItem = mItems;
context = mcontext;
this.NotifyDataSetChanged();
}
public override UserInfo this[int position]
{
get
{
return mItem[position];
}
}
public override int Count
{
get
{
return mItem.Count;
}
}
public Context MContext { get; private set; }
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View listitem = convertView;
listitem = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.ListViewDesign, parent, false);
TextView TxtName = listitem.FindViewById<TextView>(Resource.Id.TxtName);
TextView TxtNumber = listitem.FindViewById<TextView>(Resource.Id.TxtNumber);
TxtName.Text = mItem[position].firstname;
TxtNumber.Text = mItem[position].contact_no;
listitem.Click += (object sender, EventArgs e) =>
{
Toast.MakeText(parent.Context, "Clicked " + mItem[position].firstname, ToastLength.Long).Show();
};
return listitem;
}
}
}
I want to put buttons in the ListView, one per row. Here is the example of how I want to implement it
Now when user click on button, a pdf file should download based on current id from database. For example in my database xray id is 1, so when user download pdf for xray it should download based on id which is 1

Place TableLayout in your 'ListViewDesign' android layout in order to
get required view (or it can be achieved by Linear Layout by setting
its orientation horizontal and its layout_weight)
Place Button in Fourth Column
Remove Click Event from GetView Method
Access Button in GetView Method
Button DownloadButton = view.FindViewbyId<Button>(Resource.Id.btn_download);
DownloadButton.Click += delegate { DownloadFile( link + mItem[position].Id ) };
Add Namespaces
using System.Net;
using System.IO;
using System.Text;
Add a new method in your listView Adapter Class
private void DownloadFile(string url)
{
var webClient = new WebClient();
webClient.DownloadStringCompleted += (s, e) => {
var text = e.Result; // get the downloaded text
string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
string localFilename = "downloaded.txt";
string localPath = Path.Combine(documentsPath, localFilename);
File.WriteAllText(localPath, text); // writes to local storage
};
var url = new Uri("http://xamarin.com");
webClient.Encoding = Encoding.UTF8;
webClient.DownloadStringAsync(url);
RunOnUiThread(() => {
Toast.MakeText(this, "Download Completed", ToastLength.Short).Show();
});
}

Related

How to Use ArrayAdapter with Multiple Textview columns in listView Xamarin Android

I have listview with 4 columns like ItemLookupCode,Quantity,Description,Price
I am adding full code i am sorting list on button click.
i have issue in following Populate() to call 4 textview
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.PriceCheckList);
List<string> list = new List<string>();
foreach (crItem item in listObject)
{
list.Add(item.ItemLookupCode);
list.Add(Convert.ToString(item.Quantity));
list.Add(item.Description);
list.Add(Convert.ToString(item.Price));
}
spacecrafts = list.ToArray();
this.InitializeViews();
this.SortData(ascending);
this.ascending = !ascending;
}
SortBtnItemLookupCode.Click += sortBtn_Click;
void sortBtn_Click(object sender, EventArgs e)
{
SortData(ascending);
this.ascending = !ascending;
}
private void InitializeViews()
{
listView = FindViewById<ListView>(Resource.Id.listView);
SortBtnItemLookupCode = FindViewById<Button>(Resource.Id.SortBtnItemLookupCode);
}
private void Populate()
{
ArrayAdapter adapter = new ArrayAdapter<string>(this, Resource.Layout.PriceCheckListTemplate, Resource.Id.lblItemLookupCode, spacecrafts);
listView.SetAdapter(adapter);
}
private void SortData(bool asc)
{
if (asc)
{
Array.Sort(spacecrafts);
}
else
{
Array.Reverse(spacecrafts);
}
Populate();
}
}
I want to assign all above 4 object to Textview but currently ArrayAdapter accept only one Textview like i have use below Resource.Id.lblItemLookupCode but How i can assign 4 object to 4 columns Textview in ArrayAdapter
Help much appreciated
Thanks in advance
Try create model class to define data for the listView's item, then customize a custom Adapter to populate the data.
Check the code:
public class MainActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
string[] items = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };
List<CustomModel> list = new List<CustomModel>();
//add data
CustomAdapter adapter = new CustomAdapter(list);
ListView listview_ = FindViewById<ListView>(Resource.Id.listview_);
listview_.SetAdapter(adapter);
}
}
public class CustomModel
{
public string Value1 { get; set; }
...
}
public class CustomAdapter : BaseAdapter<CustomModel>
{
List<CustomModel> list = new List<CustomModel>();
public _Adapter(List<CustomModel> list)
{
this.list = list;
}
public override CustomModel this[int position]
{
get
{
return list[position];
}
}
public override int Count
{
get
{
return list.Count;
}
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var view = convertView;
view = LayoutInflater.From(parent.Context).Inflate(Resource.Layout.layout1, parent, false);
//specify value to the each textView
var textview = view.FindViewById<TextView>(Resource.Id.text_1);
textview.Text = list[position].Value1;
...
return view;
}
}

Refresh Fragment From Activity After Updating Data on SQLite Xamarin Android

I got an issue with my Android program when updating user data on Fragment_profile. Fragment_Profile loads User profile data and contains an update button in which the button navigates to an activity called EditProfileActivity. Users can technically update the data but, after they save the updated data, the data still can't be updated. The old data still appear. I try to refresh the fragment by using OnResume() and add OnRestart on my EditProfileActivity.cs and OnPause both in fragment and activity but, still nothing. I am using Xamarin Android and develop it with C#. For more info, you can see what happened in the GIF
My issue
So far, I've tried to code, and here's my code.
Fragment_profile.cs
public class Fragment_Profile : Android.Support.V4.App.Fragment
{
public HomePageActivity m_currentActivity;
public TextView m_tv_loginname, m_tv_username, m_tv_fullname , m_tv_dob;
public Boolean isRefreshing = false;
public override void OnCreate(Bundle aSavedInstanceState)
{
base.OnCreate(aSavedInstanceState);
}
//public static Fragment_Profile NewInstance(Model.User aCurrentUser)
public static Fragment_Profile NewInstance()
{
var _frag4 = new Fragment_Profile { Arguments = new Bundle() };
return _frag4;
}
public override View OnCreateView(LayoutInflater aInflater, ViewGroup aContainer, Bundle aSavedInstanceState)
{
var _ignored = base.OnCreateView(aInflater, aContainer, aSavedInstanceState);
var view= aInflater.Inflate(Resource.Layout.FragmentProfile, null);
m_tv_loginname = view.FindViewById<TextView>(Resource.Id.tv_loginname);
m_tv_username = view.FindViewById<TextView>(Resource.Id.tv_userEmail);
m_tv_fullname = view.FindViewById<TextView>(Resource.Id.tv_fullname);
m_tv_dob = view.FindViewById<TextView>(Resource.Id.tv_dob);
Button _updateProfile = view.FindViewById<Button>(Resource.Id.btnUpdateProfile);
_updateProfile.Click += _updateProfile_Click;
m_currentActivity = (HomePageActivity)this.Activity;
if (m_currentActivity.CurrentUser != null)
{
//string = "Welcome, " + m_currentActivity.CurrentUser.UserName;
m_tv_loginname.Text = m_currentActivity.CurrentUser.LoginName;
m_tv_fullname.Text = m_currentActivity.CurrentUser.UserName;
m_tv_username.Text = m_currentActivity.CurrentUser.UserEmail;
m_tv_dob.Text = m_currentActivity.CurrentUser.DateOfBirth;
}
else
{
Toast.MakeText(Activity, "The data is not found!", ToastLength.Short).Show();
Intent i = new Intent(Context, typeof(MainActivity));
StartActivity(i);
this.Activity.Finish();
}
return view;
}
private void _updateProfile_Click(object sender, EventArgs e)
{
Intent i = new Intent(Context, typeof(EditProfileActivity));
i.PutExtra("loginname", m_currentActivity.CurrentUser.LoginName);
i.PutExtra("fullname", m_currentActivity.CurrentUser.UserName);
i.PutExtra("useremail", m_currentActivity.CurrentUser.UserEmail);
i.PutExtra("dob", m_currentActivity.CurrentUser.DateOfBirth);
StartActivity(i);
}
public override void OnResume()
{
base.OnResume();
if (isRefreshing)
{
Fragment fragment = new Fragment_Profile();
Android.Support.V4.App.FragmentManager fragmentMg = Activity.SupportFragmentManager;
FragmentTransaction fragmentTrans = fragmentMg.BeginTransaction();
fragmentTrans.Replace(Resource.Id.content_frame, fragment);
fragmentTrans.Detach(fragment);
fragmentTrans.Attach(fragment);
fragmentTrans.Commit();
//adapter.notifyDataSetChanged();
}
}
public override void OnPause()
{
base.OnPause();
isRefreshing = true;
}
}
When user click the _updateProfile button It will reference to next activity which is EditProfileActivity. Here's my EditProfileActivity.cs
public class EditProfileActivity : Activity, IOnDateSetListener
{
public EditText m_editFullName, m_editUsername, m_dob;
public TextView m_tvEmail;
public Button m_btnUpdate;
public Boolean isRefreshing = false;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.EditProfile);
m_editFullName = FindViewById<EditText>(Resource.Id.et_editfullName);
m_editUsername = FindViewById<EditText>(Resource.Id.et_editUserName);
m_tvEmail = FindViewById<TextView>(Resource.Id.tv_Email);
m_dob = FindViewById<EditText>(Resource.Id.et_editDob);
m_btnUpdate = FindViewById<Button>(Resource.Id.btn_updateprofile);
m_btnUpdate.Click += _btnUpdate_Click;
m_dob.Click += _dob_Click;
Bundle extras = Intent.Extras;
m_editFullName.Text = extras.GetString("loginname");
m_editUsername.Text = extras.GetString("fullname");
m_tvEmail.Text = extras.GetString("useremail");
m_dob.Text = extras.GetString("dob");
}
private void _btnUpdate_Click(object sender, EventArgs e)
{
try
{
Model.User _currentUser = Model.User.CheckEmailUser(m_tvEmail.Text);
_currentUser.UserName = m_editUsername.Text;
_currentUser.LoginName = m_editFullName.Text;
_currentUser.UserEmail = m_tvEmail.Text;
_currentUser.DateOfBirth = m_dob.Text;
var _updated = DBManager.Instance.Update(_currentUser);
if (_updated > 0)
{
Toast.MakeText(this, "Your account has been succesfully updated!", ToastLength.Long).Show();
Finish();
OnResume();
}
else
{
Toast.MakeText(this, "Failed to update!", ToastLength.Long).Show();
}
}
catch(Exception ex)
{
Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
}
}
private void _dob_Click(object sender, EventArgs e)
{
var _dateTimeNow = DateTime.Now;
DatePickerDialog _datepicker = new DatePickerDialog(this, this
, _dateTimeNow.Year, _dateTimeNow.Month, _dateTimeNow.Day);
_datepicker.Show();
}
public void OnDateSet(DatePicker view, int year, int month, int dayOfMonth)
{
m_dob.Text = new DateTime(year, month + 1, dayOfMonth).ToShortDateString();
}
protected override void OnRestart()
{
base.OnRestart();
if (isRefreshing)
{
isRefreshing = false;
Finish();
}
}
protected override void OnPause()
{
base.OnPause();
if (!isRefreshing)
isRefreshing = true;
}
}
I also have read some articles that have the same problems as me but, It still confusing me and still same. I know it's a simple thing but, It took a few days for me because I am a newby and I still don't get the solution.
Do you guys have any ideas? Would you like to help me? If you don't mind please check my source code so, I know what I am missing. Thank in advance for your help!
In you Fragment, you can override the OnResume method. If you back to the fragment from Acitivty. you should query the new data from DB, then set the new value to the controls in Fragment.
public override void OnResume()
{
base.OnResume();
PHName.Text = photoDAO.GetFirstPhotos(Id).PhotoName;
}
Here is running GIF.
You can see this lifecycle about Fragment. Every time you display the Fragment,OnResume method will be executed. You get the newest data from DB, then set it to the Fragment page.
Here is my demo.
https://drive.google.com/file/d/11dROKS7TtqAaVYkG8w6ZKpqnQJRBD87E/view

How to validate a spinner in Xamarin Android

I'm trying to validate a spinner in C# in xamarin in visual studio if the user didn't select an option. similar to setError in TextView, I have seen examples in SO but all of them are for java if I'm not wrong.
This is my spinner
Spinner spinnerJobTypes = FindViewById<Spinner>(Resource.Id.spinnerJobTypes);
and this does not work for me
TextView errorText = (TextView)mySpinner.getSelectedView();
I would like to display this, if the user hasn't selected anything
else if (spinnerJobTypes.SelectedItem.ToString() == "-Select-")
{
//Display validation error
}
Any help is appreciated,
Spinner spinnerJobTypes = FindViewById<Spinner>(Resource.Id.spinnerJobTypes);
spinnerJobTypes.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs> (spinner_ItemSelected);
private void spinner_ItemSelected (object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spinner = (Spinner)sender;
string toast = string.Format ("The planet is {0}", spinner.GetItemAtPosition (e.Position));
Toast.MakeText (this, toast, ToastLength.Long).Show ();
}
for reference https://developer.xamarin.com/guides/android/user_interface/spinner/
Edited:
Use this and check
errorText.SetCompoundDrawablesWithIntrinsicBounds(0, 0, Resource.Drawable.errorIcon, 0);
My Suggestion is, add an "None selected" view to the Spinner and use a bool parameter to identify whether user has selected certain item or not:
SpinnerAdapter.cs
public class SpinnerAdapter : BaseAdapter<String>
{
Context context;
List<String> list;
public SpinnerAdapter(Context c, List<String> list)
{
context = c;
this.list = list;
}
public override string this[int position] => list[position-1];
public override int Count => this.list.Count+1;
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view;
if (position == 0)
{
view = LayoutInflater.From(context).Inflate(Resource.Layout.spinner_error,null);
var txtView = view.FindViewById<TextView>(Resource.Id.tvErr);
view.FindViewById<TextView>(Resource.Id.tvErr).Text = "None Selected";
//uncomment the following line if you want to show the error icon inside of spinner
//view.FindViewById<TextView>(Resource.Id.tvErr).Error = "";
}
else
{
view = convertView;
if (view == null|| view.FindViewById<TextView>(Resource.Id.tvItem)==null)
{
view = LayoutInflater.From(context).Inflate(Resource.Layout.spinner_item, null);
}
view.FindViewById<TextView>(Resource.Id.tvItem).Text = list[position-1];
}
return view;
}
}
MainActivity:
public class MainActivity : Activity
{
Spinner mSpinner;
bool validatePass=false;
TextView tvResult;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
mSpinner = FindViewById<Spinner>(Resource.Id.mSpinner);
var list = InitList();
SpinnerAdapter adapter = new SpinnerAdapter(this, list);
tvResult = FindViewById<TextView>(Resource.Id.tvResult);
mSpinner.Adapter = adapter;
mSpinner.ItemSelected += MSpinner_ItemSelected;
}
private void MSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
//use validatePass to indicate if user has selected something.
if (e.Position != 0)
{
validatePass = true;
}
else
{
validatePass = false;
}
tvResult.Text ="Validate Result: "+ validatePass.ToString();
}
private List<string> InitList()
{
List<string> list = new List<string>();
for (int i = 0; i < 15; i++)
{
list.Add("Item: " + i);
}
return list;
}
}
But if you really want to show the error icon inside the spinner, you can set the TextView.Error inside of GetView.
Here is my complete Demo:HelloSpinner.

How to add data from database table to ListView C# Xamarin Android App

Few days ago I asked how to share data between activities and one user told me to use SQLite, so I did. I want to let user click on button in the MainLayout which will redirect him to AddTaskLayout where he can add task name, and by pressing Save button app will redirect him back to MainLayout where his task will be listed in ListView.
So far, I created database, table and everything that I need. My question is: How to add data stored in database table to ListView? Every answer I've found was written in Java so searching old StackOverflow questions wasn't so helpful :/
Here's the code:
My DBRepository is class that represents creating database, creating table, inserting data to table and getting that same data:
public class DBRepository
{
public void CreateDatabase()
{
string dbPath = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal), "database.db3");
var db = new SQLiteConnection(dbPath);
}
public void CreateTable()
{
string dbPath = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal), "database.db3");
var db = new SQLiteConnection(dbPath);
db.CreateTable<ToDoTasks>();
}
public string InsertRecord(string task)
{
string dbPath = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal), "database.db3");
var db = new SQLiteConnection(dbPath);
ToDoTasks item = new ToDoTasks();
item.Task = task;
db.Insert(item);
return task;
}
public string GetData()
{
string dbPath = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal), "database.db3");
var db = new SQLiteConnection(dbPath);
string output = "";
var table = db.Table<ToDoTasks>();
foreach(var item in table)
{
output += item;
}
return output;
}
}
ToDoTasks class where I create table:
[Table("ToDo")]
public class ToDoTasks
{
[PrimaryKey, AutoIncrement, Column("_Id")]
public int Id { get; set; }
[MaxLength(100)]
public string Task { get; set; }
}
My AddTaskActivity represents second Layout where user enters task name:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.AddTask);
//define buttons
Button save, cancel;
save = FindViewById<Button>(Resource.Id.save);
cancel = FindViewById<Button>(Resource.Id.cancel);
save.Click += save_click;
cancel.Click += cancel_click;
}
private void save_click(object sender, EventArgs e)
{
DBRepository dbr = new DBRepository();
EditText name = FindViewById<EditText>(Resource.Id.taskName);
//enter user's input(task name) to table
var result = dbr.InsertRecord(name.Text);
StartActivity(typeof(MainActivity));
}
private void cancel_click(object sender, EventArgs e)
{
StartActivity(typeof(MainActivity));
}
My MainActivity where I want to populate listView:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set view
SetContentView(Resource.Layout.Main);
//create database if it doesn't exist
DBRepository dbr = new DBRepository();
dbr.CreateDatabase();
//create table (if it doesn't exist)
dbr.CreateTable();
//Define buttons
Button addTask;
ListView list;
addTask = FindViewById<Button>(Resource.Id.addTask);
addTask.Click += addTask_click;
}
private void addTask_click(object sender, EventArgs e)
{
StartActivity(typeof(AddTaskActivity));
}
I really appreciate your help. I know these are pretty basic questions, but someone have to ask them for himself and many others (future) C# android developers. Thanks!
//////////////////////
UPDATE: I checked Johan's answer as correct, but here is (in my case) correct code:
I needed to change GetData() method to return List (not object as before) and then show that List in ListView. Here's the code:
You helped me a lot, but I had to make few changes, so here they are for the record:
In DBRepository needed to change GetData() method to this:
public List<string> GetData()
{
string dbPath = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Personal), "database.db3");
var db = new SQLiteConnection(dbPath);
List<string> data = new List<string>();
foreach (var item in db.Table<ToDoTasks>())
{
var zad = item.Task.ToString();
data.Add(zad);
}
return data;
}
And then, in MainActivity where is code for ListView only add this:
var items = dbr.GetData();
var listView = FindViewById<ListView>(Resource.Id.listView);
listView.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, items);
I hope it will help someone else in the future. Thanks once again guys.
As per my comments, you would need to make the following changes.
public List<ToDoTasks> GetData()
{
string dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "database.db3");
var db = new SQLiteConnection(dbPath);
return db.Table<ToDoTasks>().ToList();
}
Then in your Activity, where you have a ListView in your you can do the following
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set view
SetContentView(Resource.Layout.Main);
//create database if it doesn't exist
DBRepository dbr = new DBRepository();
dbr.CreateDatabase();
//create table (if it doesn't exist)
dbr.CreateTable();
var items = dbr.GetData();
var listView = FindViewById<ListView>(Android.Resource.Id.ListView);
listView.Adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, items);
}
If your Activity inherits from ListActivity you can just do the following
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set view
SetContentView(Resource.Layout.Main);
//create database if it doesn't exist
DBRepository dbr = new DBRepository();
dbr.CreateDatabase();
//create table (if it doesn't exist)
dbr.CreateTable();
var items = dbr.GetData();
ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, items);
}
I have taken parts of the code from the examples provided by Xamarin, which can be found here: https://developer.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/

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?
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() );
}
});

Categories

Resources