Recycler view onClick produces another Recycler view - java

I am trying to start a new Recycler view upon clicking a container view from another recycler view. Essentially I have list of NBA teams presented as a recycler view in "NBA_Adapter"; when each team is clicked it will bring the user to another recycler view showing the players in the team. I am trying to do this using two classes that extend recycler view. The issue is that I am unable to start the second class, which I suspect is because the second class does not extend "AppCompatActivity" and instead extends RecyclerView, which the manifest file is asking me to do since it does not recognize my second class.
Manifest below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nba">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Second_Adapter">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
Main Activity below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//instantiates the parameters
recyclerView = findViewById(R.id.recycler_view);
adapter = new NBA_Adapter(getApplicationContext());
layoutManager = new LinearLayoutManager(this);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
}
}
Part of 1st Class below:
#Override
public void onClick(View v) {
TEAMS current = (TEAMS) containerView.getTag();
Intent intent = new Intent(v.getContext(), Second_Adapter.class);
intent.putExtra("id", current.getId());
v.getContext().startActivity(intent);
}
My second class is essentially the same as my first class. The only reason I do not use the same recycler view is that I am having a hard time implementing it as I am also using a search view for each recycler view. So at the moment I am separating them. Any hints or links are appreciated showing how I can do this is appreciated.

Related

Insert NewChidldActivity in-Between MainActivity and FirstChildAcvitity And Change Default ChildActivity to the NewChildActivity

Fellow Friends,
thank you all for your usual support.
I have developed an an examination app with login functionality.
Previously, the app had 3 Activities (MainActivity, WelcomeScreenActivity and QuestionsAcvitity). This app is working perfectly.
However, when the user logs in from the MainActivity, the WelcomeScreenActivity shows.
But I need to INSERT another activity (PROFILE_ACTIVITY) between the MainActivity and the WelcomeScreenActivity so that, when the user logs in successfully, the app will display the user's PROFILEACTIVITY.
Pictures below the page
Previous Code before the new ProfileActivity
MainActivity.java code
#Override
protected void onPostExecute(Void result){
if (!responseData.equals( "User Not Found" )) {
message = "Welcome";
data = responseData;//store the user id from the server in data
//Bundle bundle = new Bundle();//bundle the message and parse it to the next activity
//bundle.putString("dispMsg", message);//bundle the message using the variable dispMsg
//intent.putExtras(bundle);
startActivity(intent);
statusBar.setText(message);
} else {
message = responseData;//"Incorrect Username or Password. Try again!";
statusBar.setText(message);
}
super.onPostExecute(result);
}
DisplayWelcomeScreenActivity.java code
This activity displays after a successful login.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_welcome_screen);
// Get the Intent that started this activity and extract the string
intent = getIntent();
}
To pass from this WelcomeActivity to the next QuestionsActivity, the code below does that on buttonclick() event
public void courseClicked(View v) {
String course = "";
Intent intent = new Intent(this, CSS_342_Questions.class);
int qNum = 0;
switch (v.getId()){
case R.id.buttonCSS342:
course = "CSS 342";
qNum=1;
break;
case R.id.buttonCSS352:
course = "CSS 352";
qNum=1;
break;
case R.id.buttonCSS354:
course = "CSS 354";
qNum=1;
break;
case R.id.buttonCSS356:
course = "CSS 356";
qNum=1;
break;
case R.id.buttonCSS381:
course = "CSS 381";
qNum=1;
break;
case R.id.buttonPCR362:
course = "PCR 362";
qNum=1;
break;
}
Bundle bundle = new Bundle();
bundle.putString("dispCode", course);
bundle.putInt("qNum", qNum);
//bundle.putString("dispMsg", "Welcome");
intent.putExtras(bundle);
startActivity(intent);
}
The QuestionsActivity gets displayed using the code below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_css_342__questions);
// Get the Intent that started this activity and extract the string
intent = getIntent();
bundle = getIntent().getExtras();
showCode = bundle.getString("dispCode");
qNum = bundle.getInt("qNum");
}
This code below is the Manifest xml that links the 3 activities together for proper navigation.
AndroidManifest.XML code
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.examinationportal">
<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.DayNight.DarkActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayWelcomeScreen"
android:label="#string/welcome_screen_title"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".CSS_342_Questions"
android:allowTaskReparenting="true"
android:label="#string/question_screen"
android:parentActivityName=".DisplayWelcomeScreen">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".DisplayWelcomeScreen" />
</activity>
<activity
android:name=".RegisterUserActivity"
android:label="#string/new_user_form"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
Now, I want to insert a new activity (ProfileActivity) between the MainActivity and the DisplayScreenActivity.
I have changed the AndroidManifest xml code below.
The app is crashing. The LogCat shows the line where the app is crashing **StartActivity(intent)** in MainActivity.java; line 153
The Code After the new Activity
The MainActivity code still remains the same.
The new AndroidManifest.xml code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.examinationportal">
<uses-permission
android:name="android.permission.INTERNET"
android:required="true" />
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.AppCompat.DayNight.DarkActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ProfileAcvitity"
android:label="#string/student_profile"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".RegisterUserActivity"
android:label="#string/new_user_form"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
New ProfileActivity.java code
public class ProfileAcvitity extends AppCompatActivity {
public Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_acvitity);
intent = getIntent();
}
LOGCAT INFORMATION
04-25 13:53:33.680 14789-14789/com.example.examinationportal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.examinationportal, PID: 14789
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.examinationportal/com.example.examinationportal.DisplayWelcomeScreen}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1523)
at android.app.Activity.startActivityForResult(Activity.java:4034)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:3986)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:4325)
at android.app.Activity.startActivity(Activity.java:4293)
at com.example.examinationportal.MainActivity$GetText.onPostExecute(MainActivity.java:153)
at com.example.examinationportal.MainActivity$GetText.onPostExecute(MainActivity.java:83)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5763)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
Please help me solve this problem.
Thank you.
<activity android:name=".DisplayWelcomeScreen"
android:label="#string/student_welcome"
android:parentActivityName=".MainActivity">
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
Adding those lines in your AndroidManifest.xml file will solve your problem.

Android Activity action bar label not being set properly?

I have an app I'm designing where I have an activity with several buttons in it. This activity uses "setTitle()" to set the action bar title to the name of the user, where the default was set to a blank string in the AndroidManifest via android:label. One of the buttons leads to another activity that has a tab layout with fragments with different pieces of data. I use android:label in the manifest to set the title of this tab layout activity to a raw string. However, when entering this activity, it shows the user's name which was in the title of the activity that launched it. Why is this? Is there something wrong with how I'm defining the label of the activities? Here is my AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.uml.android.adventurersarchive">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Preferences"
android:parentActivityName=".MainActivity"
android:theme="#android:style/Theme.Holo" />
<activity android:name=".CreateCharacterActivity"
android:parentActivityName=".MainActivity"
android:label="Create a Character" />
<activity android:name=".CharacterMainActivity"
android:parentActivityName=".MainActivity"
android:label="" />
<activity android:name=".CharacterSheetActivity"
android:parentActivityName=".CharacterMainActivity"
android:label="" />
<activity android:name=".CharacterEquipmentActivity"
android:parentActivityName=".CharacterMainActivity"
android:label="Equipment" />
<activity android:name=".CharacterSpellbookActivity"
android:parentActivityName=".CharacterMainActivity"
android:label="Spellbook" />
</application>
</manifest>
The activity in question which is displaying the wrong title is "CharacterSpellbookActivity". The one that sets the title to the user name is "CharacterMainActivity".
Here is the onCreate() method of CharacterMainActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_main);
Intent intent = getIntent();
myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
setTitle(myCharacter.getCharacterName());
}
Here is where I launch the CharacterSpellbookActivity:
public void openSpellbook(View v) {
Intent intent = new Intent(this, CharacterSpellbookActivity.class);
intent.putExtra("character", myCharacter);
startActivity(intent);
}
And here is the onCreate() method of the CharacterSpellbookActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_spellbook);
Intent intent = getIntent();
myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
setTitle(myCharacter.getCharacterName());
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
SpellbookTabAdapter adapter = new SpellbookTabAdapter(myCharacter, getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
Did I do something wrong here?
However, when entering this activity, it shows the user's name which was in the title of the activity that launched it. Why is this?
Because that's what you told it to do
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_character_spellbook);
Intent intent = getIntent();
myCharacter = (CharacterInfo) intent.getParcelableExtra("character");
setTitle(myCharacter.getCharacterName());
Empty labels automatically picks up their value if they are null from their parent Activity.
Lets solve this :
remove the label attribute from manifest
If you want to change only the title why don't you ease down on your code and try this:
1
getActionBar().setTitle(R.string.your_title);
After it, you can call:
2
getActionBar().setDisplayShowTitleEnabled(true);

Android Studio - Change default application layout load

I have a android studio project,
and I have got few layouts which are connected to their .java files
note "I couldn't post images as of my low reputation"
I have got:
layout1.xml
layout2.xml
layout3.xml
layout4.xml
layout5.xml
and
Layout1.java
Layout2.java
Layout3.java
Layout4.java
Layout5.java
Android Manifest is:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#drawable/blablabla"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:screenOrientation="portrait"
android:name="com.icetea09.demomaterialdesigndrawermenu.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
layout1.xml code is this: "activity_main.xml is my layout1"
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mLvDrawerMenu = (ListView) findViewById(R.id.lv_drawer_menu);
List<DrawerMenuItem> menuItems = generateDrawerMenuItems();
mDrawerMenuAdapter = new DrawerMenuItemAdapter(getApplicationContext(), menuItems);
mLvDrawerMenu.setAdapter(mDrawerMenuAdapter);
mLvDrawerMenu.setOnItemClickListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if(savedInstanceState == null){
setFragment(0, BikeFragment.class);
}
}
The default layout load view is "layout1.xml with Layout1.java"
The question is how do I change the start view layout from layout1 to layout2.
Like when I install the app first, It will show the "layout2.xml" view instead of default one which is layout1.xml in my case.
Thank you for your time.
One way to do it, is when you add a new activity. You can click on "Launcher Activity". Then Android Studio will make this new activity the Launcher Activity.
When you already have added both activities there is another way.
The best way is to use the
AndroidManifest.xml
file. To make an activity seen as a launcher activity you add the following attributes to your activity in the manifest:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This question may be a duplicate: change application's starting activity - Android

Getting null on object reference for Up navigation

This seems to work but not sure if I have to add it to every Activity?
android:theme="#style/Theme.AppCompat"
along with extending the Classes with ActionBarActivity
Here is what I did in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.shmira.shmira.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Book"
android:theme="#style/Theme.AppCompat"
android:label="Shmira" >
</activity>
The class looking like this:
public class Book extends ActionBarActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar actions click
switch (item.getItemId()) {
// lets user travel back to where they came from
case android.R.id.home:
finish();
return true;
If you're using a support lib i.e. ActionBarActivity add the following line after calling setContentView:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and if you're not using a support lib i.e. Activity, then simply change getSupportActionBar() to getActionBar().. and don't forget to add android:theme="#style/Theme.AppCompat" to AndroidManifest.xml
To go back to the MainActivity instead of up through the stack, define ActivityA as parent for ActivityB in AndroidManifest.xml like this:
<activity
android:name=".Book"
android:label="Shmira">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.shmira.shmira.MainActivity" />
</activity>
Try adding ParentActivity of your Activity in AcivityTag in AndroidManifest.xml
<activity
android:name=".Book"
android:label="#string/book"
android:parentActivityName="ParentActivity">
Try Adding below code in your oncreate
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In your code your using getActionBar() while base class is ActionBarActivity. You should use above code instead.

Intent To Start Activity Not Working

I am trying to start an android activity name 'TheGame' from the activity 'SelectionFragment' with a button who's ID is 'startGame'
The SelectionFragment file's public class is :
public class SelectionFragment extends Fragment {
This is the code that contains the Intent:
public void startTheGame (View view) {
Intent intent = new Intent(this, TheGame.class);
startActivity(intent);
}
And this is what my Manifest file looks like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alexlamond.higherorlower"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/highlowlogo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.alexlamond.higherorlower.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/app_id" />
<activity android:name="com.facebook.LoginActivity" >
</activity>
<activity
android:name="com.alexlamond.higherorlower.TheGame"
android:label="#string/title_activity_the_game" >
</activity>
</application>
My issue is that the Intent will not work, when I got to Eclipse's suggestion of how to fix it, it says:
The constructor Intent(SelectionFragment, Class) is undefined
Your onClick attribute must match by exact naming to a public method that has the same name. You have attribute android:onClick="TheGame" and the method signature is public void startTheGame (View view). Change the onClick attribute value to "startTheGame".
However, I would rather use View.OnClickListener instead of setting the onClick attribute because:
On the long run you may reuse/move/change this layout and you will end-up with the same error.
You might do some refactoring and you might change this method by accident or not, and the compiler will not complain.
Setting this type of linkage between UI and model seems to me a violation of MVC.
In case you are starting activity from your fragment, try this:
public void startTheGame (View view) {
Intent intent = new Intent(getActivity(), TheGame.class);
startActivity(intent);
}

Categories