Closed myylely closed 5 years ago
lägga till kod i manifest för att få koppling till servern
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Age">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProductActivity" />
</application>
</manifest>
kod hur produkterna visas
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.util.Log;
import android.widget.TextView;
import com.example.myapplication.domain.Product.Product;
class ProductActivity extends AppCompatActivity {
private static final String LOG_TAG = ProductActivity.class.getSimpleName();
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_activity);
// extract the Product pass in the bundle
Bundle extras = getIntent().getExtras();
Product p = (Product) extras.get("product");
// display the product
displayProduct(p);
}
private void setViewText(int viewId, String label, String text) {
TextView tv = findViewById(viewId);
tv.setText(Html.fromHtml("<b>"+label+"</b>: " + text));
Log.d(LOG_TAG, " * " + label + " | " + text);
}
@RequiresApi(api = Build.VERSION_CODES.N)
private void displayProduct(Product product) {
setViewText(R.id.product_name, "Name", product.name());
setViewText(R.id.product_volume, "Volume", String.valueOf(product.volume()));
setViewText(R.id.product_alcohol, "Alcohol", String.valueOf(product.alcohol()));
setViewText(R.id.product_price, "Price", String.valueOf(product.price()));
}
}
product activity xml
<?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">
<TextView
android:id="@+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/product_volume"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/product_price"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/product_alcohol"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Main activity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/product_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></ListView>
</android.support.constraint.ConstraintLayout>