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>
jaką wartość uzyskiwanie w 'Console.WriteLine ("[My App] Etap 10" + lista .items.Count); '? –
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
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