Listview inside Navigational Drawer Not responding to item click android - java

I am using DrawerLayout which has Listview
When I click on the Listview item , it does not responds to the click ( not launching another activity )
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
final ActionBar actionBar=getActionBar();
actionBar.setIcon(null);
actionBar.setTitle("");
setContentView(R.layout.main_activity);
// Custom ActionBar initializations.
getActionBar().setCustomView(R.layout.action_bar_custom);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_HOME_AS_UP);
getActionBar().setDisplayUseLogoEnabled(true);
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setDisplayShowCustomEnabled(true);
//getActionBar().setIcon(R.drawable.ic_drawer);
// Remember me.
sharedPref=getSharedPreferences("myPref", MODE_PRIVATE);
editor=sharedPref.edit();
//Listview
mDrwawerList=(ListView)findViewById(R.id.list_slidermenu);
listDataHeader = new ArrayList<HashMap<String,String>>();
listDataHeader_temp = new ArrayList<String>();
navigational_adapter=new NavigationListAdapter(MainActivity.this, new ArrayList<HashMap<String,String>>());
mDrawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.nav_btn,0, 0){
public void onDrawerClosed(View drawerView){
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrwawerList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
TextView txtCategoryName=(TextView)view.findViewById(R.id.txt_category);
String category_name=txtCategoryName.getText().toString();
Log.e("category_name", "karjeevcategory_name "+category_name);
if (category_name.equals("Home")) {
}
else if (category_name.equals("Diamond of the Week")) {
}
else if (category_name.equals("About Us")) {
}
else if (category_name.equals("FAQ")) {
Intent intent=new Intent(MainActivity.this,FAQActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else if (category_name.equals("Press & Download")) {
}
else if (category_name.equals("Contact Us")) {
}
}
});
new PerformGetCategory().execute();
}

Replace your existing code with this,and create a Intent inside particular position case of listview, if FAQ item is at 5th position, create Intent inside case 4:
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
displayView(position);
}
});
And inside displayView(int position) keep this:
private void displayView(int position) {
switch (position) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
Intent intent=new Intent(MainActivity.this,FAQActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
break;
case 5:
break;
case 6:
break;
default:
break;
}
}
Hope this solves your problem.

I solved my problem from my end ...
There was layout issues , due to which the listview was not able to handle the click ..
I pasted my litsview code to bottom of the other layout code which was inside DrawerLayout

Related

Want to Update the existing listView in fragment by clicking menuitems

I am creating an app for finding nearby blood donors. In this app I am using three fragments for swipe navigation. Inn one fragment, the available donor list is shown, but I want to change the list from a menu list by on a clicking particular blood group The 1st list is but when I click the items it shows nothing.
Java code:
public class DonorsFragment extends Fragment {
SwipeMenuListView listView;
FirebaseListAdapter adapter;
TextView fullName,bloodGroup,gender,age,address;
Query query;
View rootView;
FirebaseListOptions<DonorAdapterClass> options;
public DonorsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView =inflater.inflate(R.layout.fragment_donors, container, false);
listView = rootView.findViewById(R.id.listview);
Toast.makeText(getActivity(),"oncreateview",Toast.LENGTH_SHORT).show();
query =FirebaseDatabase.getInstance().getReference().child("Users");
setQuery(query);
SwipeMenuCreator creator = new SwipeMenuCreator() {
#Override
public void create(SwipeMenu menu) {
// create "delete" item
SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity());
// set item background
deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
0x3F, 0x25)));
// set item width
deleteItem.setWidth(170);
// set a icon
deleteItem.setIcon(R.drawable.ic_phone);
// add to menu
menu.addMenuItem(deleteItem);
}
};
listView.setMenuCreator(creator);
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
// open
Toast.makeText(getActivity(),"Item Clicked",Toast.LENGTH_SHORT).show();
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
// Inflate the layout for this fragment
return rootView;
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.blood_group_menu,menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.a_pos: {
Toast.makeText(getActivity(),"A+",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_A_Pos").equalTo("yes");
setQuery(query);
break;
}
case R.id.a_neg: {
Toast.makeText(getActivity(),"A-",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_A_Neg").equalTo("yes");
setQuery(query);
break;
}
case R.id.b_pos: {
Toast.makeText(getActivity(),"B+",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_B_Pos").equalTo("yes");
setQuery(query);
break;
}
case R.id.b_neg: {
Toast.makeText(getActivity(),"B-",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_B_Neg").equalTo("yes");
setQuery(query);
break;
}
case R.id.ab_pos: {
Toast.makeText(getActivity(),"AB+",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_A_Pos").equalTo("yes");
setQuery(query);
break;
}
case R.id.ab_neg: {
Toast.makeText(getActivity(),"AB-",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users");
setQuery(query);
break;
}
case R.id.o_pos: {
Toast.makeText(getActivity(),"O+",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_O_Pos").equalTo("yes");
setQuery(query);
break;
}
case R.id.o_neg: {
Toast.makeText(getActivity(),"O-",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users").orderByChild("canGive_O_Neg").equalTo("yes");
setQuery(query);
break;
}
case R.id.list_all: {
Toast.makeText(getActivity(),"All",Toast.LENGTH_SHORT).show();
query=FirebaseDatabase.getInstance().getReference().child("Users");
setQuery(query);
break;
}
}
return super.onOptionsItemSelected(item);
}
public void setQuery(Query query){
listView = rootView.findViewById(R.id.listview);
options = new FirebaseListOptions.Builder<DonorAdapterClass>()
.setLayout(R.layout.donor_list)
.setQuery(query,DonorAdapterClass.class)
.build();
adapter = new FirebaseListAdapter(options) {
#Override
protected void populateView(#NonNull View v, #NonNull Object model, int position) {
DonorAdapterClass donors = (DonorAdapterClass) model;
if(donors.getIsDonor().toString().equals("yes")){
fullName = v.findViewById(R.id.fullName_list);
bloodGroup = v.findViewById(R.id.bloodGroup_list);
gender = v.findViewById(R.id.gender_lis);
address = v.findViewById(R.id.address_list);
age = v.findViewById(R.id.age_list);
fullName.setText(donors.getFullName().toString());
bloodGroup.setText(donors.getBloodGroup().toString());
gender.setText(donors.getGender().toString());
address.setText(donors.getAddress().toString());
age.setText(donors.getAge().toString());
}
}
};
listView.setAdapter(adapter);
}
}
all firebase query and adapter is set in setQuery() method..when it is called from onCreateView() it is working...but when it is called from switch case in onOptionsItemSelected it shows nothing and previous list view also hides
Thanks in Advance :)
This is my first answer in centuries so please bear with me. This is not an answer but just pointer to the path towards solution.
Put a break-point at the following points
onOptionsItemSelected
case R.id.a_pos
setQuery()
setQuery::listView inittialization statement
setQuery::populateView::donor initialization statement
Run the app. Attach the process with debugger. Now when you click "A+" from options, does it get a response from the server or does it throw an exception?

changing layout and activity in spinner

First of all, I am fairly new at this, so bear with me.
I am trying to make a spinner where if you select an item, the layout and the activity will change, but I cant even implement the simplest of spinners.
so far the only code I've got is the one below, but no matter what I put in it doesn't do anything, so I must just not understand it, please be very specific in your answers. thank you. the first answer got me some of the way, but the "spinner spin..... spin.setAdapter(aa)" part is not accepted under OnCreate
String[] generations = { "Gen2", "Gen3", "Gen4", "Gen5", "Gen6","Gen7" };
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,generations);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
Intent intent2 = new Intent(this, gen2.class);
startActivity(intent2);
break;
case 2:
Intent intent3 = new Intent(this, Gen3.class);
startActivity(intent3);
break;
case 3:
Intent intent4 = new Intent(this, gen4.class);
startActivity(intent4);
break;
case 4:
Intent intent5 = new Intent(this, gen5.class);
startActivity(intent5);
break;
case 5:
Intent intent6 = new Intent(this, MainActivity.class);
startActivity(intent6);
break;
case 6:
Intent intent7 = new Intent(this, gen7.class);
startActivity(intent7);
break;
}
}
Here is a example, hope it helps you understand it better:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
//Performing action onItemSelected and onNothing selected
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
switch (position){
// Check what position was selected
case 1:
//start activity depending on position
Intent intent = new Intent(this, ActivityOne.class);
startActivity(intent);
break;
case 2:
Intent intent = new Intent(this, ActivityTwo.class);
startActivity(intent);
break;
case 3:
Intent intent = new Intent(this, ActivityThree.class);
startActivity(intent);
break;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

How can i set absolute positions to items on a listView?

I created some Activities on my app,so i created a listView with the name of those activities , so i can click on them and open the activity that i want (with setOnClickListener method and switch.) and a i created a searhView, so i can filter them by its name (with setOnQueryTextListener).
Let's use 3 names as examples: Alfred,Beth,Bill
In the "normal" listView Alfred is 0, Beth is 1, Bill is 2. So i made this:
switch( position )
{
case 0: Intent newActivity = new Intent(telaPesquisa.this, alfred.class);
startActivity(newActivity);
break;
case 1: Intent newActivityy = new Intent(telaPesquisa.this, beth.class);
startActivity(newActivityy);
break;
case 2: Intent newActivity2 = new Intent(telaPesquisa.this, bill.class);
startActivity(newActivity2);
break;
When i search in the searchView for "B" just Beth and Bill appears, and that is right, but now Beth is case 0, and Bill is case 1, so it will not open the activity that i want.
How can i set absolute values to the itens? like Alfred is always 0, Beth always 1 and Bill awalys 2?
Here is my entire code.
public class telaPesquisa extends Activity {
ListView lv;
SearchView sv;
String[] teams={
"Alfred",
"Beth",
"Bill",
};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela_pesquisa);
lv=(ListView) findViewById(R.id.listView);
sv=(SearchView) findViewById(R.id.searchView);
adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,teams);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//nt firstPosition = lv.getFirstVisiblePosition()+3;
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) lv.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
switch( position )
{
case 0: Intent alfred = new Intent(telaPesquisa.this, alfred.class);
startActivity(alfred);
break;
case 1: Intent beth = new Intent(telaPesquisa.this, beth.class);
startActivity(beth);
break;
case 2: Intent bill = new Intent(telaPesquisa.this, bill.class);
startActivity(bill);
break;
}
});
sv.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String text) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onQueryTextChange(String text) {
adapter.getFilter().filter(text);
return false;
}
});
}
}
sorry for my english
Modify the listener as this:
v.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// ListView Clicked item value
String itemValue = (String) lv.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(), "Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG).show();
switch( itemValue )
{
case "alfred": Intent alfred = new Intent(telaPesquisa.this, alfred.class);
startActivity(alfred);
break;
case "beth": Intent beth = new Intent(telaPesquisa.this, beth.class);
startActivity(beth);
break;
case "bill": Intent bill = new Intent(telaPesquisa.this, bill.class);
startActivity(bill);
break;
}
});

How to pass data to navigation drawer item?

I am using a nav drawer with three items(Time sheet, claims, project) and I would like to pass ab to claims. In time sheet I have 3 activities, the ab is in the work_details activity.How can I do to achieve this?
TimeSheet has 3 activities (Information(extend to fragment),work force,work details)
Claims has 2 activities(claims(extend to fragment), cameraMain).
WorkDetails.java
Button btn1 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
builder.setTitle("Data Saved");
builder.setMessage("Are you sure you want to save?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
long ab = ts.insertTimeSheet(name, weather, date, status,b,i);
Bundle bundle =new Bundle();
bundle.putString("ab","ab");
Claims o=new Claims();
o.setArguments(bundle);
}
Claims.java
final Long fk=this.getArguments().getLong("ab");
Button button = (Button) claims.findViewById(R.id.button8);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), CameraMain.class);
Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
startActivity(intent);
}
});
return claims;
}
I refer from Send data from activity to fragment in android but the app crashed when I select claims item.
11-02 09:25:55.910 6116-6116/com.example.project.project
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.project.project, PID: 6116
java.lang.NullPointerException
at com.example.project.project.Claims.onCreateView(Claims.java:59)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1453)
I think the NullPointException is came from here
fk=data.getLong("ab");
Do I need to add the parameter in MainActivity?
MainActivity.java
Some part of code snippet in mainActivity
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new Information();
break;
case 1:
fragment=new Claims();
break;
case 2:
fragment=new Project();
case 3:
fragment=new Report();
case 4:
fragment=new ViewView();
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
EDITED
Button btn1 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
builder.setTitle("Data Saved");
builder.setMessage("Are you sure you want to save?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
long ab = ts.insertTimeSheet(name, weather, date, status,b,i);
Bundle bundle =new Bundle();
bundle.putLong("ab",ab);
Claims o=new Claims();
o.setArguments(bundle);
}
Claims.java
Bundle bundle=this.getArguments();
fk=bundle.getLong("ab");
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
AlertDialogRadio(a1);
}
});
MainActivity
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new Information();
break;
case 1:
fragment=new Claims();
break;
case 2:
fragment=new Project();
break;
case 3:
fragment=new Report();
break;
case 4:
fragment=new ViewView();
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
if(fragment instanceof Claims)
{
Bundle bundle=new Bundle();
bundle.putString("ab","ab");
fragment.setArguments(bundle);
fragment.setArguments(bundle);
}
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
}
MainActivity
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new Information();
break;
case 1:
fragment=new Claims();
Bundle bundle=new Bundle();
bundle.putLong("ab",WorkDetails.ab);
fragment.setArguments(bundle);
break;
case 2:
fragment=new Project();
break;
case 3:
fragment=new Report();
break;
case 4:
fragment=new ViewView();
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
}
WorkDetails
public static long ab=-1;//this is flied ,don't put in the method
Button btn1 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
AlertDialog.Builder builder = new AlertDialog.Builder(WorkDetailsTable.this);
builder.setTitle("Data Saved");
builder.setMessage("Are you sure you want to save?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int ii) {
ab = ts.insertTimeSheet(name, weather, date, status,b,i);
}
What you can just do is to place this data in your activity where fragment is placed. example is.
in your activity
long ab = 12.4;
in your fragment
long data = ((YourActivity)getActivity()).ab;
"OR"
make ab like
static long ab = value; // your Details activity
in your details activity and get the value of this
with details activity name like
long data = Details.ab //in your claims activity.
When you are instantiating your Claim Fragment , instantiate a bundle object and assign whatever data that you need to be passed in bundle and set it as argument to the fragment.
Bundle bundle = new Bundle();
bundle.putLong("ab",10L);
new Claims().setargument(bundle);
Then in your Claim fragment's onCreateView() you can access this bundle data as
Bundle bundle = getArguments();
long ab = bundle.getLong("ab");

Get view from fragment in activity

I'm trying to get view from Fragment dynamically added to Activity.
MainActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
myDrawerList.setOnItemClickListener(new DrawerItemClickListener());
handleIntent(getIntent());
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(
AdapterView<?> parent, View view, int position, long id
) {
// display view for selected nav drawer item
displayView(position);
}
}
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
String userToken = UserTokenStorageFactory.instance().getStorage().get();
switch (position) {
case 0:
fragment = new AllCategories();
break;
case 1:
fragment = new Subscription();
break;
case 2:
if( userToken != null && !userToken.equals( "" ) ){
Intent myIntent1 = new Intent(this, PrivateOffice.class);
this.startActivity(myIntent1);
break;
}
else {
Intent myIntent1 = new Intent(this, LogIn.class);
this.startActivity(myIntent1);
break;
}
case 3:
if( userToken != null && !userToken.equals( "" ) ){
Intent myIntent1 = new Intent(this, Create_action.class);
this.startActivity(myIntent1);
break;
}
else {
Intent myIntent1 = new Intent(this, LogIn.class);
this.startActivity(myIntent1);
break;
}
case 4:
AlertDialog dialog = onCreateDialog(DIALOG_CHOOSE_CATEGORY);
dialog.show();
break;
default:
break;
}
if (fragment != null) {
android.app.FragmentManager fragmentManager = getFragmentManager();
fTrans = fragmentManager.beginTransaction();
fTrans.replace(R.id.content_frame, fragment);
fTrans.commit();
// update selected item and title, then close the drawer
myDrawerList.setItemChecked(position, true);
myDrawerList.setSelection(position);
setTitle(viewsNames[position]);
myDrawerLayout.closeDrawer(mDrawer);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
I need to get view (LinearLayout listv) from layout of AllCategories.java. I tried using getView() with findViewById, but it returns NullPointerException. What shall I do?

Categories

Resources