2017-03-15 190 views
0

當我使用JSON然後數據中顯示許多時間在listview.When i的列表視圖向下滾動然後再次重複數據顯示在列表視圖列表視圖數據顯示

class.cs綁定列表視圖

namespace HSAPP 
{ 
    class ContListViewSpecNameClass : BaseAdapter<GetSpecNames> 
    { 
     List<GetSpecNames> objList; 

     Activity objActivity; 

     public ContListViewSpecNameClass (Activity objMyAct,List<GetSpecNames> objMyList) : base() 
     { 
      this.objActivity = objMyAct; 
      this.objList = objMyList; 
     } 

     public override GetSpecNames this[int position] 
     { 
      get 
      { 
       return objList[position]; 
      } 
     } 

     public override int Count 
     { 
      get 
      { 
       return objList.Count; 
      } 
     } 

     public override long GetItemId(int position) 
     { 
      return position; 
     } 

     public override View GetView(int position, View convertView, ViewGroup parent) 
     { 
      var item = objList[position]; 
      //View objView = convertView; 

      if (convertView == null) 
      { 
       convertView = objActivity.LayoutInflater.Inflate(Resource.Layout.ContListViewSpecName, null); 
       convertView.FindViewById<TextView>(Resource.Id.tvSpecID).Text = item.SpecID; 
       convertView.FindViewById<TextView>(Resource.Id.tvSpecName).Text = item.SpecName; 
      } 
      return convertView; 
     } 
    } 
} 

activity.cs

private void BindControl_BindSpecCompleted(object sender, BindControl.BindSpecCompletedEventArgs e) 
{ 
    jsonValue = e.Result.ToString(); 

    if (jsonValue == null) 
    { 
     Toast.MakeText(this, "No Data For Bind", ToastLength.Long).Show(); 
     return; 
    } 

    try 
    { 
     JArrayValue = JArray.Parse(jsonValue); 
     list = new List<GetSpecNames>(); 
     int count = 0; 
     while (count < JArrayValue.Count) 
     { 
      GetSpecNames getSpecName = new GetSpecNames(JArrayValue[count]["SpecID"].ToString(), JArrayValue[count]["SpecName"].ToString()); 
      list.Add(getSpecName); 
      count++; 
     } 
     listView.Adapter = new ContListViewSpecNameClass(this, list); 
     listView.ItemClick += ListView_ItemClick; 
    } 
    catch (Exception ex) 
    { 
     Toast.MakeText(this, ex.ToString(), ToastLength.Long).Show(); 
    } 
} 

private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) 
{ 
    var curItem = list[e.Position]; 
    Toast.MakeText(this, curItem.SpecID + ' ' + curItem.SpecName, ToastLength.Long).Show(); 
} 

輸出: -

This is 1st image

This is 2nd image when scroll down

+1

請參閱本主題:http://stackoverflow.com/questions/19019775/list-item-repeating-in-android-customized-listview – HeisenBerg

+0

公共ContListViewSpecNameClass(活動objMyAct,列表 objMyList):基座( ) { this.objActivity = objMyAct; this.objList = objMyList; } 這些是參考文獻 –

+0

Y ..... it Worked。謝謝 – sagarpatre

回答

0

我認爲它是因爲您沒有更新convertViewconvertView != null

從技術文檔

convertView - 舊觀點,如果可能的重用。注意:您應該在 使用前檢查此視圖是否爲非空以及適當類型。如果無法將此視圖轉換爲顯示 正確的數據,則此方法可以創建新視圖。