我想将我的第一个活动中的所有选定项目发送到选中项目的另一个活动。我的应用程序中有三个类首先是Appetizer类,第二个类是ItemListBaseAdapter类,第三个类是Manage类我想将检查项从Appetizers类发送到Manage class。如果您需要知道任何其他信息,请告诉我。 这是我的开胃菜类
public class Appetizers extends Activity{
Button manage;
public static final String[] titles = new String[] { "Aloo Chat",
"Paneer Tikka", "Spring Roll", "Paneer Chilly", "Veg Roll","Rice balls","Pasta", };
public static final String totalPrice="0";
public static final int[] quantities = new int[]{ 0 };
public static final String[] price={"20","10","30","40","50","60","10"};
public static final Integer[] images = { R.drawable.a39,
R.drawable.a40, R.drawable.a41, R.drawable.a42,R.drawable.a74,R.drawable.a75,R.drawable.a76 };
public ItemListBaseAdapter adapter;
ListView listView;
public List<RowItem> rowItems;
Button add,sub;
public static Appetizers apti;
public RowItem item;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
apti=this;
// LinearLayout layout = (LinearLayout) getWindow().findViewById(R.id.button1);
// layout.addView(new Button(this));
manage=(Button) findViewById(R.id.manage);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < titles.length; i++) {
item = new RowItem(images[i], titles[i], quantities[0],price[i],totalPrice);
rowItems.add(item);
Log.e("item in appetizer", ""+item);
manage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// intt.putParcelable("myclass",rowItems);
// intt.putStringArrayListExtra("key", rowItems);
Intent i=new Intent(Appetizers.this,Manage.class);
startActivity(i);
}
});
}
listView = (ListView) findViewById(R.id.list);
ItemListBaseAdapter adapter = new ItemListBaseAdapter(this,
R.layout.item_details_view, rowItems);
Log.e("Row items in appetizer",""+rowItems);
listView.setAdapter(adapter);
}
}
这是我的ItemListBaseAdapter类
class ItemListBaseAdapter extends ArrayAdapter<RowItem> {
public static ItemListBaseAdapter adapter;
Context context;
int getPosition;
int rowItem;
String check1;
public List<RowItem> rowItems;
/*
* here we must override the constructor for ArrayAdapter the only variable
* we care about now is ArrayList<RowItem> objects, because it is the list
* of objects we want to display.
*/
public ItemListBaseAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
adapter=this;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtTitle,txtPrice,ttprice;
TextView txtDesc;
Button add, sub,manage;
CheckBox check;
}
/*
* we are overriding the getView method here - this is what defines how each
* list item will look.
*/
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// first check to see if the view is null. if so, we have to inflate it.
// to inflate it basically means to render, or show, the view.
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.item_details_view, null);
}
/*
* Recall that the variable position is sent in as an argument to this
* method. The variable simply refers to the position of the current
* object in the list. (The ArrayAdapter iterates through the list we
* sent it)
*
* Therefore, rowItem refers to the current RowItem object.
*/
RowItem rowItem = getItem(position);
if (rowItem != null) {
holder = new ViewHolder();
// This is how you obtain a reference to the TextViews
// Images,checkBox,Buttons.
// These TextViews are created in the XML files we defined.
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.txtPrice = (TextView) convertView.findViewById(R.id.price);
holder.ttprice=(TextView) convertView.findViewById(R.id.tPrice);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
holder.check = (CheckBox) convertView.findViewById(R.id.checkBox1);
holder.add = (Button) convertView.findViewById(R.id.button1);
holder.sub = (Button) convertView.findViewById(R.id.button2);
// holder.manage = (Button) convertView.findViewById(R.id.manage);
// holder.check.setOnClickListener( new View.OnClickListener() {
// public void onClick(View v) {
// CheckBox cb = (CheckBox) v ;
// RowItem rowItem = (RowItem) cb.getTag();
// Toast.makeText(context,"Clicked on Checkbox: " + cb.getText() +" is " + cb.isChecked(), Toast.LENGTH_LONG).show();
// rowItem.setSelected(cb.isChecked());
// }
// });
/*
* holder.check.setOnClickListener( new View.OnClickListener() {
* public void onClick(View v) { CheckBox cb = (CheckBox) v ;
* RowItem rowItem = (RowItem) cb.getTag(); Toast.makeText(context,
* "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(),
* Toast.LENGTH_LONG).show(); rowItem.setSelected(cb.isChecked()); }
* });
*/
} else
{
holder = (ViewHolder) convertView.getTag();
}
/*holder = (ViewHolder) convertView.getTag();
*rowItem = rowItems.get(position); holder.txtTitle.setText(" (" +
*rowItem.getTitle() + ")"); holder.check.setText(rowItem.getName());
*holder.check.setChecked(rowItem.isSelected());
* holder.check.setTag(rowItem);
*/
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.txtPrice.setText(rowItem.getPrice());
Log.e("title",""+rowItem.getTitle());
Log.d("description",""+rowItem.getDesc());
Log.e("Price",""+rowItem.getPrice());
holder.imageView.setImageResource(rowItem.getImageId());
holder.add.setTag(position);
holder.sub.setTag(position);
//holder.manage.setTag(position);
holder.check.setTag(position);
holder.add.setEnabled(false);
holder.sub.setEnabled(false);
final RowItem finalRowItem = rowItem;
final ViewHolder viewHolderFinal = holder;
holder.add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int quantity = finalRowItem.getQuantity();
if (quantity >= 0) {
finalRowItem.setQuantity(quantity + 1); // update it by adding 1
quantity=finalRowItem.getQuantity();
if (quantity>=1) {
String p=finalRowItem.getPrice();
int x=Integer.parseInt(p);
int y=quantity*x;
String f=Integer.toString(y);
finalRowItem.setTotalPrice(f);
viewHolderFinal.ttprice.setText(finalRowItem.getTotalPrice());
}
viewHolderFinal.add.setEnabled(true);
viewHolderFinal.sub.setEnabled(true);
} else {
viewHolderFinal.add.setEnabled(false);
viewHolderFinal.sub.setEnabled(false);
}
// get the quantity for this row item
viewHolderFinal.txtDesc.setText(finalRowItem.getDesc());
viewHolderFinal.add.setEnabled(true);
viewHolderFinal.sub.setEnabled(true);
String check =finalRowItem.getDesc()+finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getTotalPrice();
Toast.makeText(context,"Clicked on Checkbox check1: " +finalRowItem.getDesc()+finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getTotalPrice(), Toast.LENGTH_LONG).show();
Intent intt=new Intent(context,Manage.class);
intt.putExtra("allItem", check);
Log.d("final description in add", finalRowItem.getDesc());
// set the new description (that uses the updated qunatity)
}
});
holder.sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int quantity = finalRowItem.getQuantity();
if (quantity == 1) {
finalRowItem.setQuantity(quantity - 1);
Toast.makeText(context, "Item is unchecked", Toast.LENGTH_LONG).show();
viewHolderFinal.check.setChecked(false);
viewHolderFinal.add.setEnabled(false);
viewHolderFinal.sub.setEnabled(false);
} else {
viewHolderFinal.add.setEnabled(true);
viewHolderFinal.sub.setEnabled(true);
}
// get the quantity for this row item
finalRowItem.setQuantity(quantity - 1);
quantity=finalRowItem.getQuantity();
String p=finalRowItem.getPrice();
int x=Integer.parseInt(p);
int y=quantity*x;
String f=Integer.toString(y);
finalRowItem.setTotalPrice(f);
viewHolderFinal.ttprice.setText(finalRowItem.getTotalPrice());
//Toast.makeText(context, "898798",0).show();
// update it by subtracting 1
viewHolderFinal.txtDesc.setText(finalRowItem.getDesc());
Log.d("final description in sub", finalRowItem.getDesc());
// set the new description (that uses the updated qunatity)
}
});
// the view must be returned to our activity
//Toast.makeText(context,"Quantity selected"+finalRowItem.getTotalPrice() ,Toast.LENGTH_SHORT).show();
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
if (cb.isChecked()) {
Log.d("checkbox checked", "checkbox checked");
// RowItem finalRowItem = (RowItem) cb.getTag();
Toast.makeText(context,"Clicked on Checkbox: " +finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getDesc()+" is " + cb.isChecked(), Toast.LENGTH_LONG).show();
// finalRowItem.setSelected(cb.isChecked());
viewHolderFinal.add.setEnabled(true);
viewHolderFinal.sub.setEnabled(true);
finalRowItem.quantity =finalRowItem.getQuantity();
finalRowItem.quantity = 1;
viewHolderFinal.txtDesc.setText(finalRowItem.getDesc());
if (finalRowItem.quantity>=1) {
Log.e("checkbox checked-2", "checkbox checked-2");
String p=finalRowItem.getPrice();
int x=Integer.parseInt(p);
int y=finalRowItem.quantity*x;
String f=Integer.toString(y);
finalRowItem.setTotalPrice(f);
viewHolderFinal.ttprice.setText(finalRowItem.getTotalPrice());
//Toast.makeText(context, "898798",0).show();
Toast.makeText(context,"Clicked on Checkbox check1: " +finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getTotalPrice()+" is " + cb.isChecked(), Toast.LENGTH_LONG).show();
}
} else {
if (finalRowItem.quantity>=1) {
Log.d("checkbox checked-3", "checkbox checked-3");
String p=finalRowItem.getPrice();
int x=Integer.parseInt(p);
int y=finalRowItem.quantity*x;
y=0;
String f=Integer.toString(y);
finalRowItem.setTotalPrice(f);
viewHolderFinal.ttprice.setText(finalRowItem.getTotalPrice());
//Toast.makeText(context, "898798",0).show();
}
finalRowItem.quantity = finalRowItem.getQuantity();
if (finalRowItem.quantity > 0) {
finalRowItem.quantity = 0;
}
viewHolderFinal.txtDesc.setText(finalRowItem.getDesc());
Toast.makeText(context,"Clicked on Checkbox check2: " +finalRowItem.getTitle() +finalRowItem.getPrice()+finalRowItem.getDesc()+finalRowItem.getQuantity()+" is " + cb.isChecked(), Toast.LENGTH_LONG).show();
viewHolderFinal.add.setEnabled(false);
viewHolderFinal.sub.setEnabled(false);
}
}
});
return convertView;
}
}
答案 0 :(得分:0)
您可以通过两种简单的方式将项目从一个班级发送到另一个班级:
1. Save the items in SharedPrefernces and access them in another class.
2. Send them as part of intent using putExtra() api.
答案 1 :(得分:0)
编写实现Parcelable
的类。将其实例传递给intent。
有关用法,请参阅this。
答案 2 :(得分:0)
您可以在商品上实施Parcelable界面。
首先,你的RowItem应该实现Parcelable接口(例如:http://developer.android.com/reference/android/os/Parcelable.html)
然后你应该使用List而不是ArrayList,并把这个对象放在intent中:
Intent i = ...;
i.putParcelableArrayListExtra(rowItems);
答案 3 :(得分:0)
在您的MainActivity或您想要发送到其他活动的值的活动中,写下以下代码。
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("Username", YOUR STRING);
editor.putString("EMAIL", YOUR STRING);
editor.commit();
是一个键值对,其中username和EMAIL是键,value是你必须发送到其他活动的字符串。
然后在您想要检索数据的其他活动中添加以下代码。
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String text = preferences.getString("Username", "null");
String Email1=preferences.getString("EMAIL", "null");
String Email=Email1.toString();
希望这有帮助。