HelloWorld
HelloWorld

Reputation: 17

How to fetch the data from gridview to a listview with onclicklistener

I've got a this project to simply fetch the data of the gridview to the listview but im kinda new to eclipse so I'm so confused to how to do it. Here is the line of codes i have at the moment

package com.smartcartv2;

import java.util.ArrayList;
import java.util.List;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayCondiments extends Activity {
    private List<Condiments> myCondiments= new ArrayList<Condiments>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_condiments);
        // Show the Up button in the action bar.
        setupActionBar();
        populateCondimentList();
        populateGridview();
        callBack();
    }

    private void populateCondimentList() {
        //This codes adds the items to the listview according each lines
        //myCondiments.add(new Condiments(R.drawable.ic_launcher, "Android", "100"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));
        myCondiments.add(new Condiments(R.drawable.download, "Mang Tomas", "50"));


    }
    private void populateGridview() {
        ArrayAdapter<Condiments> adapter =new MyListAdapter();
        GridView list = (GridView) findViewById(R.id.CondimentsGridview);
        list.setAdapter(adapter);

    }

    private void callBack(){
        GridView list=(GridView) findViewById(R.id.CondimentsGridview);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

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

                Condiments clickedProduct =myCondiments.get(position);
                String message ="You clicked " + clickedProduct.getProduct()
                                +"which costs " + clickedProduct.getPrice();
                Toast.makeText(DisplayCondiments.this, message, Toast.LENGTH_LONG).show();

            }

        });
    }

    private class MyListAdapter extends ArrayAdapter<Condiments>{
        public MyListAdapter(){
            super(DisplayCondiments.this, R.layout.item_layout, myCondiments);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View itemView=convertView;
            if (itemView==null){
                itemView=getLayoutInflater().inflate(R.layout.item_layout, parent, false);
            }
            //This Line of codes populates the gridview
            Condiments currentCondiment=myCondiments.get(position);
            ImageView imageView =(ImageView)itemView.findViewById(R.id.icon_id);
            imageView.setImageResource(currentCondiment.getIconID());

            TextView ProductText=(TextView)itemView.findViewById(R.id.txtName);
            ProductText.setText(currentCondiment.getProduct());

            TextView PriceText=(TextView)itemView.findViewById(R.id.txtPrice);
            PriceText.setText(currentCondiment.getPrice());

            return itemView;
        }


    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_condiments, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

and here is my xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DisplayCondiments" >

    <GridView
        android:id="@+id/CondimentsGridview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="164dp"
        android:numColumns="3"
        tools:listitem="@android:layout/simple_list_item_2" >

    </GridView>

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/CondimentsGridview"
        android:layout_alignTop="@+id/CondimentsGridview"
        android:layout_marginRight="829dp" >

    </ListView>

</RelativeLayout>

I really need help out here :( sorry for being noob at this

Upvotes: 0

Views: 1023

Answers (1)

VirtualProdigy
VirtualProdigy

Reputation: 1687

From reading you comments, I think I know what you're trying to do. You best bet is in your gridview adapter, when you populate each view add a tag. view.addTag(); Here you can add an object as a tag. That object shout contain all the data you want in your list view. Make sure you object implements Parcelable. This helps when passing objects around. You can think of it like serializing(kind of).

When you implement your onclick for the gridview, simply get the tag, convert it back to oject if you want or pass it as is,(parcelable) and then populate the data in your list using another custom adapter designed for the listview

list.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, int position,
                long id) {
         YourOBJ obj = (YourObj)viewClicked.getTag();
        //pass it to the listview addapter
        }

    });
}

View tag http://developer.android.com/reference/android/nfc/Tag.html

android Parcelable http://developer.android.com/reference/android/os/Parcelable.html

Upvotes: 1

Related Questions