我在我的程序中使用了一个viewpager,并且我正在显示自定义列表视图。在每个viewpager滑动我显示相同的列表视图但具有不同的列表项。当我点击提交按钮时,我希望从所有这些列表视图中检查所有项目。
但是这个提交按钮不在我的viewpager上。对我来说这很复杂。这是我的主类代码(我只是显示我的代码的一部分,现在代码中没有错误):
public class HotelMenu extends FragmentActivity implements CompletionListener {
static final int ITEMS = 10;
MyAdapter mAdapter;
ViewPager mPager;
private NetworkTask networkTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotel_menu);
get_restaurant_menu();
mAdapter = new MyAdapter(getSupportFragmentManager(),HotelMenu.this);
mPager = (ViewPager) findViewById(R.id.pager);//adapter for viewpager
Button submit= (Button) findViewById(R.id.next);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//here I want all the items checked from the listview
}
});
}
以下是此课程的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip"
android:background="@drawable/home_screen_background">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tvRestNameForMenu"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_gravity="center_vertical"
android:background="@drawable/offers_text"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:alpha="0.7"
android:background="@android:color/white">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center"
android:measureWithLargestChild="true"
android:orientation="horizontal" >
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:background="@drawable/button_shape"
android:textColor="@android:color/white">
</Button>
</LinearLayout>
</LinearLayout>
这是我对viewpager的支持:
public static class MyAdapter extends FragmentStatePagerAdapter {
Context context;
public MyAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
this.context=context;
}
@Override
public int getCount() {
return ITEMS;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show list of maincourse items
// return ArrayListFragment.newInstance(position, mainCourse);//pass viewpager page position and list to be displayed
case 1: // Fragment # 1 - This will show list of soups
return ArrayListFragment.newInstance(position,startersName,startersCost );
default:// Fragment # 2-9 - Will show list
return ArrayListFragment.newInstance(position, maincourseName,maincourseCost);
}
}
}
最后这是我在viewpager上显示listview的片段:
public class ArrayListFragment extends android.support.v4.app.Fragment{
int fragNum;
String[] arr;
ArrayList<String>item_name;
ArrayList<String>item_cost;
ListView lvsoups;
MyAdapter myAdapter;
Context context;
LayoutInflater inflater1;
View layoutView;
public static final ArrayListFragment newInstance(int val, ArrayList<String> itemName, ArrayList<String> itemCost){
ArrayListFragment list=new ArrayListFragment();
//Bundle is used to use values and array passed through hotel menu class
Bundle bundle = new Bundle(2);
bundle.putInt("val", val);
bundle.putStringArrayList("itemName",itemName);
bundle.putStringArrayList("itemCost",itemCost);
list.setArguments(bundle);
return list ;
}
/**
* Retrieving this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//init passed values
fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
item_name=getArguments().getStringArrayList("itemName");
item_cost=getArguments().getStringArrayList("itemCost");
}
/**
* to set view to the fragment activity
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.fragment_pager_list,
container, false);
lvsoups = (ListView) layoutView.findViewById(R.id.listMenu);
myAdapter = new MyAdapter(getActivity(), item_name,item_cost);
lvsoups.setAdapter(myAdapter);//set adapter to the list view
return layoutView;
}
//adapter for the listview
public class MyAdapter extends BaseAdapter {
ArrayList<String> itemName;
ArrayList<String> itemCost;
Context context;
LayoutInflater inflater;
public MyAdapter(Context context, ArrayList<String> item_name, ArrayList<String> item_cost) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(item_name!=null)
{
// arr = new String[objects.length];
this.itemName = item_name;
this.itemCost=item_cost;
}
}
@Override
public int getCount() {
return itemName.size();
}
@Override
public Object getItem(int position) {
return itemName.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int value;
View menuRow = inflater.inflate(R.layout.hotel_menu_list_item, null);
TextView tvSoups = (TextView) menuRow.findViewById(R.id.tvMenuItem); //To set name of menu item, for example Corn Soup
TextView tvPrice = (TextView) menuRow.findViewById(R.id.tvMenuPrice); //To set price of that item
CheckBox cbQty = (CheckBox) menuRow.findViewById(R.id.cbMenuItem);
Spinner spQty = (Spinner) menuRow.findViewById(R.id.spinnerQty);
String[] quantity = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
ArrayAdapter spinnerAdapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, quantity);
spQty.setAdapter(spinnerAdapter);
tvSoups.setText(itemName.get(position));
tvPrice.setText(itemCost.get(position));
Log.e("arr", itemName.get(position));
if (cbQty.isChecked()) {
value = 1;
} else {
value = 0;
}
menuRow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Intent intent=new Intent(SearchResult.this,RestMainActivity.class);
// intent.putExtra("Hotel_Master_Id",hotelMasterIdList.get(position));
// startActivity(intent);
}
});
return menuRow;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
这是我的hotel_menu_list_item.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:weightSum="5">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cbMenuItem" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:layout_weight="3"
android:id="@+id/tvMenuItem" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:layout_weight="1"
android:id="@+id/tvMenuPrice" />
<Spinner
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/spinnerQty"
android:layout_weight="1"
>
</Spinner>
</LinearLayout>