使用ListFragement时出错

时间:2014-10-29 12:52:23

标签: java android eclipse android-fragments

我正在开发一个用于在线支付的Android应用程序,我想在其中显示名为Restaurants的选项卡下的餐馆列表。与此相同,其他一些选项卡将在它们下面有一个列表,并且一些选项卡将在它们下面有形式(如果可能并且不困难,否则我将仅保留列表)

这是我写的代码。它包含Adapter类中的错误,可能是一些我不确定的逻辑错误,因为这是我的第一个Android应用程序。

Main.java文件

package com.example.tabswithswipe;
import com.example.tabsswipe.adapter.TabsPagerAdapter;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;

    private String[] tabs = { "Retaurants", "Super Store", "Fuel Stations"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        

        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }

}

acivity_main.xml文件

<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

items_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:contentDescription="@string/app_name" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

</LinearLayout>

TabPagerAdapter.java适配器文件(此文件中的错误)

package com.example.tabsswipe.adapter;
import com.example.tabsswipe.*;

import android.app.ListFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

        switch (index) {
        case 0:
           return new SuperStoreFragment();
        case 1:
            return new FuelStationsFragment();
        case 2:
            return new RestaurantsFragment();   //Error Here

        }

        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

RestaurantsFragment.java

package com.example.tabsswipe.adapter;
//import com.example.MainActivity.MyAdapter;
import com.example.tabswithswipe.R;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RestaurantsFragment extends ListFragment {

    LayoutInflater inflater;
    ViewGroup container;
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
               Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      setContentView(R.layout.activity_main);
        this.inflater = inflater;

        View rootView = inflater.inflate(R.layout.list_items, container, false);
        setListAdapter(new MyAdapter(getActivity(), android.R.layout.simple_list_item_1, R.id.textView1, getResources().getStringArray(R.array.items)));
        return rootView;
     }

    private class MyAdapter extends ArrayAdapter<String>
    {

        public MyAdapter(Context context, int resource, int textViewResourceId,
                String[] strings) {
            super(context, resource, textViewResourceId, strings);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
    //      LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.list_items, container, false);
            String [] items = getResources().getStringArray(R.array.items);
            ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
            TextView tv = (TextView) row.findViewById(R.id.textView1);

            tv.setText(items[position]);

            if(items[position].equals("kfc"))
            {
                iv.setImageResource(R.drawable.kfc);
            }
            else if(items[position].equals("pizzaHut"))
            {
                iv.setImageResource(R.drawable.pizzahut);
            }
            else if(items[position].equals("Domino"))
            {
                iv.setImageResource(R.drawable.dominos);
            }
            else if(items[position].equals("hardees") )
            {
                iv.setImageResource(R.drawable.hardees);
            }
            else if(items[position].equals("TuttiFrutti"))
            {
                iv.setImageResource(R.drawable.tuttifrutti);
            }
            else if(items[position].equals("McDonalds"))
            {
                iv.setImageResource(R.drawable.mcdonalds);
            }
            else if(items[position].equals("21 Street"))
            {
                iv.setImageResource(R.drawable.ic_launcher);
            }

            return row;
        }

    }
     //   View rootView = inflater.inflate(R.layout.fragment_movies, container, false);

       // return rootView;
   // }



}

SuperStoreFragment.java

package com.example.tabsswipe.adapter;

import com.example.tabswithswipe.R;

import android.app.ListFragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class SuperStoreFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_super_stores, container, false);

        return rootView;
    }
}

fragment_super_stores.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fa6a6a" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Design Super Stores Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>


</RelativeLayout>

FuelStationsFragment.java

package com.example.tabsswipe.adapter;
import com.example.tabswithswipe.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FuelStationsFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_fuel_stations, container, false);

        return rootView;
    }
}

fragment_fuel_stations.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ff8400" >

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Design Fuel Stations Screen"
        android:textSize="20dp"
        android:layout_centerInParent="true"/>


</RelativeLayout>

请告诉我发生的错误,地点和错误。任何形式的帮助都将非常受欢迎,例如:教程,代码示例甚至是此代码的错误解决方案。如果有的话,请忽略我的愚蠢。谢谢。

0 个答案:

没有答案