在ListView中,将第一个项目作为标题和索引问题

时间:2016-12-29 11:04:46

标签: android listview navigation-drawer

我正在尝试显示导航抽屉的标题,它显示正常,但问题是当我试图点击导航抽屉中的第一个项目时,它将显示Queues.class而不是预期的类。当点击导航抽屉应用程序中的最后一项时会崩溃。我只想在导航抽屉中显示标题,最后一项单击将显示正确的活动。也是问题是标题显示两次。

package com.abc;
//import android.app.Activity;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.R.*;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.BaseBundle;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class navigation_drawer_class extends Activity
//public class navigation_drawer_class extends ListActivity

{
     private static final int Copy = 0;
     int a =0;
     public static FrameLayout frameLayout;
     TextView mytextview;
     public static ListView mDrawerList;
     public DrawerLayout mDrawerLayout;
     String Fullname;
     protected String[] listArray = {"Home","Queue","Inbox","Create Ticket","Search","Clients","App settings"};
     protected static int position;
     private static boolean isLaunch = true;

     //*****************************************

     JSONObject post_details_obj,post_obj;

     public static String FIRST_NAME="first_name",LAST_NAME="last_name";
     JSONArray staff_data_array;
     //*****************************************
     private ActionBarDrawerToggle actionBarDrawerToggle;
     Operation op=new Operation();

     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      //------------------ THIS ONE -------------------------
      getActionBar().setHomeAsUpIndicator(R.drawable.crop3);//THIS ONE FOR THE DRAWER LOGO
      //--------------------- THIS ONE ----------------------

      frameLayout = (FrameLayout)findViewById(R.id.content_frame);
      mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
      mDrawerList = (ListView) findViewById(R.id.left_drawer);
     /// mDrawerList.setAdapter(null);
      //***************** To Set the Welcome client ******************************************

    //========= THIS ONE ===============

         new getname().execute();
        //========= THIS ONE ===============
          /*  mDrawerList.setAdapter(null);
            View header = (View)getLayoutInflater().inflate(R.layout.headerview,null);

            TextView headerValue = (TextView) header.findViewById(R.id.headerview_id);
            headerValue.setText("Mydata");

            mDrawerList.addHeaderView(header, null, false);//addHeaderView(header, null, false) 
            */
          /* TextView tv=new TextView(getApplicationContext());
          Log.d("Fullname in oncreate : ",Fullname.toString());
          tv.setText(Fullname);  */


      //******************To Set the Welcome client  ******************************************

      mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listArray));



      mDrawerList.setOnItemClickListener(new OnItemClickListener() 
      {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                 int position, long id) {

                openActivity(position);
               }
      });
      getActionBar().setDisplayHomeAsUpEnabled(true);
      getActionBar().setHomeButtonEnabled(true);

          actionBarDrawerToggle = new ActionBarDrawerToggle(
            this,      // host Activity 
        mDrawerLayout,     // DrawerLayout object 
        R.drawable.ic_launcher,     // nav drawer image to replace 'Up' caret 
        R.string.open_drawer,       // "open drawer" description for accessibility 
        R.string.close_drawer)      // "close drawer" description for accessibility 
      {
       @Override
       public void onDrawerClosed(View drawerView) 
       {
        getActionBar().setTitle(listArray[position]);
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        super.onDrawerClosed(drawerView);
       }

       @Override
       public void onDrawerOpened(View drawerView)
       {
           getActionBar().setTitle(getString(R.string.app_name));
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            super.onDrawerOpened(drawerView);
       }

       @Override
       public void onDrawerSlide(View drawerView, float slideOffset) 
       {
        super.onDrawerSlide(drawerView, slideOffset);
       }

       @Override
       public void onDrawerStateChanged(int newState) 
       {
        super.onDrawerStateChanged(newState);
       }
      };
    mDrawerLayout.setDrawerListener(actionBarDrawerToggle);

    if(isLaunch){
       isLaunch = false;
       openActivity(0);

      }
     }

     protected void openActivity(int position) {

    //  mDrawerList.setItemChecked(position, true);
    //  setTitle(listArray[position]);
      mDrawerLayout.closeDrawer(mDrawerList);
      navigation_drawer_class.position = position; //Setting currently selected position in this field so that it will be available in our child activities.



      switch (position) {
      case 0:
       startActivity(new Intent(this, Folders.class));

       break;

      //case 2:
      case 1:

       Intent i=new Intent(navigation_drawer_class.this,Queues.class);
       i.putExtra("set_queue","set");  
       startActivity(i);    
       break;

      case 2:
      //case 3:
       Intent inbox=new Intent(navigation_drawer_class.this,Tickets.class);
       inbox.putExtra("filter_id","&vis_filter_id=1");          
       inbox.putExtra("title","Inbox");
       inbox.putExtra("set_queue","no");
       startActivity(inbox);
       break;

      case 3:
      //case 4:
       startActivity(new Intent(this, New_Ticket_step1.class));
       break;

      case 4:
      //case 5:
       startActivity(new Intent(this, Search.class));
       break;

      case 5:
      //case 6:
       startActivity(new Intent(this, Client.class));
       break;

      case 6:
      //case 7:
           startActivity(new Intent(this, Settings.class));
           break;

      default:


       break;
      }   

/*
      switch (position) {
      case 0:
           break;
      case 1:
       startActivity(new Intent(this, Folders.class));
       break;

      case 2:
       Intent i=new Intent(navigation_drawer_class.this,Queues.class);
       i.putExtra("set_queue","set");  
       startActivity(i);    
       break;

      case 3:
       Intent inbox=new Intent(navigation_drawer_class.this,Tickets.class);
       inbox.putExtra("filter_id","&vis_filter_id=1");          
       inbox.putExtra("title","Inbox");
       inbox.putExtra("set_queue","no");
       startActivity(inbox);
       break;

      case 4:
       startActivity(new Intent(this, New_Ticket_step1.class));
       break;

      case 5:
       startActivity(new Intent(this, Search.class));
       break;

      case 6:
       startActivity(new Intent(this, Client.class));
       break;

      case 7:
           startActivity(new Intent(this, Settings.class));
           break;

      default:
       break;
      } 
    */
      //Toast.makeText(this, "Selected Item Position::"+position, Toast.LENGTH_LONG).show();
     }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) 
     {
      //  getMenuInflater().inflate(R.menu.main, menu);
      return super.onCreateOptionsMenu(menu);
     }

     @Override
     public boolean onOptionsItemSelected(MenuItem item)
     {
         // The action bar home/up action should open or close the drawer.
          // ActionBarDrawerToggle will take care of this.
          if (actionBarDrawerToggle.onOptionsItemSelected(item)) 
          {
                    return true;
          }
          switch (item.getItemId())
          {
                default:
                return super.onOptionsItemSelected(item);
          }
     }

        @Override
        public boolean onPrepareOptionsMenu(Menu menu)
       {

            //boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
            //menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
            return super.onPrepareOptionsMenu(menu);
        }

        /* We can override onBackPressed method to toggle navigation drawer*/
     @Override
     public void onBackPressed() 
     {
      if(mDrawerLayout.isDrawerOpen(mDrawerList))
      {
          mDrawerLayout.closeDrawer(mDrawerList);
      }
      else 
      {
          mDrawerLayout.openDrawer(mDrawerList);
      }
     }

    //-----------------------------------------------------------
        private class getname extends AsyncTask<Void, Void, JSONArray>
        {

            Dialog dialog;
            @Override
            public void onPreExecute() 
            {
                dialog = new Dialog(navigation_drawer_class.this,android.R.style.Theme_Translucent_NoTitleBar);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.progressbar);
                dialog.show();
            }

            @Override
            protected JSONArray doInBackground(Void... params) 
            {

                String STAFF_URL=op.getUrl(getApplicationContext(),"staff","get_staff_details","");
                staff_data_array = JSONfunctions.getJSONfromURL(STAFF_URL+"&vis_encode=json",navigation_drawer_class.this);
                return staff_data_array;

            }
            @Override
            public void onPostExecute(JSONArray staff_data_array) 
            {   
                super.onPostExecute(staff_data_array);
                String staff_data_result =staff_data_array.toString();

              try {
                post_obj = staff_data_array.getJSONObject(0);
                String fname=post_obj.getString(FIRST_NAME);
                String lname=post_obj.getString(LAST_NAME);
                //Fullname =fname+" "+lname;
                Fullname =fname;

                if(Fullname=="")
                {
                    Fullname="Admin";
                }

                Fullname="Welcome "+Fullname;


                View header = (View)getLayoutInflater().inflate(R.layout.headerview,null);
                TextView headerValue = (TextView) header.findViewById(R.id.headerview_id);
                headerValue.setText(Fullname);
                mDrawerList.addHeaderView(header, null, false);//addHeaderView(header, null, false) */



            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                dialog.dismiss();
            }
        }


        //-----------------------------------------------------------

}

0 个答案:

没有答案