我有一个这个项目只是简单地将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>
我真的需要这里的帮助:(对不起这是
的菜鸟答案 0 :(得分:1)
从阅读你的评论,我想我知道你要做什么。您最好的选择是在您的gridview适配器中,当您填充每个视图添加标签时。 view.addTag(); 您可以在此处添加对象作为标记。该对象喊出包含列表视图中所需的所有数据。确保对象实现了Parcelable。这有助于传递物体。您可以将其视为序列化(种类)。
当您为gridview实现onclick时,只需获取标记,如果需要将其转换回oject或按原样传递(parcelable),然后使用为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
}
});
}
查看代码 http://developer.android.com/reference/android/nfc/Tag.html
android Parcelable http://developer.android.com/reference/android/os/Parcelable.html