タブバーを使用してアイテムのバリエーションを表示する必要があるレストランアンドロイドアプリを開発しています。http://code.google.com/p/android-custom-tabsを使用して簡単なタブを作成しました。しかし、私の場合、Web サービス データに依存するタブを作成する必要があります。たとえば、データは、スターター、スープ、ヌードル、パスタ、シズラー、ジュースなどの Web サービスから取得されます。ここでは 6 つのタブが作成されます。ここで、データは Web サービスから取得され、スターターには非常に多くのスターターがあり、サイド スープには非常に多くの種類のスープがあります......データは Web サービスに継続的に更新されるため、修正されません。タブは、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);
}
ここで混乱したタブが正常に作成されましたが、各タブで同じアクティビティが呼び出されます->BreakfastActivity.java
しかし、押された各タブで異なるWebサービスデータのリストを表示したいです。たとえば、スープのタブを押したとき。ヌードルタブを押すとスープのリストが表示されるはずです ヌードルのリストはここに表示されます(すべてのデータは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>
朝食Activity.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>
私がよく説明したことを願っています。これを達成するのを手伝ってください。前もって感謝します。