Android Studio - Expression Expected - android

I am creating a weather app and I am getting a message that an Expression was expected.
Here is my Main Activity:
package com.haziqhussain.hazweather;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class WeatherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_weather);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new WeatherFragment())
.commit();
}
}
#Override
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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);*/
if(item.getItemId() == R.id.change_city){
showInputDialog();
}
return false;
}
private void showInputDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Change city");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
changeCity(input.getText().toString());
}
});
builder.show();
}
public void changeCity(String city){
WeatherFragment wf = (----->WeatherFragment<------).getSupportFragmentManager()
.findFragmentById(R.id.container);
wf.changeCity(city);
new CityPreference(this).setCity(city);
}
}
The Error is in the Public Void ChangeCity where weather fragment is being called. This is an error that keeps occuring and I am not sure what is going on.

Use this:
public void changeCity(String city){
WeatherFragment wf = (WeatherFragment) getSupportFragmentManager()
.findFragmentById(R.id.container); // removed '.'
wf.changeCity(city);
(new CityPreference(this)).setCity(city);
}

Related

Android App Stops working when I click certain Button

I am having issue with my app, my app loads however when I click on "options" button it stops working and quits the app.
Can someone help me?
first code is from options.java class and second code is from mainactivity
package com.example.sportsquizzone;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
/**
* Created by Usman on 22/02/2016.
*/
public class options extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
}
public void onButtonClick(View view) {
Intent b = new Intent(this, MainActivity.class);
b.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(b);
}
public void btnAbout(View view) {
AlertDialog.Builder About = new AlertDialog.Builder(this);
About.setMessage("* Developed by Usman Saddique \n* Developed on \n* This game runs on Android version 4.2 and above")
.setPositiveButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setTitle("About")
.setIcon(R.drawable.question_mark)
.create();
About.show();
}
}
package com.example.sportsquizzone;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
public void onButtonClick(View v)
{
if(v.getId() == R.id.btnPlay)
{
Intent i = new Intent(MainActivity.this, level.class);
startActivity(i);
}
if(v.getId() == R.id.btnOptions)
{
Intent i = new Intent(MainActivity.this, option.class);
startActivity(i);
}
}
#Override
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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void Instructions(View view) {
final AlertDialog.Builder Instructions = new AlertDialog.Builder(this);
Instructions.setMessage("The SQZ is designed to test your knowledge across variety of sports, to play: \n1. Click Play \n2. Select Dificulty \n3. when quiz begins, four answers will be provided select the correct one. ")
.setPositiveButton("Got It", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setTitle("Instructions")
.setIcon(R.drawable.question_mark)
.create();
Instructions.show();
}
public void btnQuit(View view) {
new AlertDialog.Builder(this)
.setIcon(R.drawable.warning)
.setTitle("Closing Application")
.setMessage("Are you sure you want to Quit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
I can not see the your logcat error but I think you have created the "option" class and its layout separately. So if it is what you have done , probably you have forgotten to declare the activity on manifest:
include it on your manifest:

how to restore RecyclerView?

I have the following situation:
I have two activities (Main Activity and ProductCard Activity)
Main Activity has a RecyclerView with items in it.
( User can add items manually. For example he add 3 items)
User click on some item and go to the ProductCard Activity
In the ProductCard Activity user click on the button (moneyOk_readylist) and go to the Main Activity
But when User do last step, RecyclerView become blank (empty).
I know that it is connected with states, but doesn't understand how.
Can anyone help me?
MainActivity
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private List<Person> persons;
private RecyclerView rv;
final String LOG_TAG = "myLogs";
RVAdapter adapter;
Person person;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
rv = (RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
persons = new ArrayList<>();
adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
if ((savedInstanceState != null) && (savedInstanceState.getSerializable("card") != null)) {
persons.clear();
persons.addAll((List<Person>) savedInstanceState.getSerializable("card"));
adapter.notifyDataSetChanged();
Log.d(LOG_TAG, "restore card with persons" + persons);
}
adapter.setOnItemClickListener(new RVAdapter.MyClickListener() {
#Override
public void onItemClick(int position, View v) {
Toast.makeText(MainActivity.this, "push on item" + position, Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this, ProductCard.class);
startActivity(intent);
Log.d(LOG_TAG, "go to ProductCard class" + persons);
}
});
adapter.setOnItemLongClickListener(new RVAdapter.MyLongClickListener() {
#Override
public void onItemLongClick(int position, View v) {
Toast.makeText(MainActivity.this, "push on item long" + position, Toast.LENGTH_SHORT).show();
Log.d(LOG_TAG, "Removed " + position);
persons.remove(position);
adapter.notifyDataSetChanged();
}
});
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
person= new Person("Emma Wilson", "23 years old", R.drawable.im_beach);
persons.add(person);
Log.d(LOG_TAG, "push add" + persons);
adapter.notifyDataSetChanged();
}
});
}
#Override
protected void onResume() {
super.onResume();
adapter = new RVAdapter(persons);
rv.setAdapter(adapter);
Log.d(LOG_TAG, "onresume" + persons);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("card", (Serializable) persons);
Log.d(LOG_TAG, "save cards" + persons);
}
#Override
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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
ProductCard Activity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
/**
* Created by Dmitry on 01.11.2015.
*/
public class ProductCard extends AppCompatActivity implements AdapterView.OnItemClickListener {
final String LOG_TAG = "myLogs";
Button moneyOk_readylist;
ImageButton addPhoto;
ImageView imageView;
private static int LOAD_IMAGE_RESULTS = 1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.product_card);
moneyOk_readylist=(Button)findViewById(R.id.moneyOk_readylist);
addPhoto=(ImageButton)findViewById(R.id.add_photo);
imageView=(ImageView)findViewById(R.id.imageView);
addPhoto.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
addPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
//i.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(i,"Select picture"),1);
}
});
moneyOk_readylist.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(ProductCard.this,MainActivity.class);
startActivity(intent);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Here we need to check if the activity that was triggers was the Image Gallery.
// If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
// If the resultCode is RESULT_OK and there is some data we know that an image was picked.
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
// Let's read picked image data - its URI
imageView.setImageURI(data.getData());
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
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();
switch (id) {
case R.id.action_save_card:
Toast.makeText(ProductCard.this, "Карточка сохранена", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(ProductCard.this,MainActivity.class);
startActivity(intent);
Log.d(LOG_TAG, "go to Main class" );
break;
case R.id.action_delete_card:
Toast.makeText(ProductCard.this, "Карточка удалена", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
}

Android Studio - Cannot resolve method 'add(int, com.*.*.WeatherFragment)'

I am creating a weather app and I am getting an error message.
Here is my Main Activity:
package com.haziqhussain.hazweather;
import android.support.v7.app.ActionBarActivity;
import android.text.InputType;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class WeatherActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_weather);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new WeatherFragment())
.commit();
}
}
#Override
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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);*/
if(item.getItemId() == R.id.change_city){
showInputDialog();
}
return false;
}
private void showInputDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Change city");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
changeCity(input.getText().toString());
}
});
builder.show();
}
public void changeCity(String city){
WeatherFragment wf = (WeatherFragment).getSupportFragmentManager()
.findFragmentById(R.id.container);
wf.changeCity(city);
new CityPreference(this).setCity(city);
}
}
Please could someone help and the problem is the onCreate section, .add is causing an error and it is not resolving the container.
Most likely, WeatherFragment inherits from android.app.Fragment. In WeatherActivity, you are using the backport of fragments (e.g., getSupportFragmentManager()). That means that WeatherFragment has to extend android.support.v4.app.Fragment.

Getting Cast Exception Android

I'm creating a simple note app in android. I am basically using two activity classes named as Main Page and Second Activity. I'm storing some data in the shared preferences in second activity and want to use it in my first Activity. I'm storing data in shared preferences as (String,Integer) key-pair. In my main activity class, when i'm getting the value from shared preferences as Integer and compare it with value 0,i'm getting an exception that java.lang.string can't be cast to java.lang.integer. I don't know why this exception is coming. Android studio is not giving me an exception but when i run my app, my app crashes. I have attach the code for reference.
MainActivity class:
package com.example.prateeksharma.noteballondor;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class MainPage extends ActionBarActivity {
Notes notes = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main_page, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = getSharedPreferences("com.example.prateeksharma.noteballondor", Context.MODE_PRIVATE);
//Map<String, Integer> notesMap = (Map<String, Integer>)sharedPreferences.getAll();
List<Notes> listNotes = new ArrayList<>();
Map<String, ?> prefsMap = sharedPreferences.getAll();
for (Map.Entry<String, ?> entry: prefsMap.entrySet()) {
if (entry.getValue() == 0){
notes = new Notes();
}
notes.setNoteText(entry.getKey());
listNotes.add(notes);
sharedPreferences.edit().putInt(entry.getKey(),2);
}
NotesArrayAdapter notesArrayAdapter = new NotesArrayAdapter(this,R.layout.list_layout, listNotes);
final ListView listview = (ListView) findViewById(R.id.listView);
listview.setAdapter(notesArrayAdapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
/**/
#Override
public void onItemClick(AdapterView<?> parent, final View view,
int position, long id) {
notes = (Notes) parent.getItemAtPosition(position);
Intent intent = new Intent(MainPage.this, SecondActivity.class);
intent.putExtra("Notes",notes);
startActivity(intent);
}
});
}
#Override
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
switch (item.getItemId()) {
case R.id.new_link:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Notes",(android.os.Parcelable)null);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
SecondActivity Class:
package com.example.prateeksharma.noteballondor;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class SecondActivity extends ActionBarActivity {
public SharedPreferences sharedPreferences;
SharedPreferences.Editor edit;
Notes notes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
sharedPreferences = getSharedPreferences("com.example.prateeksharma.noteballondor", Context.MODE_PRIVATE);
edit = sharedPreferences.edit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_second, menu);
return true;
}
#Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
notes = (Notes)intent.getParcelableExtra("Notes");
EditText editText = (EditText) findViewById(R.id.editText);
if(notes != null){
editText.setText(notes.getNoteText());
}
else{
editText.setText(null);
}
}
#Override
protected void onStop() {
super.onStop();
getIntent().removeExtra(notes.getNoteText());
}
#Override
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();
Intent intent = getIntent();
//noinspection SimplifiableIfStatement
switch (item.getItemId()) {
case R.id.save:
EditText editText = (EditText)findViewById(R.id.editText);
if (notes != null){
edit.putInt(String.valueOf(editText.getText()),1);
}
else{
edit.putInt(String.valueOf(editText.getText()),0);
}
edit.commit();
return true;
case R.id.delete:
if(notes != null){
edit.remove(notes.getNoteText());
edit.commit();
finish();
return true;
}
default:
return super.onOptionsItemSelected(item);
}
}
}
Notes class that I'm using:
package com.example.prateeksharma.noteballondor;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by Dell on 02-06-2015.
*/
public class Notes implements Parcelable {
private String noteText;
public String getNoteText() {
return noteText;
}
public void setNoteText(String noteText) {
this.noteText = noteText;
}
private Notes(Parcel in) {
noteText = in.readString();
}
public Notes(){
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(noteText);
}
#SuppressWarnings("unused")
public static final Parcelable.Creator<Notes> CREATOR = new Parcelable.Creator<Notes>() {
#Override
public Notes createFromParcel(Parcel in) {
return new Notes(in);
}
#Override
public Notes[] newArray(int size) {
return new Notes[size];
}
};
}
Can anybody tell me why this error is coming in MainActivity class at this line
if (entry.getValue() == 0)
Thank you..
Try to replace
if ((Integer)entry.getValue() == 0){
With
If((Integer.parseInt( entry.getValue()) ==0){

How to return list item selected to parent activity - android

I'm learning android development. And I trying to do the following:
An wellcome activity, with a TextView with the following text: "Please Select"
This Textview, has an OnClick Listener setted.
My intent is, when the user click on this textview, one new activity with a listview must be opened.
This listview cointains some values like: Country 1, Country 2, Country 3 and so on;
So, when the user select one value, this value must be returned to the parent activity.
In my parent activity I have the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
countrySamples = (TextView)findViewById(R.id.countrySamples);
countrySamples.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListCountrySelectedFragment whatKindOjectIsThis = new ListCountrySelectedFragment();
whatKindOjectIsThis.setListCountrySelectedActivityDelegate(new ListCountrySelectedFragment.ListCountrySelectedActivityDelegate() {
#Override
public void selectCountry(String name) {
selectItem(name);
}
});
}
});
}
[...]
public void selectItem(String name) {
int index = valuesArray.indexOf(name);
if (index != -1) {
countryButton.setText(name);
}
}
And I've created an blank fragment with a list.
And added the following code:
public static interface ListCountrySelectedActivityDelegate {
public abstract void selectCountry(String name);
}
private ListCountrySelectedActivityDelegate delegate;
[...]
But, my fragment is never started.. The true is.. I have to create a Fragment to this? Or, must by an activity? Or I'm totally wrong?
Thanks
Edited (complete code):
Login Activity:
package com.testenum_13;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import com.testenum_13.R;
import com.testenum_13.adapters.CountryAdapter;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.HashMap;
public class Login extends ActionBarActivity {
TextView countryButton;
private EditText codeField;
private int countryState = 0;
private ArrayList<String> countriesArray = new ArrayList<String>();
private HashMap<String, String> countriesMap = new HashMap<String, String>();
private boolean ignoreOnTextChange = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
countryButton = (TextView)findViewById(R.id.countryButton);
countryButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Intent intent = new Intent(Login.this, CountrySelected.class);
CountrySelected fragment = new CountrySelected();
fragment.setCountrySelectActivityDelegate (new CountrySelected.CountrySelectActivityDelegate() {
#Override
public void countryWSelected(String name) {
selectCountry(name);
}
});
//startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
#Override
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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void selectCountry(String name) {
int index = countriesArray.indexOf(name);
if (index != -1) {
ignoreOnTextChange = true;
codeField.setText(countriesMap.get(name));
countryButton.setText(name);
countryState = 0;
}
}
}
Country Activity:
package com.testenum_13;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.internal.widget.ActionBarOverlayLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import com.testenum_13.adapters.CountryAdapter;
import com.testenum_13.adapters.CountryAdapter.Country;
import java.util.List;
public class CountrySelected extends FragmentActivity {
public static interface CountrySelectActivityDelegate {
public abstract void countryWSelected(String name);
}
private CountryAdapter listViewAdapter;
private CountrySelectActivityDelegate delegate;
ListView countryList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_country_selected);
countryList = (ListView)findViewById(R.id.countryList);
listViewAdapter = new CountryAdapter(getBaseContext());
countryList.setAdapter(listViewAdapter);
countryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Country country = null;
int section = listViewAdapter.getSectionForPosition(position);
int row = listViewAdapter.getPositionInSectionForPosition(position);
if (row < 0 || section < 0) {
return;
}
country = listViewAdapter.getItem(section, row);
if (position < 0) {
return;
}
if (country != null && delegate != null)
{
delegate.countryWSelected(country.name);
}
finish();
}
});
}
public void setCountrySelectActivityDelegate(CountrySelectActivityDelegate delegate) {
this.delegate = delegate;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_country_selected, menu);
return true;
}
#Override
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_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thanks
You could use startActivityForResult(Intent, int) in the parent Activity and override onActivityResult() in the child activity. See this tutorial for more details on how to implent this pattern.

Categories

Resources