0

我有一个这个项目来简单地将gridview的数据获取到listview,但我对eclipse有点陌生,所以我很困惑如何去做。这是我目前拥有的代码行

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);
    }

}

这是我的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>

我真的需要帮助 :( 很抱歉在这方面是菜鸟

4

1 回答 1

1

通过阅读您的评论,我想我知道您要做什么。你最好的办法是在你的 gridview 适配器中,当你填充每个视图时添加一个标签。view.addTag(); 您可以在此处添加对象作为标记。该对象喊包含您想要在列表视图中的所有数据。确保你的对象实现了 Parcelable。这有助于传递对象。你可以把它想象成序列化(某种)。

当您为 gridview 实现 onclick 时,只需获取标记,如果需要,将其转换回 oject 或按原样传递(可打包),然后使用为列表视图设计的另一个自定义适配器填充列表中的数据

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
        }

    });
}

查看标签 http://developer.android.com/reference/android/nfc/Tag.html

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

于 2013-10-15T16:22:09.403 回答