0

我正在开发一个餐厅 android 应用程序,我需要在其中使用标签栏来显示项目的变化。我使用http://code.google.com/p/android-custom-tabs制作了简单的标签。但在我的情况下,我必须创建选项卡取决于 Web 服务数据。例如,数据来自网络服务,例如:Starter、Soups、Noodles、Pasta、Sizzlers、juices。这里将创建六个选项卡。这里的数据来自网络服务有子数据像Starter有这么多真实性的开始,在side Soups这么多类型的汤......数据可以不断更新更新到网络服务所以不固定有多少应使其完全依赖于 Web 服务数据。现在我已经实现了如何使用下面的代码动态地制作标签。

Here result is a ArrayList That contain web service data
for(int i=0; i<results.size();i++){
        TabHost tabHost = getTabHost();
            // Tab for Menu
            TabSpec tab = tabHost.newTabSpec(item_details.getTab().toString());
            tab.setIndicator(itemDetailsrrayList.get(i).getTab(), getResources().getDrawable(R.drawable.all));
            Intent intent = new Intent(MainMenuActivity.this, BreakfastActivity.class);
            intent.putExtra("id", id);

            tab.setContent(intent);

            tabHost.addTab(tab);
        }

在这里,我混淆了成功创建的选项卡,但在每个选项卡中都会调用相同的 Activity -->BreakfastActivity.java但我想在每个按下的选项卡中显示具有不同 Web 服务数据的列表。例如当按下汤的标签时。应该显示汤的列表,当按下面条选项卡时,这里显示面条列表(所有数据来自网络服务)......等等

MainMenuActivity.java 我在哪里创建 TabHost

public class MainMenuActivity extends TabActivity{

    //SOAP WEB SERVICE CREDENTIALS
    private final String NAMESPACE = "http://xxx.org/";
    private final String URL = "http://xxx.xxx.com/WebServices/ResturantById.asmx?op=getMenu";
    private final String SOAP_ACTION = "http://xxx.org/getMenu";
    private final String METHOD_NAME = "getMenu";

    private Button btn_creat_order, btn_back_main_menu;
    String id;
    private ProgressDialog progressDialog = null;
    private SoapObject response = null;
    private Handler messageHandler = null;
    private ArrayList<ItemDetails> results;
    private ItemDetails item_details ;

    public void onCreate(Bundle savedInstanceState){
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_menu);

        //INITIALIZATION METHOD FOR VIEWS AND COMPONENTS 
        initComs();
        //NETWORKING METHOD
        initControls();

        //BACK BUTTON FOR PREVIOUSE SCREEN
        btn_back_main_menu.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                startActivity(new Intent(MainMenuActivity.this, ViewRestuarantFullActivity.class));
            }
        });

        //BUTTON FOR CREAT ORDER SCREEN
        btn_creat_order.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                startActivity(new Intent(MainMenuActivity.this, CreateOrderActivity.class));
            }
        });

    }

    private void initControls()
    {
        try
        {
            CallingDotNetWebServiceUsingSoap();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    private void CallingDotNetWebServiceUsingSoap()
    {
        try
        {
            progressDialog = ProgressDialog.show(MainMenuActivity.this, "Please Wait", "Loading...");
            new Thread()
            {
                public void run()
                {
                    try
                    {
                        //MAKING SOAP OBJECT
                        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                        //SEND PARAMETER TO WEB SERVICE
                        request.addProperty("ResturantId", id);

                        //MAKING ENVELOP
                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                        //TRUE IF WEB SERVICE MADE IN .NET
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(request);
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                        //CALL FOR WEB SERVICE
                        androidHttpTransport.call(SOAP_ACTION, envelope);
                        //RESPONSE COMING FROM WEB SERVICE
                        response = (SoapObject)envelope.getResponse();
                        SoapObject soapResult = (SoapObject)response.getProperty(0);
                        for(int i=0;i<soapResult.getPropertyCount();i++)
                        {
                           //SOAP OBJECT FOR PROPERTIES COMES FROM WEB SERVICE
                           SoapObject so = (SoapObject) soapResult.getProperty(i);

                           //fetching properties from web service and store it to item_detal bean class 
                           item_details = new ItemDetails();
                           item_details.setTab(so.getProperty("MenuItem").toString());

                           //-- ArrayList results will keep data 
                           results.add(item_details);
                        }
                        messageHandler.sendEmptyMessage(0);
                    }
                    catch (Exception e) 
                    {
                        e.printStackTrace();
                        progressDialog.dismiss();
                    }
                }
            }.start();

            messageHandler = new Handler()
            {
                @Override
                public void handleMessage(Message msg) 
                {
                    super.handleMessage(msg);
                    try{
                        progressDialog.dismiss();

                        itemDetailsrrayList = results;
                        for(int i=0; i<results.size();i++){

                        TabHost tabHost = getTabHost();
                        // Tab for Photos
                        TabSpec tab = tabHost.newTabSpec(item_details.getTab().toString());
                        tab.setIndicator(itemDetailsrrayList.get(i).getTab(), getResources().getDrawable(R.drawable.all));
                        Intent intent = new Intent(MainMenuActivity.this, BreakfastActivity.class);
                        intent.putExtra("id", id);

                        tab.setContent(intent);

                        tabHost.addTab(tab);
                    }

                    }catch (Exception e) {
                        // TODO: handle exception
                        Log.v("","Exception : "+e);
                    }
                }
            };  
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
    private static ArrayList<ItemDetails> itemDetailsrrayList;

    //INITIALIZATION OF VIEWS AND COMPONENTS ALSO DATA INITIALIZE WHICH COMES FROM PRECIOUS CALSS IN BUNDLE
    private void initComs(){
        btn_creat_order = (Button)findViewById(R.id.btn_create_order);
        btn_back_main_menu = (Button)findViewById(R.id.btn_back_main_menu);
        item_details = new ItemDetails();
        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            id = extras.getString("id");
            results = new ArrayList<ItemDetails>();
        }
    }
}

主菜单.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"
  android:background="@drawable/rat01"
  >

  <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="#F0F0F0"
  >
  <LinearLayout

          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"
          android:gravity="center"
          android:orientation="vertical" >
  <Button 
        android:id="@+id/btn_back_main_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/btnback"
        android:gravity="left"
        android:layout_gravity="left"
  />
  </LinearLayout>
    <TextView 
            android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Menu List"
              android:textSize="20dp"
              android:textStyle="bold"
              android:textColor="#000000"
              android:gravity="center"
              android:padding="10dp"
    />
    <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"
          android:gravity="center"
          android:orientation="vertical" >

          <Button 
            android:background="@drawable/btn_create_order"
            android:layout_gravity="right" 
            android:id="@+id/btn_create_order" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </Button>
      </LinearLayout>
  </LinearLayout>

<TabHost 
        android:id="@+id/tabhost"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent">
  <LinearLayout android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
    <HorizontalScrollView android:layout_width="fill_parent"
                          android:layout_height="wrap_content"
                          android:fillViewport="true"
                          android:scrollbars="horizontal" >
      <TabWidget android:id="@android:id/tabs"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"/>
    </HorizontalScrollView>
    <FrameLayout android:id="@android:id/tabcontent"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent" />
  </LinearLayout>
</TabHost>

</LinearLayout>

早餐活动.java

public class BreakfastActivity extends Activity {

    private ArrayList<ItemDetails> results;
    private BaseAdapterClassForMainMenu adapter;
    private ListView breakfast_list;
    private final String NAMESPACE = "http://xxx.org/";
    private final String URL = "http://xxx.xxx.com/WebServices/ResturantById.asmx?op=getMenu";
    private final String SOAP_ACTION = "http://xxx.org/getMenu";
    private final String METHOD_NAME = "getMenu";


    private ProgressDialog progressDialog = null;
    private SoapObject response = null;
    private Handler messageHandler = null;


    private ItemDetails item_details ;

    Button btn_gmap, btn_back;
    String id;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.breakfast);

        initComs();

        initControls();

    }

    private void initComs(){
        breakfast_list = (ListView)findViewById(R.id.breakfast_list);
        results = new ArrayList<ItemDetails>();
//      menuitem_name = (TextView)findViewById(R.id.txt_main_menu_item);

        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            id = extras.getString("id");
        }
        Log.v("","chunk to test : "+id);
    }

    private void initControls()
    {
        try
        {
            CallingDotNetWebServiceUsingSoap();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    private void CallingDotNetWebServiceUsingSoap()
    {
        try
        {
            progressDialog = ProgressDialog.show(BreakfastActivity.this, "Please Wait", "Loading...");
            new Thread()
            {
                public void run()
                {
                    try
                    {
                        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                        request.addProperty("ResturantId", id);
//                      request.addProperty("facility", 2);

                        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                        envelope.dotNet = true;
                        envelope.setOutputSoapObject(request);
                        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                        androidHttpTransport.call(SOAP_ACTION, envelope);
                        response = (SoapObject)envelope.getResponse();
                        Log.v("","Response : "+response);
                        SoapObject soapResult = (SoapObject)response.getProperty(0);
                        for(int i=0;i<soapResult.getPropertyCount();i++)
                        {
                           SoapObject so = (SoapObject) soapResult.getProperty(i);

                           item_details = new ItemDetails();
                           item_details.setMenu_id(so.getProperty("MenuItemId").toString());
                           item_details.setMenu_item(so.getProperty("MenuItem").toString());

                           results.add(item_details);
                        }
                        messageHandler.sendEmptyMessage(0);
                    }
                    catch (Exception e) 
                    {
                        e.printStackTrace();
                        progressDialog.dismiss();
                    }
                }
            }.start();

            adapter = new BaseAdapterClassForMainMenu(BreakfastActivity.this, results);

            messageHandler = new Handler()
            {
                @Override
                public void handleMessage(Message msg) 
                {
                    super.handleMessage(msg);

                    breakfast_list.setAdapter(adapter);
                    progressDialog.dismiss();
                }
            };  
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onStart(){
        super.onStart();
        Toast.makeText(BreakfastActivity.this, "OnStart()", Toast.LENGTH_SHORT).show(); 
    }
}

早餐.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:background="@drawable/rat01"
  >

        <ListView 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/breakfast_list"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
        >
        </ListView>

</LinearLayout>

希望我解释得很好。请帮助实现这一目标。先感谢您。

4

1 回答 1

0

这只是您选择与BreakfastActivity. 目前,您正在通过意图共享 ID。

        intent.putExtra("id", id);

您需要为活动共享足够的信息以在选项卡上显示相关结果。分享餐厅 ID、菜单 ID(早餐/午餐)、类别(汤/果汁)以及您需要将结果缩小到项目列表的任何其他信息。您可以使用Intent

Intent intent = getIntent();
int resId = intent.getIntExtra(MY_RESTAURANT);
int catId = intent.getIntExtra(MY_CATEGORY);
int menId = intent.getIntExtra(MY_MENU_ID);

根据传递给活动的 ID 在您的数据库中搜索项目。

于 2013-04-06T06:21:52.753 回答