2016-04-30 8 views
0

私はアンドロイドの新機能ですが、自分のアプリケーションにナビゲーションメニューを作りたいと思います。私は正常にWebサービスからデータを取得します。しかし、私はどのようにナビゲーションメニューにWebサービスのメニュー項目を追加するナビゲーションメニューの問題がありますか? このアクティビティでは、最初にWebサービスを介してデータをフェッチし、jsonオブジェクトを配列オブジェクトに変換してから、printmenu関数を呼び出してナビゲーションにメニュー項目を追加します。このセクションで問題があります。あなたのAsyncTaskonPostExecuteアンドロイドのナビゲーションメニュー

public class MainActivity extends AppCompatActivity 
    { 
     private ArrayList<MenuItems> menuModel=new ArrayList<>(); 
      protected void onCreate(Bundle savedInstanceState) 
      { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_main); 
      } 
      public class MenuList extends AsyncTask<String,Void,Void> 
      { 
      Boolean result = true; 
      URL url; 
      HttpURLConnection urlConnection = null; 
      String[] response; 
      ProgressDialog dialog; 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       dialog=new ProgressDialog(MainActivity.this); 
      } 

      @Override 
      protected Void doInBackground(String... params) 
      { 
       try{ 
        response = new String[params.length]; 
        for(int i=0;i<params.length;i++) 
        { 
         url = new URL(params[i]); 
         urlConnection = (HttpURLConnection)  url.openConnection(); 
         urlConnection.setRequestMethod("POST"); 
         urlConnection.connect(); 
         int responseCode = urlConnection.getResponseCode(); 
         if(responseCode== HttpURLConnection.HTTP_OK) 
         { 
          InputStream in = urlConnection.getInputStream(); 
          response[i] = convertStreamToString(in); 
          Log.d("Passs", response[i]+""); 
         } 
        } 

       } 
       catch (Exception e) 
       { 
        Log.e("Error",e.getMessage()); 
        result = false; 
       } 
       return null; 
      } 
      @Override 
      protected void onPostExecute(Void aVoid) { 
       super.onPostExecute(aVoid); 
        if(result) 
        { 
         allMenuItems(response[0]); 
         //printCategory(response[0]); 

        } 
       else { 
        Toast.makeText(MainActivity.this,"Somthing bad happen ",Toast.LENGTH_LONG).show(); 
       } 
      } 
     } 
     private void allMenuItems(String response) 
     { 
      try 
      { 
       JSONObject jsonObject = new JSONObject(response); 
       JSONArray jsonArray = jsonObject.getJSONArray("menu"); 
       for(int i=0;i<jsonArray.length();i++) 
       { 
        JSONObject objMenu=jsonArray.getJSONObject(i); 
        MenuItems objectmenu=new MenuItems(); 
        objectmenu.setMenu_id(objMenu.getString("category_id")); 
        objectmenu.setMenuitem_item(objMenu.getString("category_name")); 
        objectmenu.setMenu_image(objMenu.getString("category_banner")); 
        menuModel.add(objectmenu); 
       } 
       printMenu(); 
      } 
      catch (Exception e) 
      { 
       e.getMessage(); 
      } 
     } 
     private void printMenu() 
     { 
      for(int i=0;i<=menuModel.size();i++) 
      { 



      } 
     } 
    } 
XML File 

<?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="wrap_content" 
    android:clickable="true"> 


     <LinearLayout 

      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <LinearLayout 
       android:id="@+id/linear_layout_menu" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical"> 

       <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:paddingTop="10dp" 
        android:textSize="20dp" 
        android:text="Category" 
        android:textStyle="bold" 
        android:textColorHint="@color/gry" 
        android:textColor="@color/gray" 
        android:layout_centerVertical="true" 
        android:layout_gravity="center_horizontal" 
        android:layout_toEndOf="@+id/image_view" 
        android:layout_toRightOf="@+id/image_view" 
        android:gravity="center_vertical" 
        android:paddingLeft="10dp"/> 


       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <ImageView 
         android:paddingTop="10dp" 
         android:paddingLeft="10dp" 
         android:id="@+id/image_view" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src = "@drawable/love" 

         /> 

        <TextView 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:paddingTop="10dp" 
         android:textSize="19dp" 
         android:text="Ring" 
         android:textStyle="normal" 
         android:textColor="@color/lightgray" 
         android:layout_centerVertical="true" 
         android:layout_gravity="center_horizontal" 
         android:layout_toEndOf="@+id/image_view" 
         android:layout_toRightOf="@+id/image_view" 
         android:gravity="center_vertical" 
         android:paddingLeft="10dp"/> 


       </RelativeLayout> 



       <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 

        <ImageView 
         android:paddingTop="10dp" 
         android:paddingLeft="10dp" 
         android:id="@+id/imageView52" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:src = "@drawable/bracelt"/> 

        <TextView 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:paddingTop="10dp" 
         android:textSize="19dp" 
         android:text="Bracelet" 
         android:textStyle="normal" 
         android:textColor="@color/lightgray" 
         android:layout_centerVertical="true" 
         android:layout_gravity="center_horizontal" 
         android:layout_toEndOf="@+id/imageView52" 
         android:layout_toRightOf="@+id/imageView52" 
         android:gravity="center_vertical" 
         android:paddingLeft="10dp"/> 

       </RelativeLayout> 
      </LinearLayout> 



      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 


       <View 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:layout_marginTop="25dp" 
        android:background="@color/lightgray"/> 

      <ImageView 
       android:layout_marginTop="30dp" 
       android:paddingTop="10dp" 
       android:paddingLeft="6dp" 
       android:id="@+id/imageView57" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src = "@drawable/ratenew"/> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="35dp" 
       android:textSize="16dp" 
       android:textStyle="normal" 
       android:text="Rate Us" 
       android:textColor="@color/lightgray" 
       android:layout_centerVertical="true" 
       android:layout_gravity="center_horizontal" 
       android:layout_toEndOf="@+id/imageView57" 
       android:layout_toRightOf="@+id/imageView57" 
       android:gravity="center_vertical" 
       android:paddingLeft="10dp"/></RelativeLayout> 


      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

      <ImageView 
       android:paddingTop="10dp" 
       android:paddingLeft="6dp" 
       android:id="@+id/imageView58" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src = "@drawable/gestures"/> 
      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="10dp" 
       android:textSize="16dp" 
       android:textStyle="normal" 
       android:paddingLeft="10dp" 
       android:text="Today's Deal" 
       android:textColor="@color/lightgray" 
       android:layout_centerVertical="true" 
       android:layout_gravity="center_horizontal" 
       android:layout_toEndOf="@+id/imageView58" 
       android:layout_toRightOf="@+id/imageView58" 
       android:gravity="center_vertical" 

       /> 
    </RelativeLayout> 
      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

      <ImageView 
       android:paddingTop="10dp" 
       android:paddingLeft="6dp" 
       android:id="@+id/imageView59" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src = "@drawable/contect"/> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="10dp" 
       android:textSize="16dp" 
       android:paddingLeft="10dp" 
       android:textStyle="normal" 
       android:text="Contact Us" 
       android:textColor="@color/lightgray" 
       android:layout_centerVertical="true" 
       android:layout_gravity="center_horizontal" 
       android:layout_toEndOf="@+id/imageView59" 
       android:layout_toRightOf="@+id/imageView59" 
       android:gravity="center_vertical" 
       /> 
      </RelativeLayout> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

      <ImageView 
       android:paddingTop="10dp" 
       android:id="@+id/imageView60" 
       android:paddingLeft="6dp" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src = "@drawable/about"/> 

      <TextView 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="10dp" 
       android:textSize="16dp" 
       android:paddingLeft="10dp" 
       android:textStyle="normal" 
       android:text="About Us" 
       android:textColor="@color/lightgray" 
       android:layout_centerVertical="true" 
       android:layout_gravity="center_horizontal" 
       android:layout_toEndOf="@+id/imageView60" 
       android:layout_toRightOf="@+id/imageView60" 
       android:gravity="center_vertical" 
       /> 
      </RelativeLayout> 
      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center"> 

       <View 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:layout_marginTop="20dp" 

        android:background="@color/lightgray"/> 


       <ImageView 
        android:paddingTop="5dp" 
        android:paddingLeft="90dp" 
        android:paddingRight="0dp" 
        android:layout_marginTop="30dp" 
        android:paddingBottom="7dp" 
        android:id="@+id/imageView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/fb" /> 

       <ImageView 
        android:paddingTop="13dp" 
        android:paddingLeft="5dp" 

        android:layout_marginTop="20dp" 
        android:paddingBottom="7dp" 
        android:id="@+id/imageView2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src = "@drawable/inst" 
        android:layout_toEndOf="@+id/imageView1" 
        android:layout_toRightOf="@+id/imageView1"/> 

       <ImageView 
        android:paddingTop="5dp" 
        android:paddingLeft="5dp" 
        android:paddingRight="20dp" 
        android:paddingBottom="7dp" 
        android:layout_marginTop="30dp" 
        android:id="@+id/imageView3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src = "@drawable/tw" 
        android:layout_toEndOf="@+id/imageView2" 
        android:layout_toRightOf="@+id/imageView2"/> 

      </RelativeLayout> 





     </LinearLayout> 
</RelativeLayout> 
+0

どのようなエラーが表示されますか?あなたのログも投稿してください。 –

+0

エラーはありません。これは、メニューアイテムdynamicallを配置する方法と、メニューアイテムIDに従って、1つのアクティビティから別のアクティビティへリダイレクトする方法についてはわかりません。 –

答えて

1

invalidateOptionsMenu()を呼び出します。それでonCreateOptionsMenuメソッドを再度呼び出す必要があります。 onCreateOptionsMenuには、menuModelMenuItemを追加してください。

関連する問題