So I have this class here:
package phil.droid.game;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
public class GameList extends GamesTrialActivity
{
private ListView lv1;
protected String Game_names[]={"God of War","FOS RO DAH", "dhwaud"};
private String Game_pics[]={"God of War","God of War II"};
private int pos;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gamelist);
lv1=(ListView)findViewById(R.id.thegamelist);
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , Game_names));
lv1.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
pos = position;
Intent i = new Intent(GameList.this, game_viewer.class);
startActivity(i);
}
});
}
}
And then this VclassV extends the one ^above^
package phil.droid.game;
import android.os.Bundle;
import android.widget.TextView;
public class game_viewer extends GameList
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.game_template);
TextView game_title = (TextView)findViewById(R.id.GameTitle);
game_title.setText("" + pos);
}
}
The problem is at the moment that the last bit recognizes "pos" as "0" no matter what option I click on. Can someone suggest a way to make it so pos is recognized as the number element that's clicked on in the previous class?
Make pos protected, not private.
What you're trying to do can't work: The newly launched activity will not share the same storage as the parent, even if they inherit. The only way it would be possible is if the value was static, but that's not a good idea either.
What you should do instead is to send the data as part of the intent before starting the activity, e.g.:
intent.putExtra("pos", position);
and then you can pull it out in the new activity with
getIntent().getIntExtra("pos", -1); // -1 is used as default value
Also, game_viewer should most likely be a separate activity, rather than inherit from GameList.
Related
In Android, I am trying to override the toString() of the ParseUser for use in my listview, using
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import java.util.List;
/**
* A simple {#link Fragment} subclass.
*/
public class ExampleFragment extends Fragment {
public ExampleFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_example, container, false);
final ListView listView = (ListView) rootView.findViewById(R.id.exampleListView);
ParseQuery query = User.getQuery();
query.findInBackground(new FindCallback<User>() {
public void done(List<User> objects, ParseException e) {
if (e == null) {
// The query was successful.
listView.setAdapter(new ArrayAdapter<User>(rootView.getContext(), android.R.layout.simple_list_item_1, objects));
for (User user : objects) {
Log.i("AppInfo", "Added " + user.toString());
}
} else {
// Something went wrong.
}
}
});
return rootView;
}
public class User extends ParseUser {
#Override
public String toString() {
Log.i("AppInfo", "Username: " + getUsername());
return getUsername();
}
}
}
Seems simple enough. I had this nested in my fragment class and it crashed with this
java.lang.ClassCastException: com.parse.ParseUser cannot be cast to com.mycompany.myproject.ExampleFragment$User
but when I move it into its own subclass (where it gets its own entry in the explorer view in android studio), it works perfectly.
Is there a reason it does not work nested in my fragment class? Seems a bit unnecessary to create its own subclass just to override one function.
UPDATE: If I remove the for loop part it does not error out, but it still does not call my overridden toString() as the listview returns com.parse.ParseUser#etcetcetc.
make sure you have do these 2 things
1.register class before Parse.initialize
ParseObject.registerSubclass(User.class);
2.use annotation to specified the class mapping to waht Class
#ParseClassName("_User")
public class User extends ParseUser {
...
}
here is the reference.
https://parse.com/docs/android/guide#objects-subclassing-parseobject
I am trying to create a list view in Java that allows each list item to open a web url address but I can not seem to get the code right. Please can some one show me where IO going wrong.
package com.sasquatchapps.hydraquip10.bestofmonsterquest;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class Season1Activity extends Activity{
private String episodes[] = {"America's Loch Ness Monster","Sasquatch Attack",
"Giant Squid Found","Birdzilla","Bigfoot","“Mutant K9","Lions in the Backyard","Gigantic Killer Fish","Swamp Beast","Russia's Killer Apemen","Unidentified Flying Creatures","The Real Hobbit",
"Giganto: The Real King Kong","American Werewolf"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayAdapter<String> adapter = new
ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, episodes);
setListAdapter(adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(this, "Item clicked;" + episodes[position], Toast.LENGTH_SHORT).show();
if (position == 0) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/watch?v=o7-RdxrCFAg"));
startActivity(intent);
}
}
}
You have some errors - Where is your listview? Modify your code
1) create listview -
ListView myList;
2) In your onCreate assign this list:
myList = (ListView) findViewById(R.id.my_list);
3) Set adapter to your list:
myList.setAdapter(adapter);
I tried to have a view of an ActionBarActivity opening after hitting a spinner button.
There are two items on my spinner, the second one runs fine, but when I tried to access the Categorias item the app throws me a NullPointerException inside the DrawerActivity class. I don't actually know where the problem is. Another ActionBarActivity extension class that I have runs perfectly.
I'm new to Android/Java development.
The Spinner Fragment
import inmostla.ligatangamanga.pruebaintegrar.navigationdrawer.NavigationDrawerActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerFragment extends Fragment {
private static Spinner spinner;
private int selected;
private View mView;
static void setSpinnerContent( View view ){
spinner = (Spinner) view.findViewById(R.id.spinner1);
return ;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_spinner, container, false);
// Now use the above view to populate the spinner.
setSpinnerContent( view );
/**
* Maneja las acciones seleccionadas del Spinner
*/
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
selected = spinner.getSelectedItemPosition();
switch(selected){
case 1:
Intent categorias = new Intent( );
categorias.setClass( getActivity() , NavigationDrawerActivity.class );
startActivity(categorias);
break;
case 2:
Intent convenios = new Intent();
convenios.setClass(getActivity(), ConveniosFragment.class);
startActivity(convenios);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return view;
}
}
The Navigation Drawer Activity extends a ActionBarActivity
package inmostla.ligatangamanga.pruebaintegrar.navigationdrawer;
import inmostla.ligatangamanga.pruebaintegrar.navigationdrawer.NavigationDrawerFragment;
import inmostla.ligatangamanga.pruebaintegrar.R;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class NavigationDrawerActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}...
Maybe that's because getActivity() returns null. Try overriding onAttach() and keep Activity reference as a field in your class. Use this reference instead of getActivity() when you needed a reference to context or activity.
I have two String[], one with names and the other one with phone number. If I select one name from list, how can I call phone number of that person from Dialog box. For example, first person in first string[] has a phone number of second[]. How to get that number?
package com.mkyong.android;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ListFruitActivity extends ListActivity {
static final String[] Imena = new String[] { "Aleksandar Panic", "Dubravka Protic", "Milutin Panic",
"Jelica Panic", "Nemanja Gagic", "Doris Dragojevic", "Milica Protic", "Kiki Beba",
"Dule Savic", "Miroslav Miskovic", "Petar Djuric", "Dragoslav Joksimovic", "Petar Petrovic" };
static final String[] Brojevi=new String[] { "0638638045", "062450050", "065045324", "05443",
"06535475", "564218", "546567", "514574","5454333","444787","413133","354867","24879"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,
Imena));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
AlertDialog.Builder adb=new AlertDialog.Builder(ListFruitActivity.this);
adb.setTitle("Odabir");
adb.setMessage("Izabrali ste profesora "+parent.getItemAtPosition(position));
adb.setPositiveButton("Call",new DialogInterface.OnClickListener() {
public void onClick (DialogInterface dialog, int which) {
Intent i=new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:+651234567")); //here is the problem, how to get the right number
startActivity(i);
}
});
adb.show();
}
});
}
}
Why do you need two String[] arrays?
You have two options to implement it in a better way:
Use a Map<String, String> to store name as key and phone number as value.
Create a class Contact to store all your contact details like name, address, phone number, email etc and then use a List<Contact> or an array (recommended way of doing this).
Hoping to get into android app development so I'm doing some basic tutorials just now.
Just trying to get comfortable with the basics at the moment, one of which is using the Typeface class.
package org.me.myandroidstuff;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class HelloWorldActivity extends Activity implements OnClickListener
{
private View mainView;
private TextView tbox1;
private Button exitButton;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
}
The line
tbox1 = (TextView)findViewById(R.id.textBox1);
Has a red cross beside it (I'm using eclipse) with the error
tbox1 cannot be resolved
Its been a while since i have used java, but as i aware the following code
create a new TextView object called tbox1
Assigns the tbox1 object the id specified in the xml for the TextView tag in an external main.xml
Then tbox1 executes the setTypeFace() method on itself?
Obviously I'm going wrong somewhere, any ideas? Something really simple no doubt...
You can't inform us about one error and neglect the others. Look at your code.
Besides what user370305 said, you have other problems. Namely, your Activity, according to the contract, implements OnClickListener but does not override the necessary onClick(View v) method. You must add it for the contract to be met.
So your code should look like:
package org.me.myandroidstuff;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class HelloWorldActivity extends Activity implements OnClickListener {
private View mainView;
private TextView tbox1;
private Button exitButton;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainView=(View)findViewById(R.id.mainView);
mainView.setBackgroundColor(getResources().getColor(R.color.silver));
tbox1 = (TextView)findViewById(R.id.textBox1);
tbox1.setTypeface(Typeface.MONOSPACE);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Remember, you can't talk about errors until you fix every other that might cause other errors to be falsely reported.
First try to set setContentView(R.layout.yourlayoutfilename); in onCreate().
1.) Delete line super.onCreate(savedInstanceState);
2.) Retype super.onCreate(savedInstanceState);
3.) Clean the Project
4.) Build the Project