Hello everyone I'm working on my Android project where I created expandable list view with Men's and Women's sports. Under each of them I have a list of sports. Each sport after you click on it open new activity. Example: If you click on Men's sports then Baseball you will open new activity where you get listed all Baseball events from database. I'm on that point where I created my expandable list view and my activities for each sport, now I have to populate each sport from data base. My data base is sorted in array-list from 0 to 11. Here is how it looks my database sorted in array-list:
this Main (id=831962574768)
groupedFeeds ArrayList (id=831963150464)
array Object[12] (id=831963153088)
[0] GroupedFeed (id=831963152968)
category "Women's Golf" (id=831962986192)
feeds ArrayList (id=831963152992)
[1] GroupedFeed (id=831963153592)
category "Volleyball" (id=831962991720)
feeds ArrayList (id=831963153616)
[2] GroupedFeed (id=831963153744)
category "Men's Soccer" (id=831962996544)
feeds ArrayList (id=831963153768)
[3] GroupedFeed (id=831963153896)
category "Women's Soccer" (id=831963006320)
feeds ArrayList (id=831963153920)
[4] GroupedFeed (id=831963154864)
category "Men's Golf" (id=831963016488)
feeds ArrayList (id=831963154888)
[5] GroupedFeed (id=831963155072)
category "Men's Cross Country" (id=831963036816)
feeds ArrayList (id=831963155096)
[6] GroupedFeed (id=831963155224)
category "Women's Cross Country" (id=831963041984)
feeds ArrayList (id=831963155248)
[7] GroupedFeed (id=831963155472)
category "Men's Bowling" (id=831963093056)
feeds ArrayList (id=831963155496)
[8] GroupedFeed (id=831963155712)
category "Women's Bowling" (id=831963098224)
feeds ArrayList (id=831963155736)
[9] GroupedFeed (id=831963155864)
category "Women's Basketball" (id=831963170720)
feeds ArrayList (id=831963155888)
[10] GroupedFeed (id=831963157504)
category "Men's Basketball" (id=831963299944)
feeds ArrayList (id=831963157528)
[11] null
modCount 11
size 11
loader RSSLoader (id=831962575480)
aMan AssetManager (id=831962469744)
Now I have to populate my Expandable List View for each Sport(Activity). Here is my Main method where I have to create function to populate my sports. I put the comments where that function suppose to be and how I think that should populate inside of the ecah sport. Please if someone can help I'm stuck and can not figure it out.
public class MainActivity<View> extends ActionBarActivity {
ExpandableListView exv;
List<GroupedFeed> gfList;
GroupedFeed gfResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exv=(ExpandableListView)findViewById(R.id.expandableListView1);
exv.setAdapter(new MyAdapter(this));
exv.setOnChildClickListener(new OnChildClickListener() {
**// Create GroupedFeed findFeed(String locateSport);
public boolean onChildClick(ExpandableListView parent,
android.view.View v, int groupPosition, int childPosition,
long id) {
switch (groupPosition)
{
case 0:
switch (childPosition)
{
case 0:
**// Call gfResult = findFeed("Men's Baseball");
Baseball(); **// pass gfResult
break;
case 1:
MensBasketball();
break;
case 2:
MensBowling();
break;
case 3:
MensCross_Country();
break;
case 4:
MensGolf();
break;
case 5:
MensSoccer();
break;
case 6:
MensTrack_Field();
break;
}
break;
case 1:
switch (childPosition)
{
case 0:
WomensBasketball();
break;
case 1:
WomensBowling();
break;
case 2:
WomensCross_Country();
break;
case 3:
WomensGolf();
break;
case 4:
WomensSoccer();
break;
case 5:
Softball();
break;
case 6:
WomensTrack_Field();
break;
case 7:
Volleyball();
break;
}
}
return false;
}
private void Baseball() {
Intent myIntent = new Intent(MainActivity.this, Baseball.class);
startActivity(myIntent);
}
private void MensBasketball() {
Intent myIntent = new Intent(MainActivity.this, MensBasketball.class);
startActivity(myIntent);
}
private void WomensBasketball() {
Intent myIntent = new Intent(MainActivity.this, WomensBasketball.class);
startActivity(myIntent);
}
private void MensBowling() {
Intent myIntent = new Intent(MainActivity.this, MensBowling.class);
startActivity(myIntent);
}
private void WomensBowling() {
Intent myIntent = new Intent(MainActivity.this, WomensBowling.class);
startActivity(myIntent);
}
private void MensCross_Country() {
Intent myIntent = new Intent(MainActivity.this, MensCross_Country.class);
startActivity(myIntent);
}
private void WomensCross_Country() {
Intent myIntent = new Intent(MainActivity.this, WomensCross_Country.class);
startActivity(myIntent);
}
private void MensGolf() {
Intent myIntent = new Intent(MainActivity.this, MensGolf.class);
startActivity(myIntent);
}
private void WomensGolf() {
Intent myIntent = new Intent(MainActivity.this, WomensGolf.class);
startActivity(myIntent);
}
private void MensSoccer() {
Intent myIntent = new Intent(MainActivity.this, MensSoccer.class);
startActivity(myIntent);
}
private void WomensSoccer() {
Intent myIntent = new Intent(MainActivity.this, WomensSoccer.class);
startActivity(myIntent);
}
private void Softball() {
Intent myIntent = new Intent(MainActivity.this, Softball.class);
startActivity(myIntent);
}
private void MensTrack_Field() {
Intent myIntent = new Intent(MainActivity.this, MensTrack_Field.class);
startActivity(myIntent);
}
private void WomensTrack_Field() {
Intent myIntent = new Intent(MainActivity.this, WomensTrack_Field.class);
startActivity(myIntent);
}
private void Volleyball() {
Intent myIntent = new Intent(MainActivity.this, Volleyball.class);
startActivity(myIntent);
}
});
Main myMain = new Main();
try {
// AssetManager aMan = getAssets();
// #SuppressWarnings("unused")
this.gfList = myMain.loadRSS();
} catch (Exception e) {
System.out.println("Hosed");
}
}
}
The easiest way to do this would be to pass the data to the activity in the intent you're using to start the activity:
Intent intent = new Intent(getBaseContext(), YourActivity.class);
intent.putExtra("youe_data_key", yourData);
startActivity(intent);
Related
So here's my code:
public class MainActivity extends AppCompatActivity {
/* access modifiers changed from: protected */
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
}
public void onClick(View view) {
Intent intent;
switch (view.getId()) {
case R.id.button: // Text
intent = new Intent(this,"Faiz Ahmed \nLove music forever\nTCSS 450");
break;
case R.id.button2: // Image
intent = new Intent(this, ImageActivity.class);
break;
case R.id.button3: // Web
intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("http://developer.android.com"));
break;
case R.id.button4: // Toast
Toast.makeText(this, "Here's to a new quarter!", Toast.LENGTH_SHORT)
.show();
break;
case R.id.button5: // Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("I am a dialog").setTitle("Some Title").create().show();
}
intent = null;
if (intent != null) {
startActivity(intent);
}
}
}
All the import's are there I just for some reason none of the buttons are working. The first 2 (Text & Image) I know I need a lot more code, but the last 3 should work & they're not for some reason. Any know what I'm doing wrong & how to fix it?
Nothing of the buttons will work because in the switch-case statement you initialize the intent according to which button was pressed.
After that you set intent to null and lastly you start the activity if the intent is not null, but it always is (since you set it before to null).
Short story delete intent = null; statement.
// intent = null; delete this line
if (intent != null) {
startActivity(intent);
}
I'm working on a music sheet app, and have a bunch of songs in a ListView.
I'm using an onItemClick method, but the problem is, I don't know how to open an activity depending on what subitem is selected.
The array of songs is uta[], so I can find the specific String with uta[position], but how can I open a specific activity based off of the position that is picked by the user in the ListView?
You can make a switch/case statement on the String that you fetch with uta[position]
public void onItemClick(AdapterView parent, View v, int position, long id) {
String value = uta[position].getValue();
switch(value){
case "value1":
Intent intent = new Intent(this, activity1.class); startActivity(intent);
break;
case "value2":
Intent intent = new Intent(this, activity2.class); startActivity(intent);
break;
case "value3":
Intent intent = new Intent(this, activity3.class); startActivity(intent);
break;
}
}
Note: switch/case statement on Strings requires JDK 7, see the Oracle documentation.
Try in this manner in your onitemclick method
if (position == 0) {
Intent intent = new Intent(this, activity1.class);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, activity2.class);
startActivity(intent);
} else if (position == 2){
Intent intent = new Intent(this, activity3.class);
startActivity(intent);
}
Your ListView item-click listener provides the view that has been clicked:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Song song = (Song) getItemAtPosition(position);
if (song.getId() == SOME_ID)
startActivity(new Intent(this, SomeActivity.class));
// you can then create the logic for all your activities
}
});
Also, you may pass the song to one activity that would load itself based on the song data. For that, I recommend you read about the Parcelable interface and how to put Extras in an Intent.
Can you use:
listView1.setOnItemClickListener(new AdapterView.onItemClickListener() {
#Override
public void onItemClick(AdapterView adapter, View view, int position, long arg) {
switch (position) {
case 0:
startActivity(new Intent(this, first_activity.class));
break;
case 1:
startActivity(new Intent(this, second_activity.class));
break;
// and more...
}
});
Cheers!
I want to make the ImageView work as button, because I want it be able to click and go to another activity. Each imageView(button) should contain its own value. The problem is, I don't know how to pass the value in the imageView(button) to another activity. This is what I have tried so far:
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
String value = " ";
switch(view.getId())
{
case R.id.imageView2:
value = "5";
break;
case R.id.imageView6:
value = "10";
break;
case R.id.imageView3:
value = "30";
break;
case R.id.imageView02:
value = "50";
break;
case R.id.imageView06:
value = "100";
break;
default:
break;
}
if(view.getId()==R.id.imageView2){
//get the value from switch case and send to other activity
}
Try this way,hope this will help you to solve your problem.
public class ButtonClickHandler implements View.OnClickListener {
public void onClick(View view) {
switch (view.getId()) {
case R.id.imageView2:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "5");
startActivity(intent);
break;
case R.id.imageView6:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "10");
startActivity(intent);
break;
case R.id.imageView3:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "30");
startActivity(intent);
break;
case R.id.imageView02:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "50");
startActivity(intent);
break;
case R.id.imageView06:
Intent intent = new Intent(YourCurrentActivity.this, YourOtherActivity.class);
intent.putExtra("YourKeyName", "100");
startActivity(intent);
break;
default:
break;
}
}
};
String valueFromIntent = getIntent().getStringExtra("YourKeyName");
Intent intent = new Intent(YourSecondActivity.this, YourThirdActivity.class);
intent.putExtra("YourKeyName", valueFromIntent);
startActivity(intent);
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", value);
startActivity(i);
here
if(view.getId()==R.id.imageView2){
//here
}
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");
You can use Bundle to do the same in Android
//Create the intent
Intent i = new Intent(this, ActivityTwo.class);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
String getrec=textView.getText().toString();
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“imagebuttonValue”, getrec);
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
Now in your second activity retrieve your data from the bundle:
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
String imagebuttonValue = bundle.getString(“imagebuttonValue”);
Using Intent you can call another activity and even send data with the help of putExtra from one activity to another activity, simply check below piece of code for understanding:
Intent intent= new Intent(currentActivity.this,nextActivity.class);
intent.putExtra("Key",yourvalue);
startActivity(intent);
On next acitivty to retrieve data:
Intent intent = getIntent();
String yourvalue= intent.getExtras().getString("Key");
Pass data trough Intent to your next activity and get your data in that activity like this
Intent intent = new Intent(Activity.this,SecondActity.class);
intent.putExtra("key",value);
startActivity(intent);
Get like this :
Intent intent = getIntent();
String value = intent.getStringExtra("key");
u want to launch a new activity and pass the label value to that ?
if yes then just create an Intent and add the value to it , and use this intent to launch other acitivty.
for example if your value is "5" then :-
Intent intent = new Intent(context) ;
intent.putExtra("key","5");
(refer here)
startActivity(intent);
and at onCreate() method of newly launched activity :-
String value= getIntent.getStringExtra("key"); (Refer here )
Get the values from the Image view. Use Extras and send it to the other Activity.
Lets Say first Activity is X and Next Activity is Y :-
//Include this in your code in the first activity inside your if condition
if(view.getId()==R.id.imageView2){
Intent main= new Intent(X.this, Y.class);
main.putExtra("key", value);
X.this.startActivity(main);
}
At Y Activity onCreate
Intent intent = getIntent();
String value= intent.getStringExtra("key");
Hope it helps.
Thanks!
I have got ExpandableListView. It looks like this http://www.androidhive.info/wp-content/uploads/2013/07/android-expandable-listview.jpg . I just want to OnChildClick for example Despicable Me 2 go to the SecondActivity and send PutExtra string with "Despicable Me 2". I know how to use PutExtra and GetExtra method.
Can someone give me advice how to solve this problem? I tried to go with switches but it does not work it does not do anything(It just stay in ExpandableListView).
Here is MainActivity.java:
Public string movie;
expListView.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Intent intent0 = new Intent(getApplicationContext(), SecondActivity.class);
switch(groupPosition)
{
//Top250
case 0: switch(childPosition)
{
case 0: movie="The Conjuring";
intent0.putExtra("text1", movie);
startActivity(intent0);
break;
case 1: movie="Despicable Me 2";
intent0.putExtra("text1", movie);
startActivity(intent0);
break;
}
break;
//Now Showing
case 1: switch(childPosition)
{
case 0:
break;
}
break;
//Coming Soon
case 2:switch(childPosition)
{
case 0:
break;
}
break;
}
return false;
}
});
I downloaded this code from http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/ you can see all of xml and activities here except SecondActivity.
try to put the intent inside the switch case
like this
case 0: switch(childPosition)
{
case 0: Intent intent0 = new Intent(getApplicationContext(), SecondActivity.class);
movie="The Conjuring";
intent0.putExtra("text1", movie);
startActivity(intent0);
break;
or
if(childPosition ==1)
Intent intent0 = new Intent(getApplicationContext(), SecondActivity.class);
movie="The Conjuring";
intent0.putExtra("text1", movie);
startActivity(intent0);
I am trying to implement a side menu in my application it works fine for the most part, but the problem I have is that once I display de menu and try to scroll down the whole list turns white (background) and the text disappears.
http://i.imgur.com/6a6TgJJ.png
http://i.imgur.com/ykT7hCN.png
Above I attach two pictures showing the behavior of the menu, when I slide my finger from the left to the right the side menu shows, but if I scroll down in the menu, it becomes an empty white list, here is my code:
public class ControlApp extends SlidingActivity{
ArrayList<String> datos ;
ArrayAdapter<String> adaptador;
Intent intent;
private String[] mMenuLista;
// private MyCustomAdapter mAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notificaciones);
setBehindContentView(R.layout.activity_menu);
Parse.initialize(this, "BqCCNsbb14MPgeWz3rznxO4DamuXUbsgiTug8P8I", "9j7GSLWnV46fkPsNMwnMD2FormAiclKlGitfDq2b");
ParseAnalytics.trackAppOpened(getIntent());
getSlidingMenu().setBehindOffset(100);
// mAdapter = new MyCustomAdapter();
PushService.setDefaultPushCallback(this, Notificaciones.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
mMenuLista = getResources().getStringArray(R.array.lista_menu);
// for(int i=0; i<=9;i++){
// mAdapter.addItem(mMenuLista[i]);
// if(i == 9){
// mAdapter.addSeparatorItem(mMenuLista[i]);
// }
// }
ListView primario = (ListView) findViewById(R.id.left_drawer);
primario.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list, mMenuLista));
primario.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> pariente, View view, int posicion, long id) {
selectItem(posicion);
}
});
}
public void selectItem(int posicion){
switch (posicion) {
case 0:
intent = new Intent(ControlApp.this, Notificaciones.class);
startActivity(intent);
break;
case 1:
intent = new Intent(ControlApp.this, Calificaiones.class);
startActivity(intent);
break;
case 2:
intent = new Intent(ControlApp.this, Mensajes.class);
startActivity(intent);
break;
case 3:
intent = new Intent(ControlApp.this, Citas.class);
startActivity(intent);
break;
case 4:
intent = new Intent(ControlApp.this, Permisos.class);
startActivity(intent);
break;
case 5:
intent = new Intent(ControlApp.this, Eventos.class);
startActivity(intent);
break;
case 6:
intent = new Intent(ControlApp.this, Horarios.class);
startActivity(intent);
break;
case 7:
intent = new Intent(ControlApp.this, Circulares.class);
startActivity(intent);
break;
case 8:
intent = new Intent(ControlApp.this, ProgramaDeEstudios.class);
startActivity(intent);
break;
case 9:
intent = new Intent(ControlApp.this, Ajustes.class);
startActivity(intent);
break;
case 10:
ParseUser.logOut();
ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser == null){
// sesion cerrada correctamente
Toast toast = Toast.makeText(getApplicationContext(), "Sesión cerrada correctamente", Toast.LENGTH_SHORT);
toast.show();
Intent regis = new Intent (ControlApp.this, MainActivity.class);
startActivity(regis);
finish();
}else{
Toast toast = Toast.makeText(getApplicationContext(), "No se logro cerrar sesion, intentelo de nuevo", Toast.LENGTH_SHORT);
toast.show();
}
break;
default:
break;
}
}
#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;
}
}
I hope someone can help me find my mistakes so I can correct the error. thank you!!!
I'm guessing you are manually setting the background color of a ListView in your menu to black, while the main activity theme has a white background?
You need to set the android:cacheColorHint attribute on the ListView to match the list background color. The white is appearing due to rendering optimization by Android which uses that cacheColorHint value to quickly redraw the list while scrolling.
I read a good blog post detailing this issue once. I can't find it right now but will link to it if I do :)