我是android的新手。我正在尝试使用内置viewpager
的2个标签实现listview
。
当scroll.Listview
包含一个spinner
和三个textview
将使用适配器生成时,会出现此问题。如果我从生成的第一个微调器中选择项目,那么我得到它的值。但是当我向下选择不同的微调器时,我先前选择的微调器被选择到位置0。
这是代码
public class Baggage_Home extends AppCompatActivity{
TabLayout tabs;
private ViewPager viewPager;
public static TextView extra_total;
private int[] tabIcons = {
R.drawable.ic_business_center_black_24dp,
R.drawable.ic_room_service_black_24dp,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baggage_meal);
extra_total=(TextView)findViewById(R.id.extra_total);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(2);
setupViewPager(viewPager);
extra_total.setText("₹ "+String.valueOf(InternationalRoundTrip.total));
tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
setupTabIcons();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Extra Services");
setSupportActionBar(toolbar);
}
private void setupTabIcons() {
tabs.getTabAt(0).setIcon(tabIcons[0]);
tabs.getTabAt(1).setIcon(tabIcons[1]);
}
@Override
public void onBackPressed() {
if(!Flight_search_new.trm_cty_code_cnt.equals("IN") || !Flight_search_new.to_cty_code_cnt.equals("IN")) {
if(Flight_search_new.valtype=="Internationaloneway"){
Intent intent = new Intent(Baggage_Home.this,FlightBookingPage_Int.class);
startActivity(intent);
}else{
Intent intent = new Intent(Baggage_Home.this,RoundFlightBookingPage_Int.class);
intent.putExtra("BAGGAGE","abcd");
startActivity(intent);
}
}else
{
Intent intent = new Intent(Baggage_Home.this,FlightBookingPage.class);
startActivity(intent);
}
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new
ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Baggage_display(), "Baggage");
adapter.addFragment(new Meal_display(), "Meal");
viewPager.setAdapter(adapter);
}
}
行政类别的XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout1"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:id="@+id/tabs"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed"
>
<android.support.design.widget.TabItem
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Baggage"
/>
<android.support.design.widget.TabItem
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Meal"
/>
</android.support.design.widget.TabLayout>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="@color/btn_color"
android:id="@+id/extra_total"
android:layout_gravity="bottom|center"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/layout1"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</LinearLayout>
BAGGAGE DISPLAY CLASS
public class Baggage_display extends Fragment {
ArrayList<Baggage_details> bagdetails=new ArrayList<Baggage_details>();
Baggage_Adapter baggageAdapter;
ListView listView;
public static ArrayList<String> title_adult_bag_arr=new ArrayList<String>();
public static ArrayList<String> title_adult_bag_arr_rtn=new ArrayList<String>();
public static ArrayList<String> title_child_bag_arr=new ArrayList<String>();
public static ArrayList<String> title_child_bag_arr_rtn=new ArrayList<String>();
LinearLayout lvs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View views = inflater.inflate(R.layout.baggage_display, container, false);
listView=(ListView)views.findViewById(R.id.listView);
bagdetails.clear();
title_adult_bag_arr.clear();
title_adult_bag_arr_rtn.clear();
title_child_bag_arr.clear();
title_child_bag_arr_rtn.clear();
for (int i = 0; i < Integer.parseInt(Flight_search_new.adult); i++)
{
if(Flight_search_new.valtype.equals("Internationalround"))
{
if(InternationalRoundTrip.api.equals("galileo"))
{
if (InternationalRoundTrip.provider.equals("ACH"))
{
bagdetails.add(new Baggage_details("Onward", Flight_search_new.trm_cty_code +"-"+ Flight_search_new.to_cty_code,"Adult -"+(i+1),RoundFlightBookingPage_Int.ach_adl_baggage));
baggageAdapter=new Baggage_Adapter(getContext(),bagdetails);
if(RoundFlightBookingPage_Int.ach_adl_baggage_rtn != null) {
bagdetails.add(new Baggage_details("Return", Flight_search_new.to_cty_code + "-" + Flight_search_new.trm_cty_code, "Adult -" + (i + 1), RoundFlightBookingPage_Int.ach_adl_baggage_rtn));
baggageAdapter = new Baggage_Adapter(getContext(), bagdetails);
}
System.out.println("WWWWW"+RoundFlightBookingPage_Int.ach_adl_baggage.toString());
System.out.println("RRRRR"+RoundFlightBookingPage_Int.ach_adl_baggage_rtn.toString());
}
else
{
}
}
else
{
bagdetails.add(new Baggage_details("Onward", Flight_search_new.trm_cty_code +"-"+ Flight_search_new.to_cty_code,"Adult -"+(i+1),RoundFlightBookingPage_Int.titlebag));
baggageAdapter=new Baggage_Adapter(getContext(),bagdetails);
if(RoundFlightBookingPage_Int.titlebag_return != null) {
bagdetails.add(new Baggage_details("Return", Flight_search_new.to_cty_code + "-" + Flight_search_new.trm_cty_code, "Adult -" + (i + 1), RoundFlightBookingPage_Int.titlebag_return));
baggageAdapter = new Baggage_Adapter(getContext(), bagdetails);
}
}
}
else{
Toast.makeText(getContext(), "Not Int Round", Toast.LENGTH_SHORT).show();
}
}
if(Integer.parseInt(Flight_search_new.child) > 0) {
for (int i = 0; i < Integer.parseInt(Flight_search_new.child); i++) {
if (InternationalRoundTrip.api.equals("galileo")) {
if (InternationalRoundTrip.provider.equals("ACH")) {
bagdetails.add(new Baggage_details("Onward", Flight_search_new.trm_cty_code + "-" + Flight_search_new.to_cty_code, "Child -" + (i + 1), RoundFlightBookingPage_Int.ach_child_bag));
baggageAdapter = new Baggage_Adapter(getContext(), bagdetails);
if (RoundFlightBookingPage_Int.ach_adl_baggage_rtn != null) {
bagdetails.add(new Baggage_details("Return", Flight_search_new.to_cty_code + "-" + Flight_search_new.trm_cty_code, "Child -" + (i + 1), RoundFlightBookingPage_Int.ach_chi_bag_rtn));
baggageAdapter = new Baggage_Adapter(getContext(), bagdetails);
}
System.out.println("CCCCCC"+RoundFlightBookingPage_Int.ach_child_bag.toString());
System.out.println("PPPPP"+RoundFlightBookingPage_Int.ach_chi_bag_rtn.toString());
} else {
}
} else {
bagdetails.add(new Baggage_details("Onward", Flight_search_new.trm_cty_code +"-"+ Flight_search_new.to_cty_code,"Child -"+(i+1),RoundFlightBookingPage_Int.titlebag));
baggageAdapter=new Baggage_Adapter(getContext(),bagdetails);
if(RoundFlightBookingPage_Int.titlebag_return!= null) {
bagdetails.add(new Baggage_details("Return", Flight_search_new.to_cty_code + "-" + Flight_search_new.trm_cty_code, "Child -" + (i + 1), RoundFlightBookingPage_Int.titlebag_return));
baggageAdapter = new Baggage_Adapter(getContext(), bagdetails);
}
}
}
}
return views;
}
}
用餐显示类
public class Meal_display extends Fragment {
ArrayList<Meal_details> bagdetails=new ArrayList<Meal_details>();
public static ArrayList<String> title_adult_meal_arr=new ArrayList<String>();
public static ArrayList<String> title_adult_meal_arr_rtn=new ArrayList<String>();
public static ArrayList<String> title_child_meal_arr=new ArrayList<String>();
public static ArrayList<String> title_child_meal_arr_rtn=new ArrayList<String>();
Meal_Adapter baggageAdapter;
ListView listView;
LinearLayout lvs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View views = inflater.inflate(R.layout.meal_display, container, false);
listView=(ListView)views.findViewById(R.id.listView);
title_adult_meal_arr.clear();
title_adult_meal_arr_rtn.clear();
title_child_meal_arr.clear();
title_child_meal_arr_rtn.clear();
for (int i = 0; i < Integer.parseInt(Flight_search_new.adult); i++)
{
if(Flight_search_new.valtype.equals("Internationalround"))
{
if(InternationalRoundTrip.api.equals("galileo"))
{
if (InternationalRoundTrip.provider.equals("ACH"))
{
bagdetails.add(new Meal_details("Onward", Flight_search_new.trm_cty_code +"-"+ Flight_search_new.to_cty_code,"Adult -"+(i+1),RoundFlightBookingPage_Int.ach_adl_meal_onward));
baggageAdapter=new Meal_Adapter(getContext(),bagdetails);
if(RoundFlightBookingPage_Int.ach_adl_meal_rtn != null && !RoundFlightBookingPage_Int.ach_adl_meal_rtn.isEmpty())
{
bagdetails.add(new Meal_details("Return", Flight_search_new.to_cty_code + "-" + Flight_search_new.trm_cty_code, "Adult -" + (i + 1), RoundFlightBookingPage_Int.ach_adl_meal_rtn));
baggageAdapter = new Meal_Adapter(getContext(), bagdetails);
}
}
else
{
}
}
else
{
for (int k = 0; k < Round_ViewFlight.final_sec_meal.size(); k++) {
String src_des = Round_ViewFlight.final_sec_meal.get(k);
bagdetails.add(new Meal_details("Onward", src_des, "Adult -" + (i + 1), RoundFlightBookingPage_Int.titlemeal));
baggageAdapter = new Meal_Adapter(getContext(), bagdetails);
}
if(RoundFlightBookingPage_Int.titlemeal_return != null && !RoundFlightBookingPage_Int.titlemeal_return.isEmpty())
{
for (int k = 0; k < Round_ViewFlight.final_sec_meal_return.size(); k++) {
String src_des1 = Round_ViewFlight.final_sec_meal_return.get(k);
bagdetails.add(new Meal_details("Return", src_des1, "Adult -" + (i + 1), RoundFlightBookingPage_Int.titlemeal_return));
baggageAdapter = new Meal_Adapter(getContext(), bagdetails);
}
}
}
}
else{
Toast.makeText(getContext(), "Not Int Round", Toast.LENGTH_SHORT).show();
}
}
if(Integer.parseInt(Flight_search_new.child) > 0) {
for (int i = 0; i < Integer.parseInt(Flight_search_new.child); i++) {
if (InternationalRoundTrip.api.equals("galileo")) {
if (InternationalRoundTrip.provider.equals("ACH")) {
bagdetails.add(new Meal_details("Onward", Flight_search_new.trm_cty_code + "-" + Flight_search_new.to_cty_code, "Child -" + (i + 1), RoundFlightBookingPage_Int.ach_child_meal));
baggageAdapter = new Meal_Adapter(getContext(), bagdetails);
if (RoundFlightBookingPage_Int.ach_adl_baggage_rtn != null && !RoundFlightBookingPage_Int.ach_adl_baggage_rtn.isEmpty()) {
bagdetails.add(new Meal_details("Return", Flight_search_new.to_cty_code + "-" + Flight_search_new.trm_cty_code, "Child -" + (i + 1), RoundFlightBookingPage_Int.ach_child_meal_rtn));
baggageAdapter = new Meal_Adapter(getContext(), bagdetails);
}
} else {
}
}
else {
for (int k = 0; k < Round_ViewFlight.final_sec_meal.size(); k++) {
String src_des = Round_ViewFlight.final_sec_meal.get(k);
bagdetails.add(new Meal_details("Onward", src_des, "Child -" + (i + 1), RoundFlightBookingPage_Int.titlemeal));
baggageAdapter = new Meal_Adapter(getContext(), bagdetails);
}
if(RoundFlightBookingPage_Int.titlemeal_return != null && !RoundFlightBookingPage_Int.titlemeal_return.isEmpty())
{
for (int k = 0; k < Round_ViewFlight.final_sec_meal_return.size(); k++) {
String src_des1 = Round_ViewFlight.final_sec_meal_return.get(k);
bagdetails.add(new Meal_details("Return", src_des1, "Child -" + (i + 1), RoundFlightBookingPage_Int.titlemeal_return));
baggageAdapter = new Meal_Adapter(getContext(), bagdetails);
}
}
}
}
}
listView.setAdapter(baggageAdapter);
return views;
}
}
MEALCLASS的GETTER SETTER
public class Meal_Adapter extends BaseAdapter {
Context context;
public static ArrayList<String> spinner_items=new ArrayList<String>();
public static ArrayList<Meal_details> rowItems;
public static ArrayList<String> jeet_aar_meal=new ArrayList<String>();;
ArrayList<String> Bag_selected_amount= new ArrayList<String>();
ArrayList<String> Bag_selected__key= new ArrayList<String>();
Meal_Adapter(Context context, ArrayList rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position)
{
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
TextView sector,status;
TextView title_count;
Spinner spinner;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.meal_adapter, null);
holder = new ViewHolder();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.status = (TextView) convertView.findViewById(R.id.meal_status);
holder.sector = (TextView) convertView.findViewById(R.id.sector);
holder.title_count = (TextView) convertView.findViewById(R.id.title_count);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner);
try
{
final Meal_details row_pos = rowItems.get(position);
String status = String.valueOf(row_pos.getstatus());
String title_count = String.valueOf(row_pos.gettitle_count());
String sector=String.valueOf(row_pos.getsector_name());
holder.status.setText(status);
holder.sector.setText(sector);
holder.title_count.setText(title_count);
holder.spinner.setTag(title_count+status+sector);
holder.spinner.setOnItemSelectedListener(MealSelectedListener);
spinner_items = row_pos.getbaggage_list();
holder.spinner = spinnerDetails(convertView, R.id.spinner, String.valueOf(rowItems.get(position).getbaggage_list()));
}
catch (Exception e)
{
Log.e("PASS_ADAP ERROR:", e.getMessage());
}
convertView.setTag(holder);
return convertView;
}
private Spinner spinnerDetails(View v, int resId, String text) {
Spinner mySpinner = (Spinner) v.findViewById(resId);
@SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(context,
android.R.layout.simple_spinner_dropdown_item,
spinner_items);
mySpinner.setAdapter(spinnerArrayAdapter);
return mySpinner;
}
}
BAGGAGE CLASS的GETTER SETTER
public class Baggage_Adapter extends BaseAdapter {
Context context;
public static ArrayList<Baggage_details> rowItems;
public static ArrayList<String> spinner_items=new ArrayList<String>();
public static ArrayList<String> jeet_aar_bag=new ArrayList<String>();
ArrayList<String> Bag_selected_amount= new ArrayList<String>();
ArrayList<String> Bag_selected__key= new ArrayList<String>();
Baggage_Adapter(Context context, ArrayList rowItems) {
this.context = context;
this.rowItems = rowItems;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position)
{
return rowItems.indexOf(getItem(position));
}
/* private view holder class */
private class ViewHolder {
TextView sector,status;
TextView title_count;
Spinner spinner;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.baggage_adapter, null);
holder = new ViewHolder();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.status = (TextView) convertView.findViewById(R.id.bag_status);
holder.sector = (TextView) convertView.findViewById(R.id.sector);
holder.title_count = (TextView) convertView.findViewById(R.id.title_count);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner);
try
{
final Baggage_details row_pos = rowItems.get(position);
String status = String.valueOf(row_pos.getstatus());
String title_count = String.valueOf(row_pos.gettitle_count());
String sector=String.valueOf(row_pos.getsector_name());
holder.status.setText(status);
holder.sector.setText(sector);
holder.title_count.setText(title_count);
holder.spinner.setTag(title_count+status+sector);
holder.spinner.setSelected(true);
holder.spinner.setOnItemSelectedListener(BaggageSelectedListener);
spinner_items = row_pos.getbaggage_list();
holder.spinner = spinnerDetails(convertView, R.id.spinner, String.valueOf(rowItems.get(position).getbaggage_list()));
}
catch (Exception e)
{
Log.e("PASS_ADAP ERROR:", e.getMessage());
}
convertView.setTag(holder);
return convertView;
}
private Spinner spinnerDetails(View v, int resId, String text) {
Spinner mySpinner = (Spinner) v.findViewById(resId);
@SuppressWarnings({ "unchecked", "rawtypes" })
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(context,
android.R.layout.simple_spinner_dropdown_item,
spinner_items);
mySpinner.setAdapter(spinnerArrayAdapter);
return mySpinner;
}
}
任何人都可以指导我出错的地方。
我用过
viewPager.setOffscreenPageLimit(2);
但它似乎不起作用。
答案 0 :(得分:0)
如果我从生成的第一个微调器中选择项目,那么我得到的值 它。但是当我选择不同的旋转器然后旋转器,我 之前选择的选中位置为0。
在您的Baggage_details DTO中保留商店微调器选择的索引:
例如: -
1)使用getter / setter在您的Baggage_details类中添加一个新字段(例如-spinnerIndex = 0)。
2)获取选定的索引并在适配器的getView()中的微调器中设置
holder.spinner.setSelection(row_pos.getSpinnerIndex());
3)将此内容写入BaggageSelectedListener
row_pos.setSpinnerIndex(userSelectedIndexOfSpinner)