感謝您閱讀本文!請幫幫我。從Intent Activity獲取數據並在ListView中激活按鈕
我想創建一個餐廳應用程序,在菜單中顯示3個按鈕,然後單擊顯示食物列表的食物按鈕,然後客戶可以選擇他們想要訂購的食物,然後將訂單發送回第一個頁。我讀了很多文章,有人說使用listview.setItemsCanFocus(true),但我有問題要理解它來實現它。有人說getView裏面的按鈕監聽器,但是當我實現我的程序時只是掛起。請幫幫我。謝謝。
我的菜單:
這裏是我的菜(第二)頁面
我有問題:
將數據從食物傳送回.class返回到我的主類是 restaurant.class
在列表中添加按鈕後,食物不可點擊(整排食物)。
我的主類(餐廳)
public class SesameRestaurant extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void foodMenu(View v){
startActivity(new Intent(SesameRestaurant.this,Food.class));
//setContentView(R.layout.foods);
}
public void drinkMenu(View v){startActivity(new Intent(SesameRestaurant.this,Drink.class));}
public void billMenu(View v){}
}
我的第二類(food.class)
package com.restaurant.sesame;
public class Food extends ListActivity {
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
static final String[] title = new String[] {
"Cow Rib steak",
"Thai Prawn Fried Rice",
"Christmas Sensation Delight",
"Salmon Steak" };
static final String[] detail = new String[] {
"1h 37m Shipping: $10.00",
"1h 39m Shipping: Free",
"58m 6s Shipping: $10.00",
"59m 30s Shipping: $10.95" };
private Integer [] imgid = { R.drawable.food1, R.drawable.food2, R.drawable.food3, R.drawable.food4 };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foods);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){
try {
rd = new RowData(i,title[i],detail[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.list, R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position, long id) {
Log.w("my app", "Click list Item!!!");
Toast.makeText(getApplicationContext(), "You have selected "+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
public void orderClick(View v){
}
private class RowData {
protected int mId;
protected String mTitle;
protected String mDetail;
RowData(int id,String title,String detail)
{
mId=id;
mTitle = title;
mDetail=detail;
}
@Override
public String toString() {
return mId+" "+mTitle+" "+mDetail;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
TextView detail = null;
ImageView i11=null;
RowData rowData= getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.list, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
detail = holder.getdetail();
detail.setText(rowData.mDetail);
i11=holder.getImage();
i11.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private TextView detail = null;
private ImageView i11=null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public TextView getdetail() {
if(null == detail){
detail = (TextView) mRow.findViewById(R.id.detail);
}
return detail;
}
public ImageView getImage() {
if(null == i11){
i11 = (ImageView) mRow.findViewById(R.id.img);
}
return i11;
}
}
}
public void backClick(View v){
finish();
}
}
我的食物菜單界面的食物.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_height="wrap_content"
android:text="Back"
android:onClick="backClick"
android:layout_width="fill_parent">
</Button>
我list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView android:id="@+id/img"
android:scaleType="centerCrop"
android:layout_width="100dp"
android:layout_height="100dp"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:id="@+id/title"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="16sp" />
<TextView
android:layout_width="fill_parent"
android:id="@+id/detail"
android:textColor="#ffffff"
android:layout_height="wrap_content"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Quantity :"
android:textColor="#ffffff" />
<EditText
android:id="@+id/quantityInput"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_toRightOf="@id/quantity"
android:hint="1-10"
android:paddingLeft="10dp"
android:textSize="12dp" />
<Button
android:id="@+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_toRightOf="@id/quantity"
android:onClick="orderClick"
android:text="Order"
android:textSize="12dp" />
</RelativeLayout>
</LinearLayout>
我將不勝感激代碼片段。謝謝。我實際上使用startActivity作爲測試,並將其替換爲startActivityForResult()。謝謝! –
任何人?請幫助我...>< –
嘿,我沒有在stackoverflow了一下,完全忘了這篇文章對不起:/。如果你仍然需要這個,試試這個鏈接http://stackoverflow.com/questions/2694947/passing-data-from-new-activity-to-old-activity –