GAMA
GAMA

Reputation: 5996

using Adapter to display ListView Mono for android

I created a ListView and to populate it I'm using Adapter.

GetView() of Adapter class is as follows:

LayoutInflater inflater = activity.LayoutInflater;
if (convertView == null) 
{
    convertView = inflater.Inflate(Resource.Layout.List_MktData, null);
}

DataRow dr;
object[] row;

if(position==0)
{
    Console.WriteLine("In position 0");
    dr = result.Rows[0];
    row=dr.ItemArray;

    tvCMPValue.Text=row[0].ToString();
    tvPrevClose.Text=row[1].ToString();
    tvOpen.Text=row[2].ToString();
    tvHigh.Text=row[3].ToString();
    tvLow.Text=row[4].ToString();
    tvClose.Text=row[5].ToString();
}

else if(position==1)
{
    Console.WriteLine("In position 1");
    dr = result.Rows[1];
    row=dr.ItemArray;

    tvPercChgValue.Text=row[0].ToString();
    tvPrevCloseValue.Text=row[1].ToString();
    tvOpenValue.Text=row[2].ToString();
    tvHighValue.Text=row[3].ToString();
    tvLowValue.Text=row[4].ToString();
    tvCloseValue.Text=row[5].ToString();
}

where all elements starting with tv are TextView within a ListView.

Now the problem is when I run this code, I get the following output as shown in Screen shot.

i.e. Content of Row1 and Row2 gets displayed in Row1' whileRow2` is showing correct data.

I know it is something related with position variable, but after spending lot of time on it, I'm still not able to crack it.

Note : Data is coming from DataTable having two rows.

1st row of ListView should be populated with 1st row of DataTable while 2nd row of ListView should be populated with 2nd row of DataTable.

I'm absolute beginner when it comes to Mono for Android, so any help appreciated...

screen shot

Upvotes: 0

Views: 1700

Answers (1)

Matthew
Matthew

Reputation: 5222

What you have is: 2 rows that have a table in each row, each consisting of two rows. What you want is: 2 rows in the ListView with a LinearLayout with one row of TextViews .

Then, do this for each call to GetView:

LayoutInflater inflater = activity.LayoutInflater;
if (convertView == null) 
{
    convertView = inflater.Inflate(Resource.Layout.List_MktData, null);
}

DataRow dr;
object[] row;

Console.WriteLine("In position " + position);
dr = result.Rows[position];
row = dr.ItemArray;

tvCMPValue.Text = row[0].ToString();
tvPrevClose.Text = row[1].ToString();
tvOpen.Text = row[2].ToString();
tvHigh.Text = row[3].ToString();
tvLow.Text = row[4].ToString();
tvClose.Text = row[5].ToString();

Solves two problems: less code and it should work. Also, as you may notice, there is a gap between the two items in the list. this is a result of the first row (table) in the second row (list item), this is the header table row, not having any values.

Upvotes: 1

Related Questions