ListView not displaying -- ListView Item Selection into webView - java

I am trying to make a ListView that lists all League of Legends Champions.
My current problem is that right now just the listview itself will not show on the screen and cannot figure out why.
Here is my MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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;
}
}
Here is my champListAcivity:
public class champListActivity extends ListActivity {
#Override
public void onCreate(Bundle fdsa) {
super.onCreate(fdsa);
ArrayAdapter<Champ> aa;
ArrayList<Champ> champList = new ArrayList<Champ>();
ListView listView = (ListView) findViewById(R.id.ChampListView);
// I SKIPPED THE CODE CONTAINING THE ADDING OF THE CHAMPIONS
// TO THE LIST BECAUSE ITS TOO LONG AND ALREADY RIGHT BECAUSE IT
// WORKED BEFORE AND I HAVENT CHANGED IT
int layoutID = android.R.layout.simple_list_item_1;
aa = new ArrayAdapter<Champ>(this, layoutID, champList);
listView.setAdapter(aa);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l,v,position,id);
Champ selChamp = (Champ) getListAdapter().getItem(position);
String champURL = selChamp.getLink();
Intent intent = new Intent(this,webViewActivity.class);
intent.putExtra("url", champURL);
startActivity(intent);
}

You're not setting your content view in the champListActivity:
public class champListActivity extends ListActivity {
#Override
public void onCreate(Bundle fdsa) {
super.onCreate(fdsa);
setContentView(R.layout.activity_your_champ_list_name); // <- missing!

Related

filling listView when button clicked in other activity

Is there a way how you can add a string to an arraylist, which then fills a listview when a button is clicked in another activity? To be more clear: In activity A, I have a Listview and a menu item where I can go to Activty B. In Activtiy B I have got an EditText and a button. I want to achieve that everytime I click on the Button in activty B, the new String gets listed in the Listview. As a "clicklistener" I used a boolean. Is there a better way? Because with the code below I can only make one list entry which gets changed by every next button click.
Java Code Activity A:`package com.example.schoolapp;
public class MainActivity extends AppCompatActivity {
TextView tvSubjects;
ListView listView;
static ArrayList<String> arrayList = new ArrayList<>();
static ArrayAdapter<String> arrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvSubjects = findViewById(R.id.tv_subjects);
listView = findViewById(R.id.listView);
arrayList = new ArrayList<>();
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
arrayList);
arrayAdapter.notifyDataSetChanged();
listView.setAdapter(arrayAdapter);
if (NewNoteActivity.buttonClicked){
Intent intent = getIntent();
String string = intent.getStringExtra("Subject");
arrayList.add(string);
arrayAdapter.notifyDataSetChanged();
}
}
//Code for the menu, works fine
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.main_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.item_newSubject:
Intent intent = new Intent(this, NewNoteActivity.class);
startActivity(intent);
return true;
default:
return false;
}
}
}`
Java Code for Activity B:
public class NewNoteActivity extends AppCompatActivity {
EditText et_subject;
Button btn_createSubject;
String subject;
static boolean buttonClicked = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_note);
et_subject = findViewById(R.id.et_subject);
btn_createSubject = findViewById(R.id.btn_createSubject);
btn_createSubject.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buttonClicked = true;
subject = et_subject.getText().toString();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("Subject", subject);
startActivity(intent);
}
});
buttonClicked = false;
}
}

Can't get selected spinner item outside #Override method and into another class

I have MainActivity and SpinnerActivity, I want to get selected spinner item value from SpinnerActivity into the MainActivity. I tried to declare String as Static, I also tried with getter but without any success.
in Spinner Activity I also have if statement, how can I get value outside "if statement" and "#Override method" into another class?
MainActivity.class
public class MainActivity extends AppCompatActivity {
SpinnerActivity spinnerActivity = new SpinnerActivity();
Spinner spinnerProvince;
String selectedSpinnerProvince = spinnerActivity.inSpinnerSelectedProvince;
// String selectedSpinnerProvince = SpinnerActivity.inSpinnerSelectedProvince;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerProvince = findViewById(R.id.spinnerProvince);
populateSpinnerProvinces();
spinnerProvince.setOnItemSelectedListener(spinnerActivity);
}
public void populateSpinnerProvinces() {
ArrayAdapter<String> provincesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.province));
provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProvince.setAdapter(provincesAdapter);
}
}
SpinnerActivity.class
public class SpinnerActivity implements android.widget.AdapterView.OnItemSelectedListener {
public String inSpinnerSelectedProvince;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spinnerProvince) {
inSpinnerSelectedProvince = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
} else {// code here}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I were about to advice you to use Intent to share data between both activities when i noticed that your "SpinnerActivity" is not really an Activity since it not extends Android Activity class (AppCompactActivity or other classes like this).
Your SpinnerActivity is a Listener. You can use it to implement the action to trigger when an action is performed on your Spinner view. For that you need to do it inside the "#Override" method of onItemSelected.
If you don't like to use the previous methode because of the Override methode you should implement directly the action to trigger at on click events on your spinner view in your MainActivity by doing this:
public class MainActivity extends AppCompatActivity {
SpinnerActivity spinnerActivity = new SpinnerActivity();
Spinner spinnerProvince;
String selectedSpinnerProvince = spinnerActivity.inSpinnerSelectedProvince;
// String selectedSpinnerProvince =
SpinnerActivity.inSpinnerSelectedProvince;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerProvince = findViewById(R.id.spinnerProvince);
populateSpinnerProvinces();
//spinnerProvince.setOnItemSelectedListener(spinnerActivity);
spinnerProvince.setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spinnerProvince) {
inSpinnerSelectedProvince = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
} else {// code here}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void populateSpinnerProvinces() {
ArrayAdapter<String> provincesAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.province));
provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProvince.setAdapter(provincesAdapter);
}
}

OnCreate method is not called (Sunshine app)

I'm working on the android self study Sunshine app and I'm having trouble getting extra menu entries in the action bar.
The most likely reason I've found, is that the OnCreate method of my frame class is not being called and thus the menu is not being inflated.
Here's my MainActivity code:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MainActivity","OnCreate Started 1");
if (savedInstanceState == null) {
Log.d("MainActivity","OnCreate Started 2");
ForecastFragment MyFragment = new ForecastFragment();
if (MyFragment == null){
Log.d("MainActivity","OnCreate Started 3");
}
else{
getSupportFragmentManager().beginTransaction()
.add(R.id.container, MyFragment)
.commit();}
}
}
Here is now the code of my ForecastFragment class:
public class ForecastFragment extends Fragment {
public ForecastFragment() {
}
//#Override
protected void OnCreate(Bundle savedInstanceState){
Log.d("Forecastfragment","OnCreate Started");
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.forecastfragment, menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refresh) {
FetchWeatherTask WeatherTask = new FetchWeatherTask();
WeatherTask.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
When I run the app, I don't see the logging that the OnCreate method of the ForecastFragment class is being called.
Any ideas?
This is not the exact inherited onCreate method of Fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate()");
}
it should be onCreate not OnCreate

How to Extend Two Classes in Android?

I need to extend Two classes from a Single Class.My class Wants to extend Both ListActivity & MainActivity.
I found a question similar to this.
But i don't know how to Implement this https://stackoverflow.com/a/5836735/2781359
Thanks for your Help.
The Class which has to be Extended is ConnectionEditActivity.
public class ConnectionEditActivity extends ListActivity implements OnClickListener
{
public static Connection connectionParam;
private Connection connection;
private Button save;
private EditText name;
private EditText password;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.connection = connectionParam;
this.save = (Button) this.findViewById(R.id.save);
this.save.setOnClickListener(this);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD)
{
// Don't need the Save button on newer devices
android.widget.LinearLayout.LayoutParams a = (LayoutParams) this.save.getLayoutParams();
a.height = 0;
this.save.setLayoutParams(a);
this.save.forceLayout();
}
this.name = (EditText) this.findViewById(R.id.name);
this.password = (EditText) this.findViewById(R.id.password);
}
#Override
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
// Inflate the menu items for use in the action bar
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.connection_edit_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item)
{
// Handle presses on the action bar items
switch (item.getItemId())
{
case R.id.action_save:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void onResume()
{
super.onResume();
this.name.setText(this.connection.getName());
this.password.setText(this.connection.getPassword());
}
protected void onPause()
{
super.onPause();
this.connection.setName(this.name.getText().toString());
this.connection.setPassword(this.password.getText().toString());
finish();
}
public void onClick(View v)
{
if (v == this.save)
{
this.finish();
}
}
}
Mainactivity
public abstract class MainActivity extends ActionBarActivity
{
protected ListView mDrawerList;
protected DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
String LOG_TAG = "Remote It";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
// getSupportActionBar().hide();
setContentView(R.layout.activity_main_drawer);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_initMenu();
mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
mDrawer.setDrawerListener(mDrawerToggle);
}
private void _initMenu()
{
NsMenuAdapter mAdapter = new NsMenuAdapter(this);
// Add Header
mAdapter.addHeader(R.string.ns_menu_main_header);
// Add first block
menuItems = getResources().getStringArray(R.array.ns_menu_items);
String[] menuItemsIcon = getResources().getStringArray(R.array.ns_menu_items_icon);
int res = 0;
for (String item : menuItems)
{
int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcon[res], "drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
// if (res==1) mItem.counter=12; //it is just an example...
// if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
// Second Block
mAdapter.addHeader(R.string.ns_menu_main_header2);
menuItems = getResources().getStringArray(R.array.ns_menu_itemss);
String[] menuItemsIcons = getResources().getStringArray(R.array.ns_menu_items_iconss);
int ress = 0;
for (String item : menuItems)
{
int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcons[ress], "drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
// if (res==1) mItem.counter=12; //it is just an example...
// if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
mDrawerList = (ListView) findViewById(R.id.drawer);
if (mDrawerList != null)
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
/*
* The action bar home/up should open or close the drawer.
* ActionBarDrawerToggle will take care of this.
*/
if (mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle
{
public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout)
{
super(mActivity, mDrawerLayout, R.drawable.ic_drawer, R.string.ns_menu_open, R.string.ns_menu_close);
}
#Override
public void onDrawerClosed(View view)
{
getSupportActionBar().setTitle(getString(R.string.ns_menu_close));
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView)
{
getSupportActionBar().setTitle(getString(R.string.ns_menu_open));
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
}
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
mDrawer.closeDrawer(mDrawerList);
switch (position)
{
case 1:
Intent a = new Intent(MainActivity.this, Home.class);
startActivity(a);
break;
case 2:
Intent ac = new Intent(MainActivity.this, ConnectionListActivity.class);
startActivity(ac);
break;
default:
}
}
EDIT
I need to Extend it.Because the MainActivity has the navigation drawer.Now ConnectionEditActivity
doesn't shows the navigationDrawer nor the ActionBar .But i need to show the ActionBar
Any Suggestions ??
In Java you can't extend multiple classes, and for a good reason. Take for example what you are trying to accomplish by extending MainActivity and ListActivity. In your new class, when you call:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
}
Which onCreate() are you overriding? The one from ListActivity, or the one from MainActivity?
What the link you posted is saying is that instead of inheriting from another object, you compose your new object of the one you are trying to use. For example:
public class NewClass extends OldClass1 {
private OldClass2 mOldClass2 = new OldClass2();
#Override
public methodFromOldClass1() {
}
public methodFromOldClass2() {
mOldClass2.methodFromOldClass2();
}
}
The problem with this approach is that the methods from MainActivity and ListActivity are still going to have the same name, which although you can work around, it will become a headache quickly.
So the problem is a result of how you designed your class hierarchy. You will need to think about what functions you need from MainActivity, and what functions from ListActivity and choose how to reimplement your objects.
My Suggestion, since ListActivity only makes it slightly easier to work with lists (not that much easier) you can just skip it and implement the code related to the list on your own, and that way you can just extend MainActivity
You need to start by identifying what parts of MainActivity you need to inherit from, and what do you need from ListActivity.
Then, you have various possibilities:
Trivially, not extending ListActivity. Extending ListActivity only provides you with utility methods to work with the ListView, but you can totally have a ListView in an Activity without it being a ListActivity.
Create a utility class that contains extracted methods you need from MainActivity and call these methods from both your new class and MainActivity.
Modify MainActivity so that it extends ListActivity. After all it does contain a ListView (you'd loose the ActionBar thing, though).

How I can fill a ListView in Android from a other Activity?

I want built a Android App and use Eclipse for this. It is my first App and I have read on http://developer.android.com how I can create a App and more.
To my Appliation:
I use three Activitys.
The First Activity ist the main. On the Second Activity can I input a string and have a Button. On my third Activity I have a ListView but its empty. If I click on the second Activity of the Button I want that the string will be send to the ListView on the Third Activity.
a other Question is how I can save Informations in a Android App. Can I use a database or what is the right way. I want save the listview and if I open the Activity from the Main Activity i want see the Listview with the Informations.
MyCode:
FirstActivity:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void createPlan(View view)
{
Intent intent = new Intent(this,CreateActivity.class);
startActivity(intent);
}
}
Second Activity:
public class CreateActivity extends Activity {
public final static String ListViewMessage = "de.linde.KSDILLPlan";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
FillSpinnerViews();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
public void FillSpinnerViews()
{
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.daysArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Spinner Spinner2 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.zeitArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner2.setAdapter(adapter2);
}
public void createPlan(View view)
{
String PlanName;
//String StundenZahl;
//String Wochentag;
//Boolean doppelstunde;
Intent intent = new Intent(this,OpenActivity.class);
EditText planName = (EditText)findViewById(R.id.editText1);
PlanName = planName.getText().toString();
intent.putExtra(ListViewMessage, PlanName);
startActivity(intent);
}
Third Activity: The ListView
public class OpenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_open2, menu);
return true;
}
}
You can pass the data from one activity to another using intent as follows,
In second activity in onclick of button,
Intent in=new Intent(currentclass.this,nextactivity.class);
in.putExtras("value_name",value);
startActivity(in);
finish();
In third activity,
If(getIntent().getExtras!=null)
{
String msg=getIntent().getStringExtra("value_name"); // value_name is the attribute given in second activity
}
You can store the data in the database or sharedpreference. it depends upon your needs.
If you want to store bulk data as large number of person details, you can store in database using SQLite or if you want to store small data like login details you can store in SharedPreferences.
Refer the link for Database and SharedPreferences as
SQLite
SharedPreferences
Use following code to get the string from intent's bundle.
public class OpenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open);
String text = getIntent().getStringExtra( CreateActivity.ListViewMessage );
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_open2, menu);
return true;
}
}
Refer this link for persistent storage question.

Categories

Resources