数据无法正常显示

时间:2016-08-31 13:53:37

标签: android

我在android中开发了一个应用程序。在第一个屏幕上,它将询问用户的用户名和密码,如果它是正确的,它将进入主屏幕。在主屏幕上,它将显示事件的详细信息。

问题是,有时在主屏幕上显示数据,有时甚至不调用服务。我没有得到我做错事的地方。

以下是我的主页代码:

public class MainActivity extends ActionBarActivity {

int mPosition = -1;
String mTitle = "";
private List<UpcomingJobs> upcomingJobs = new ArrayList<>();
private RecyclerView recyclerView;
private UpcomingAdapter uAdapter;

// Array of strings storing country names
String[] mMenus ;

// Array of integers points to images stored in /res/drawable-ldpi/
int[] mOptions = new int[]{
        R.drawable.home,
        R.drawable.internships,
        R.drawable.events,
        R.drawable.logout,

};

// Array of strings to initial counts


private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private LinearLayout mDrawer ;
private List<HashMap<String,String>> mList ;
private SimpleAdapter mAdapter;
final private String MENU = "menu";
final private String OPTION = "option";


@Override
protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    uAdapter = new UpcomingAdapter(upcomingJobs);
    RecyclerView.LayoutManager mLayoutManager = new     LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    String url = "url";
    AQuery mAQuery = new AQuery(MainActivity.this);
    mAQuery.ajax(url, String.class, new AjaxCallback<String>() {
        @Override
        public void callback(String url, String data, AjaxStatus status) {
            super.callback(url, data, status);
            if (android.support.v7.appcompat.BuildConfig.DEBUG) {
                Log.d("###$Request URL", url + "");
                Log.d("###$Response ", data + "");
                Log.d("###$Status Message : ", status.getMessage() + "");
                Log.d("###$Status Code : ", status.getCode() + "");

            }
            if (null != data && status.getCode() != -101) {

                String StringData = "" + data;
                try {
                    JSONArray rootArray = new JSONArray(StringData);
                    int len = rootArray.length();
                    for (int i = 0; i < len; ++i) {
                        JSONObject json = rootArray.optJSONObject(i);

                        UpcomingJobs b1 = new UpcomingJobs();
                        b1.Name = json.optString("Name");
                        b1.Position_Name = json.optString("PositionName");
                        b1.EventDate = json.optString("EventDate");
                        b1.EvetnId = json.optInt("EvetnId");

                        upcomingJobs.add(b1);
                        recyclerView.setAdapter(uAdapter);
                    }
                } catch (JSONException e) {
                }
            }
        }
    });



    mMenus = getResources().getStringArray(R.array.menus);

    // Title of the activity
    mTitle = (String)getTitle();

    // Getting a reference to the drawer listview
    mDrawerList = (ListView) findViewById(R.id.drawer_list);

    // Getting a reference to the sidebar drawer ( Title + ListView )
    mDrawer = ( LinearLayout) findViewById(R.id.drawer);

    // Each row in the list stores country name, count and flag
    mList = new ArrayList<HashMap<String,String>>();
    getSupportActionBar().setTitle("UpComming Events");


    for(int i=0;i<4;i++){
        HashMap<String, String> hm = new HashMap<String,String>();
        hm.put(MENU, mMenus[i]);

        hm.put(OPTION, Integer.toString(mOptions[i]) );
        mList.add(hm);
    }

    // Keys used in Hashmap
    String[] from = { OPTION,MENU};

    // Ids of views in listview_layout
    int[] to = { R.id.option , R.id.menu};

    // Instantiating an adapter to store each items
    // R.layout.drawer_layout defines the layout of each item
    mAdapter = new SimpleAdapter(this, mList, R.layout.drawer_layout, from, to);

    // Getting reference to DrawerLayout
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);

    // Creating a ToggleButton for NavigationDrawer with drawer event listener
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer , R.string.drawer_open,R.string.drawer_close){

        /** Called when drawer is closed */
        public void onDrawerClosed(View view) {
            highlightSelectedCountry();
            supportInvalidateOptionsMenu();
        }

        /** Called when a drawer is opened */
        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle("Menu");
            supportInvalidateOptionsMenu();
        }
    };

    // Setting event listener for the drawer
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    // ItemClick event handler for the drawer items
    mDrawerList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            if(position==1){
                Intent intent=new Intent(MainActivity.this,About_us.class);
                startActivity(intent);

            }
            else if(position==3)
            {
                session.logoutUser();
            }

            // Increment hit count of the drawer list item


            if(position < 2) { // Show fragment for countries : 0 to 4
                showFragment(position);
            }else{ // Show message box for countries : 5 to 9
                Toast.makeText(getApplicationContext(), mMenus[position], Toast.LENGTH_LONG).show();
            }

            // Closing the drawer
            mDrawerLayout.closeDrawer(mDrawer);
        }
    });


    // Enabling Up navigation
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    getSupportActionBar().setDisplayShowHomeEnabled(true);

    // Setting the adapter to the listView
    mDrawerList.setAdapter(mAdapter);

}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


public void showFragment(int position){

    //Currently selected country
    mTitle = mMenus[position];

    // Creating a fragment object
    CountryFragment cFragment = new CountryFragment();

    // Creating a Bundle object
    Bundle data = new Bundle();

    // Setting the index of the currently selected item of mDrawerList
    data.putInt("position", position);

    // Setting the position to the fragment
    cFragment.setArguments(data);

    // Getting reference to the FragmentManager
    FragmentManager fragmentManager  = getSupportFragmentManager();

    // Creating a fragment transaction
    FragmentTransaction ft = fragmentManager.beginTransaction();

    // Adding a fragment to the fragment transaction
    ft.replace(R.id.content_frame, cFragment);

    // Committing the transaction
    ft.commit();
}

// Highlight the selected country : 0 to 4
public void highlightSelectedCountry(){
    int selectedItem = mDrawerList.getCheckedItemPosition();

    if(selectedItem > 3)
        mDrawerList.setItemChecked(mPosition, true);
    else
        mPosition = selectedItem;

    if(mPosition!=-1)
        getSupportActionBar().setTitle(mMenus[mPosition]);
}

  }

0 个答案:

没有答案