display data from another activity to another. from class to array list to linearlayout dynamically

The code for each part is displayed below.

This is the class wherein the arraylist gets its data from

package kfc.project;

public class Product {


    String name;
    int servingSize;
    int calories;
    int fat;
    int saturatedFat;
    int transFat;
    int cholesterol;
    int sodium;
    int carbs;
    int protein;


    public Product (String name, int servingSize, int calories, int fat, int saturatedFat, int transFat, int cholesterol, int sodium, int carbs, int protein){
        this.name=name;
        this.calories=calories;
        this.servingSize=servingSize;
        this.fat=fat;
        this.saturatedFat=saturatedFat;
        this.transFat=transFat;
        this.cholesterol=cholesterol;
        this.sodium=sodium;
        this.carbs=carbs;
        this.protein=protein;
    }

    public Product (String name, int servingSize){
        this.name=name;
        this.servingSize=servingSize;
    }

    public String getName() {
        return name;
    }

    public int getServingSize() {
        return servingSize;
    }

    public int getCalories() {
        return calories;
    }

    public int getFat() {
        return fat;
    }

    public int getSaturatedFat() {
        return saturatedFat;
    }

    public int getTransFat() {
        return transFat;
    }

    public int getCholesterol() {
        return cholesterol;
    }

    public int getSodium() {
        return sodium;
    }

    public int getCarbs() {
        return carbs;
    }

    public int getProtein() {
        return protein;
    }


}

This is the array list for the products which gets the parameters above

package kfc.project;

import java.util.ArrayList;

public class ProductList {
    ArrayList<Product> list;


    public ProductList (){
        list = new ArrayList<Product>();

        //CREATE PRODUCT HERE
        Product chicken;
        list.add(new Product("ChickenTest",99));    

        Product rice;
        list.add(new Product("RiceTest",11));
    }

    public String[] getNames (){
        int c = 0;
        int size = list.size() - 1;
        String[] names = new String[size];

        while (size >= c) {
            //names.add(list.get(c).getName());
            names[c] = list.get(c).getName();
            c++;
        }

        c = 0;

        return names;
    }

    public ArrayList<Product> getList (){
        return list;
    }

}

The list view java file to make it visible to the xml file which is already working

package kfc.project;

public class ProductListView extends ListActivity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);

        ProductList pl = new ProductList();

        //setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, PRODUCTS));
        setListAdapter(new ArrayAdapter<Product>(this, R.layout.list_item, pl.getList() ));

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {

          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(), "View Product", Toast.LENGTH_SHORT).show();

            startActivity(new Intent("kfc.project.PRODUCTDETAIL"));
          }

        });

    }



    //TEST STRING ARRAY
    static String[] PRODUCTS = new String[] {
        "Chicken", "Rice", "Coke", "Chicken Burger"
    };



}

The xml file. This is where i want the data from the class displayed dynamically whenever it is clicked from the list view so the data is dynamic

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  android:scrollbarAlwaysDrawVerticalTrack="true"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" android:gravity="top|center"
  android:background="@drawable/bg">

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >


        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:gravity="right">

            <Button
                android:id="@+id/button1"      
                android:background="@drawable/button2"
                android:textColor="#FFFFFF"   
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="ADD TO MENU" android:gravity="right" android:paddingRight="4sp" android:paddingTop="10sp"/>

            <Button
                android:id="@+id/button2"
                android:background="@drawable/button2"
                android:textColor="#FFFFFF"   
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="GET TOTAL" android:gravity="right" android:paddingRight="15sp" android:paddingTop="10sp"/>

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Serving Size: "
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="100"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>
          <ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@android:drawable/divider_horizontal_dark"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="2dp"
    android:paddingTop="2dp" />

    </TableLayout>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:gravity="left">

    </TableRow>

    <TableLayout
        android:id="@+id/tableLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </TableRow>

        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:text="Amount Per Serving: "
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="20"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calories:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="30"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@android:drawable/divider_horizontal_dark"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="2dp"
    android:paddingTop="2dp" />

        <TableRow
            android:id="@+id/tableRow8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/tableRow14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="% Daily Value:          "
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/textView21"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="35"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </TableRow>
        </TableRow>

    </TableLayout>

    <TableLayout
        android:id="@+id/tableLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Total Fat:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView22"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="60"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="       Saturated Fat:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView23"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="34"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView10"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="       Trans Fat:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView24"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="53"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cholesterol:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView25"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="25"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sodium:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView26"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Total Carbohydrate:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView27"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="200"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow16"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="       Dietary Fiber:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView28"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="34"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow17"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="       Sugars:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView29"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="16"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow18"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Protein:"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView30"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="40"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@android:drawable/divider_horizontal_dark"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="fitXY"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="2dp"
    android:paddingTop="2dp" />
<TableRow
    android:id="@+id/tableRow24"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</TableRow>

        <TableRow
            android:id="@+id/tableRow19"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </TableRow>

    </TableLayout>

    <TableLayout
        android:id="@+id/tableLayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </TableRow>

        <TableRow
            android:id="@+id/tableRow21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </TableRow>

        <TableRow
            android:id="@+id/tableRow22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView19"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Vitamin A: "
                android:textAppearance="?android:attr/textAppearanceMedium" />
                        <TextView
                android:id="@+id/textView31"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="40"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView20"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="   Vitamin C: "
                android:textAppearance="?android:attr/textAppearanceMedium" />
                        <TextView
                android:id="@+id/textView32"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="30"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Calcium: "
                android:textAppearance="?android:attr/textAppearanceMedium" />
                        <TextView
                android:id="@+id/textView33"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="35"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView18"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="   Iron: "
                android:textAppearance="?android:attr/textAppearanceMedium" />
                        <TextView
                android:id="@+id/textView34"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="40"
                android:textAppearance="?android:attr/textAppearanceMedium" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow25"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        </TableRow>

    </TableLayout>

</LinearLayout>

Answers


  lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        // When clicked, show a toast with the TextView text
        Toast.makeText(getApplicationContext(), "View Product",
            Toast.LENGTH_SHORT).show();

        Product product = pl.getList().get(position);
        System.out.println("getName() : "+product.getName());


        Intent intent = new Intent(ProductListView.this, ProductDetails.class);
        intent.putExtra("name", product.getName());
              //Add other values alos in Extras....

        ProductListView.this.startActivity(intent);
        }
    });

From the product object obtained, all the values of that product can be accessed. While calling the next Activity, pass these values in Extras and use them in the product details activity.

ProductDetails.class

Set the xml as the content of the ProductDetails activity and get the details passed over Intent in the onCreate() of the activity. These values could be set to the TexViews in the xml.

@Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.product_details);

        Bundle extras = getIntent().getExtras();

        String name = extras.getString("name");

        if (name  != null) {
            TextView text1 = (TextView) findViewById(R.id.textView2);
            text1.setText(name);
        }
    }

Need Your Help

Passing a SqlConnection to class library file (dll)

c# asp.net sql class

I am busy developing a class library project in C# to be reused and attached to different projects in future. It will mainly be used for Table Valued Parameters. My question is, how do I pass a SQL

Move Style From PHP to Head

php html css

I've got a HTML page, the first thing the body does is include the top banner div and the navigation bar div:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.