android


get data of listview item on click


hey guys i am new to xamarin android , i created a listview which takes value of json format i want to get the name or the id of the listview clicked item and i don't know how..
here is my class
class Doctors
{
public int id;
public string Name;
public string address;
public string spec;
public string rating;
public string fee;
public Doctors(string Name, string address, string spec, string rating, string fee)
{
this.Name = Name;
this.address = address;
this.spec = spec;
this.rating = rating;
this.fee = fee;
}
}
and here is my adapter
class DoctorsAdapter : BaseAdapter<Doctors>
{
private Context mContext;
private int mRowLayout;
private List<Doctors> mFriends;
public DoctorsAdapter(Context context, int rowLayout, List<Doctors> friends)
{
mContext = context;
mRowLayout = rowLayout;
mFriends = friends;
}
public override int Count
{
get { return mFriends.Count; }
}
public override Doctors this[int position]
{
get { return mFriends[position]; }
}
public override long GetItemId(int position)
{
return position;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
row = LayoutInflater.From(mContext).Inflate(mRowLayout, parent, false);
}
TextView name = row.FindViewById<TextView>(Resource.Id.docname);
name.Text = mFriends[position].Name;
TextView spec = row.FindViewById<TextView>(Resource.Id.docspec);
spec.Text = mFriends[position].spec;
TextView address = row.FindViewById<TextView>(Resource.Id.docadd);
address.Text = mFriends[position].address;
TextView feeetv = row.FindViewById<TextView>(Resource.Id.feetv);
feeetv.Text = mFriends[position].fee;
RatingBar doctorrating = row.FindViewById<RatingBar>(Resource.Id.docrating);
doctorrating.Rating = Int32.Parse(mFriends[position].rating);
return row;
}
}
and this the method which populates listview with json
private void populatelistview()
{
if (jsonString == null)
{
Toast.MakeText(this, "json is null", ToastLength.Long);
}
else
{
try
{
// jsonobject = JObject.Parse(jsonString);
jsonArray = JArray.Parse(jsonString);
mDoctors = new List<Doctors>();
int count = 0;
while (count < jsonArray.Count)
{
Doctors doctor = new Doctors(jsonArray[count]["Name"].ToString(), jsonArray[count]["spec"].ToString(), jsonArray[count]["address"].ToString(), jsonArray[count]["rating"].ToString(), jsonArray[count]["fees"].ToString());
// Toast.MakeText(this, jsonArray[count]["name"].ToString(), ToastLength.Long).Show();
mDoctors.Add(doctor);
count++;
}
mListView = FindViewById<ListView>(Resource.Id.docview);
mAdapter = new DoctorsAdapter(this, Resource.Layout.ticket_result, mDoctors);
mListView.Adapter = mAdapter;
mListView.ItemClick += MListView_ItemClick;
}
catch (System.Exception exception)
{
Toast.MakeText(this, exception.ToString(), ToastLength.Long);
}
}
i want to get the name or the id of the listview clicked item
You can implement your MListView_ItemClick like this:
private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var position = e.Position;
var item = mDoctors[position] as Doctors;
var name = item.Name;
var id = item.id;
}
First get the position where the item is clicked, then find the matched item in your mDoctors, and finally you can get the Name and id you defined in this item.

Related Links

Huge unusable font size in Android Studio
How to play sound when headphone connected in android programatically?
Android add icon in toolbar inside a fragment
Android design library FAB no shadow
Soft Keyboard doesn't appear when tapping on EditText
No implementation found for native / UnsatisfiedLinkError makes me crazy
What portion of 3rd party android library is included when generating apk?
Fix messed up system bar tint when using android design library appbar/toolbar
how to extract audio from video
Android Project imported in android studio giving exception
How do I store a date value from DatePicker into a formatted String value?
Android RecyclerView with PullRefreshLayout
Can Two views in same size and at same place act on screen operation at the same time?
How to use a pdf document in an android app?
All icons of ActionMode Bar are not showing in Android?
Android Bending View canvas to give curl effect

Categories

HOME
shell
grok
fabricjs
facebook-oauth
c++11
telegram
github-for-windows
android-studio-2.2
pda
components
can
yandex
special-characters
g++
dkim
activecollab
rtf
zerobrane
flash-player
alpacajs
symfony-2.8
job-scheduling
delphi-xe5
replication
visual-glitch
undertow
named-entity-recognition
aggregation
moses
pic32
motion-detection
flooding
mat
google-earth
biztalk-rule-engine
hivemq
reason
connection-pool
hackintosh
xv6
plaintext
watchface
jide
openresty
openal
term
wcftestclient
yii2-api
insight
openblas
podscms
video-conferencing
tinkerpop
excel-dna
jqchart
quicktime
janus
gpg-signature
shinobi
chromium-os
reporting-services-2012
neon
drawstring
i386
installshield-2010
android-dateutils
gogs
vorpal.js
embedding
aurora
cda
hadoop-partitioning
phpwebsocket
fuzzywuzzy
user-management
ascii-art
asyncdisplaykit
directdraw
opl
sciruby
fiware-monitoring
zen-cart
omnifaces
entity-framework-4.3
adehabitathr
packet-sniffers
sonarqube-5.0
cocos3d
appserver
cbind
human-computer-interface
cancellation
jquery-slider
decoupling
xmltodict
gwt-openlayers
hibernate-entitymanager
sphinxql
ie8-compatibility-mode
jeromq
cocoalibspotify-2.0
tlf
neos-server
node-blade
boost-signals
mapping-by-code
infobright
uipagecontrol
twitterizer
svk
ie-compatibility-mode
object-database
opml
scrollbars
echo3

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile