将产品移至购物车Android

时间:2015-10-26 09:10:10

标签: c# android json xamarin

我正在编写Android应用程序,我需要一个问题的帮助。

目标:

  • 点按具有所有属性的按钮
  • 添加到购物车

我有产品,从API(JSON)解析

代码:

 string url2 = "http://new.murakami.ua/?mkapi=getProducts";
        JsonValue json = await FetchAsync(url2);


        ParseAndDisplay1(json);
        ParseAndDisplay2(json);
        ParseAndDisplay3(json);
        ParseAndDisplay4(json);
        ParseAndDisplay5(json);
        ParseAndDisplay6(json);
        ParseAndDisplay7(json);
        ParseAndDisplay8(json);


 private async Task<JsonValue> FetchAsync(string url)
    {
        // Create an HTTP web request using the URL:
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
        request.ContentType = "application/json";
        request.Method = "GET";

        // Send the request to the server and wait for the response:
        using (WebResponse response = await request.GetResponseAsync())
        {
            // Get a stream representation of the HTTP web response:
            using (Stream stream = response.GetResponseStream())
            {
                // Use this stream to build a JSON document object:
                JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
                //dynamic data = JObject.Parse(jsonDoc[15].ToString);
                Console.Out.WriteLine("Response: {0}", jsonDoc.ToString());

                //string path =  System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);

                //string filename = System.IO.Path.Combine(path, "myfile.txt");
                //using (var streamWriter = new StreamWriter(filename, true))
                //{
                //  streamWriter.Write(jsonDoc.ToString());
                //  streamWriter.Close();


                //}
                // Return the JSON document:
                return jsonDoc;
            }
        }
    }

    private void ParseAndDisplay1(JsonValue json)
    {



        TextView productname = FindViewById<TextView> (Resource.Id.posttittle);
        TextView price = FindViewById<TextView> (Resource.Id.price);
        TextView weight = FindViewById<TextView> (Resource.Id.weight);
        productname.Click += delegate {
            var intent404 = new Intent (this, typeof(SoupesDetailActivity1));

            StartActivity (intent404);
        };

            JsonValue firstitem = json [81];

            productname.Text = firstitem ["post_title"];
            price.Text = firstitem ["price"] + " грн";
            weight.Text = firstitem ["weight"] + "г";




    }

我在设计中有布局显示字段(产品名称,价格,重量)

Axml代码:

<LinearLayout
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/linearLayout12">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="147.6dp"
                android:id="@+id/linearLayout13">
                <ImageView
                    android:src="@drawable/sushi"
                    android:layout_width="123.1dp"
                    android:layout_height="match_parent"
                    android:id="@+id/image1"
                    android:background="#000" />
                <LinearLayout
                    android:orientation="vertical"
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="5dp"
                    android:layout_height="match_parent"
                    android:id="@+id/linearLayout14"
                    android:background="#cf8632" />
                <LinearLayout
                    android:orientation="vertical"
                    android:minWidth="25px"
                    android:minHeight="25px"
                    android:layout_width="184.9dp"
                    android:layout_height="match_parent"
                    android:id="@+id/linearLayout15">
                    <TextView
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:layout_width="181.9dp"
                        android:layout_height="63.3dp"
                        android:id="@+id/posttittle"
                        android:textColor="#ffffffff"
                        android:layout_marginLeft="7.7dp"
                        android:textSize="14sp"
                        android:clickable="true" />
                    <LinearLayout
                        android:orientation="horizontal"
                        android:minWidth="25px"
                        android:minHeight="25px"
                        android:layout_width="match_parent"
                        android:layout_height="93.8dp"
                        android:id="@+id/linearLayout16">
                        <TextView
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:layout_width="wrap_content"
                            android:layout_height="48.7dp"
                            android:id="@+id/price"
                            android:layout_marginTop="39.8dp"
                            android:layout_marginLeft="5.5dp"
                            android:textColor="#ffffffff" />
                        <TextView
                            android:textAppearance="?android:attr/textAppearanceMedium"
                            android:layout_width="58.8dp"
                            android:layout_height="53.3dp"
                            android:id="@+id/weight"
                            android:layout_marginLeft="21.5dp"
                            android:layout_marginTop="41.9dp"
                            android:textColor="#faaf56"
                            android:paddingTop="7dp" />
                    </LinearLayout>
                </LinearLayout>
                <ImageButton
                    android:src="@drawable/cart2"
                    android:layout_width="wrap_content"
                    android:layout_height="57.1dp"
                    android:id="@+id/imageButton6"
                    android:layout_marginTop="34.8dp"
                    android:background="#00000000"
                    android:layout_marginLeft="2.4dp" />

我需要在新活动(CartActivity和移动产品属性)中生成此项。 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以通过意图传递这些属性,如

productname.Click += delegate {
            var intent404 = new Intent (this, typeof(SoupesDetailActivity1));
              JsonValue firstitem = json [81];

            intent404.putStringExtra("title",firstitem ["post_title"]);
            intent404.putStringExtra("price",firstitem ["price"] + " грн");
            intent404.putStringExtra("weight",firstitem ["weight"] + "г");
            StartActivity (intent404);
        };

接收活动

String title=getIntent().getStringExtra("title");