2016-06-21 9 views
11

To jest mój pierwszy kontakt z ListView i miałem problemy. Jestem pewien, że mam nieprawidłowo wdrożoną technikę. Jednak po wielu poszukiwaniach w Internecie i oglądaniu samouczków na widokach listy, jeszcze tego nie odkryłem.Wywoływany twórca ListView, ale nie ma innych metod.

Sporadycznie będzie to wyświetlane przez większość czasu, gdy po prostu się nie uruchamia. Kiedy się wyświetli, jest wtedy, gdy ekran jest wyłączony, a ja uruchomię aplikację i włączam ekran urządzenia, a wyświetli się lista. Jest to jednak bardzo trafione.

Konstruktor jest wywoływany za każdym razem, jednak po tym czasie Count i GetView nigdy nie są wywoływane.

Wszystko wydaje się być wyświetlane w moim pliku main.axml poniżej

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android" 
    p1:orientation="vertical" 
    p1:layout_width="match_parent" 
    p1:layout_height="match_parent" 
    p1:id="@+id/linearLayout1"> 
    <Spinner 
     p1:layout_width="match_parent" 
     p1:layout_height="50.5dp" 
     p1:id="@+id/stores" 
     p1:layout_marginBottom="16.0dp" /> 
    <Button 
     p1:id="@+id/scanItem" 
     p1:layout_width="fill_parent" 
     p1:layout_height="wrap_content" 
     p1:text="Scan Item" /> 
    <ListView 
     p1:minWidth="25px" 
     p1:minHeight="25px" 
     p1:layout_width="match_parent" 
     p1:layout_height="match_parent" 
     p1:id="@+id/itemView" /> 
</LinearLayout> 

W moim głównym działalności Mam prześledzić wszystko dzięki i wszystko nazywa się coraz.

Podam tutaj informacje o tym, w jaki sposób tworzę listę wysyłaną do niestandardowego adaptera, którego używam. Mam niestandardowego obiektu nazwie RootObject który przechowuje listę elementów

var list = JsonConvert.DeserializeObject<RootObject>(response); 
ListView myItems = FindViewById<ListView>(Resource.Id.itemView); 
PIAdapter itemViewAdapter = new PIAdapter(this, list); 
myItems.Adapter = itemViewAdapter; 

To wszystko wydaje się działać

My Adapter konstruktor jest nawet nazywany i mogę potwierdzić, 2 szt są w moim liście.

Jednak, gdy dołączę Console.WriteLine w Count i GetView 99% czasu, nigdy nie są wywoływane. Mimo to mogę wywołać wszystkie pola w konstruktorze i potwierdzić, że mam wypełnione wartości, a pod pewnymi warunkami poprawnie wyświetlają się.

public class PIAdapter : BaseAdapter 
    { 


     RootObject list = new RootObject(); 
     Activity context; 

     public PIAdapter(Activity context, RootObject list) 
     { 
      this.list = list; 
      this.context = context; 
      Console.WriteLine("[My App] Step 10" + list.items.Count); 

     } 
     public override int Count 
     { 
      get 
      { 

       return list.items.Count; 
      } 
     } 

     public override Java.Lang.Object GetItem(int position) 
     { 
      return null; 
     } 

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

     public override View GetView(int position, View convertView, ViewGroup parent) 
     { 
      Console.WriteLine("[My App] - Step 11"); 
      View view = convertView; 
      if(view == null) 
      { 
       view = context.LayoutInflater.Inflate(Resource.Layout.myItem, null); 
      } 

      var item = list.items[position]; 

      ImageView customImage = view.FindViewById<ImageView>(Resource.Id.customImage); 
      TextView customName = view.FindViewById<TextView>(Resource.Id.customName); 
      TextView customBarcode = view.FindViewById<TextView>(Resource.Id.customBarcode); 
      TextView customUp = view.FindViewById<TextView>(Resource.Id.customUpVote); 
      TextView customDown = view.FindViewById<TextView>(Resource.Id.customDownVote); 

      customName.Text = item.name; 
      customBarcode.Text = item.barcode; 
      customUp.Text = item.upvotes; 
      customDown.Text = item.downvotes; 

      //Koush.UrlImageViewHelper.SetUrlDrawable(customImage, "http://api.myurl.com/images/" + item.barcode + ".png", Resource.Drawable.myicon); 

      return view; 
     } 


    } 
} 

W przypadku, gdy jest potrzebna jestem edycji tego dołączyć plik myItem.axml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:minWidth="25px" 
    android:minHeight="25px"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/relativeLayout1"> 
     <ImageView 
      android:src="@android:drawable/ic_menu_gallery" 
      android:layout_width="100dp" 
      android:layout_height="100dp" 
      android:id="@+id/customImage" /> 
     <TextView 
      android:text="Medium Text" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/customImage" 
      android:id="@+id/customBarcode" /> 
     <TextView 
      android:text="Medium Text" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/customBarcode" 
      android:id="@+id/customName" 
      android:layout_toRightOf="@id/customImage" /> 
     <ImageView 
      android:src="@drawable/up" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/customImage" 
      android:id="@+id/customUp" 
      android:layout_below="@id/customName" /> 
     <TextView 
      android:text="0" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/customUp" 
      android:id="@+id/customUpVote" 
      android:layout_below="@id/customName" /> 
     <ImageView 
      android:src="@drawable/down" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/customUpVote" 
      android:id="@+id/customDown" 
      android:layout_below="@id/customName" /> 
     <TextView 
      android:text="0" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="50dp" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@id/customDown" 
      android:id="@+id/customDownVote" 
      android:layout_below="@id/customName" /> 
    </RelativeLayout> 
</LinearLayout> 
+0

jaką wartość uzyskiwanie w 'Console.WriteLine ("[My App] Etap 10" + lista .items.Count); '? –

+0

Odkąd zapomniałem dodać spację po 10, dostaję to. 06-21 00: 56: 32.266 I/mono-stdout (21134): [Moja aplikacja] Krok 102 – StevenDStanton

+0

Jeśli to pomaga, to jest moje źródło danych. Jest to json z 2 przedmiotami {"items": [{"kod kreskowy": "690443240066", "name": "gjjnn", "upvotes": "1", "downvotes": "0", "updated": "1466479409028"}, {"kod kreskowy": "038000845031", "nazwa": "fhj", "upvotes": "2", "downvotes": "1", "zaktualizowany": "1466396732038"}]} – StevenDStanton

Odpowiedz

3

wklejony kod i przetestowany w moim urządzeniu i emulatora. Nie mam problemu, o którym mówisz.

Ale zmieniłem niektóre rzeczy.

To jest mój kod aktywny:

var list = JsonConvert.DeserializeObject<RootObject>("{\"items\":[{\"barcode\": \"690443240066\",\"name\": \"gjjnn\",\"upvotes\": \"1\",\"downvotes\": \"0\",\"updated\": \"1466479409028\"},{\"barcode\": \"038000845031\",\"name\": \"fhj\",\"upvotes\": \"2\",\"downvotes\": \"1\",\"updated\": \"1466396732038\"}]}"); 
ListView myItems = FindViewById<ListView>(Resource.Id.itemView); 
PIAdapter itemViewAdapter = new PIAdapter(this, list); 
myItems.Adapter = itemViewAdapter; 

I zmieniło to w głównej układ:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:p1="http://schemas.android.com/apk/res/android" 
    p1:orientation="vertical" 
    p1:layout_width="match_parent" 
    p1:layout_height="match_parent" 
    p1:id="@+id/linearLayout1"> 
    <Spinner 
     p1:layout_width="match_parent" 
     p1:layout_height="50dp" 
     p1:id="@+id/stores" 
     p1:layout_marginBottom="16dp" /> 
    <Button 
     p1:id="@+id/scanItem" 
     p1:layout_width="fill_parent" 
     p1:layout_height="wrap_content" 
     p1:text="Scan Item" /> 
    <ListView 
     p1:layout_width="match_parent" 
     p1:layout_height="match_parent" 
     p1:id="@+id/itemView" /> 
</LinearLayout> 
+0

Dzięki za odpowiedź. Sprawdzę to w ten weekend. Był to długi tydzień w pracy i jest to projekt poboczny. – StevenDStanton

+0

Czy miałeś okazję go przetestować? – jzeferino

+0

Jeszcze nie został opóźniony w ten weekend z innymi funkcjami aplikacji. Będę na to patrzeć jutro. – StevenDStanton