I want the clicked item to be displayed in another activity.The second activity appears but the string doesn't. I tried other methods as well but the second activity just remains blank.
Here is the Main Activity
EditText TxtOne;
Button btOne;
ListView lisOne;
ArrayList aL;
ArrayAdapter<String> adapt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TxtOne = (EditText) findViewById(R.id.TxtOne);
btOne = (Button) findViewById(R.id.btOne);
lisOne = (ListView) findViewById(R.id.lisOne);
aL = new ArrayList<String>();
adapt = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,aL);
lisOne.setAdapter(adapt);
onBClick();
lisOne.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent itemInfo = new Intent(MainActivity.this, secondActivity.class);
itemInfo.putExtra("Itemis",position);
startActivity(itemInfo);
}
});
}
public void onBClick(){
btOne.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
String InpItem = TxtOne.getText().toString();
aL.add(InpItem);
adapt.notifyDataSetChanged();
}
});
}
The following is the second Activity
public class secondActivity extends AppCompatActivity {
TextView txt2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle passedData = getIntent().getExtras();
txt2 = (TextView)findViewById(R.id.txt2);
txt2.setText(passedData.getString("Itemis"));
}
}
I have read other similar posts but none of them work.
Some talk about the textView not being in the active layout because of which the result is NULL. The textView is in the active layout but still it doesn't display the item.
Solution anyone??
In the below Listener, you are adding position to itemInfo Intent. Here position is of type int, So you should do getIntExtra() to retrieve the Integer data instead of getStringExtra()
lisOne.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
Intent itemInfo = new Intent(MainActivity.this, secondActivity.class);
itemInfo.putExtra("Itemis",position); //position is INT value!!
startActivity(itemInfo);
}
});
So you should do -
getIntExtra(String name, int defaultValue); //this returns the int value of position.
Replace these two lines -
txt2 = (TextView)findViewById(R.id.txt2);
txt2.setText(Integer.toString(getIntent().getExtras().getIntExtra("Itemis",0))); //getting int value in second activity and converting into string for displaying
Note - you should use getStringExtra("Itemis") for retrieving string data from Intents. Not getString("Itemis").
Refer Intent Docs Page for further details on setting and retrieving values from intents.
You are passing and reading extras incorrectly. Do the following:
itemInfo.putExtra("Itemis", position);
and
txt2.setText(String.valueof(getIntent().getIntExtra("Itemis")));
Related
In my app I have three fragments. In the third fragment, a variable is take from a seekBar. Now I want to use this variable in my MainActivity. I tried to send the variable with an intent and show it in a textView onClick to test it, but the textView only shows „null“. Why isn‘t the variable send to the activity?
My MainActivity:
public class MainActivity extends AppCompatActivity {
TextView textTest;
public int a = 33;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textTest = (TextView) findViewById(R.id.textView3);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = getIntent();
String messages = intent.getStringExtra("message");
textTest.setText(String.valueOf(messages));
}
});
}
}
My Fragment that sends the variable:
public class ItemThreeFragment extends Fragment {
SeekBar seekBar;
TextView textView11;
int value = 10;
public static ItemThreeFragment newInstance() {
ItemThreeFragment fragment = new ItemThreeFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_three, container, false);
seekBar = (SeekBar) view.findViewById(R.id.seekBar);
seekBar.setMax(25);
seekBar.setProgress(value);
textView11 = (TextView) view.findViewById(R.id.textView11);
textView11.setText("" + value);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
value = i;
textView11.setText("" + value);
Intent intent = new Intent(getActivity().getBaseContext(),
MainActivity.class);
intent.putExtra("message", value);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
})
return view;
}
}
The problems are
In your activity, Intent intent = getIntent(); will get the intent that starts MainActivity.
In your fragment's onProgressChanged, your code doesn't communicate with MainActivity at all.
I can think of two relatively simple solutions for now:
Call a MainActivity function using ((MainActivity) getActivity()).someFunction() in your fragment.
In the MainActivity, use ((YourFragment) getSupportFragmentManager().findFragmentById(R.id.your_fragment_id)).property to access the fragment object's content. You can put the seek bar value into a class variable.
And check this: Communicating between a fragment and an activity - best practices
getActivity().findViewById(R.id.textView3).setText(""+value);
put the code inside onProgressChanged(),I hope its help you.
I think you missed startActivity(intent).
Intent intent = new Intent(getActivity().getBaseContext(),
MainActivity.class);
intent.putExtra("message", value);
getActivity().startActivity(intent);
I have been trying to figure out why my app has been crashing every time I try to view the list of player names by clicking leaderboard. This is not complete in terms of functionality yet, but I cannot get past the step of two players entering their names and the data being transferred to the list of names upon clicking the button (which will eventually be a leaderboard). I'm just trying to learn android basics and cannot understand why the crash keeps happening. Any help would be greatly appreciated.
public class ListAdapter extends ArrayAdapter<Player> {
LayoutInflater inflater;
public ListAdapter(Context context, ArrayList<Player> players){
super(context, R.layout.user_item, players);
inflater = LayoutInflater.from(context);
}
#Override
#NonNull
public View getView(int position, View view, #NonNull ViewGroup parent){
if (view == null){
view = inflater.inflate(R.layout.user_item, parent, false);
}
Player user = getItem(position);
String text = user.getName() + "" + "Score:" + user.getScore();
((TextView) view.findViewById(R.id.result)).setText(text);
return view;
}
}
Main activity
public class MainActivity extends AppCompatActivity {
private ArrayList<Player> players = new ArrayList<>();
#Override
/*
Creates buttons and assigns listeners.
*/
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button newgame = findViewById(R.id.newgame);
Button leader = findViewById(R.id.leader);
final EditText player1 = (EditText) findViewById(R.id.player1);
final EditText player2 = (EditText) findViewById(R.id.player2);
//New Game Button listener, irrelevant for right now
leader.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String name1 = player1.getText().toString();
String name2 = player2.getText().toString();
Player user1 = new Player(name1);
Player user2 = new Player(name2);
players.add(user1);
players.add(user2);
Intent list = new Intent(view.getContext(), Leaderboard.class);
list.putParcelableArrayListExtra(getString(R.string.players), players);
startActivity(new Intent(MainActivity.this, Leaderboard.class));
}
});
}
}
What should be a listview of players
public class Leaderboard extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leaderboard);
ArrayList<Player> players = getIntent().getParcelableArrayListExtra(getString(R.string.players));
ListView listView = findViewById(R.id.list);
ListAdapter adapter = new ListAdapter(this, players);
listView.setAdapter(adapter); //LINE CAUSING CRASH
Button reset = findViewById(R.id.reset);
Button menu = findViewById(R.id.menu);
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(Leaderboard.this, MainActivity.class));
}
});
}
}
Error Log:
Caused by: java.lang.NullPointerException: Attempt to invoke interface
method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:380)
at android.widget.ListView.setAdapter(ListView.java:575)
I do not understand how the size is null, if I am entering player names.
ArrayList players is null after you are assigning it using getIntent().getParcelableArrayListExtra(getString(R.string.players));
When you pass players to the adapter, the adapter tries to get the size of the arraylist and throws an exception because the list is null.
Make sure that your arraylist is not null before instantiating the adapter and assigning it to the listview.
How can i start a new activity with a button when i select from a spinner?
my code tho.. i've searched a lot but nothing works, hope something happens here :)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton button = (ImageButton) findViewById(R.id.imgbtnarroceros);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
});
Spinner spinner;
spinner= (Spinner) findViewById(R.id.spinner) ;
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.itinerary, android.R.layout.simple_spinner_item) ;
spinner.setAdapter(adapter);
From my understanding, you want to open an activity which has a button when you select an item from the spinner. Please correct me if my understanding is wrong.
setOnItemSelectedListener to your spinner and handle the item select event.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position){
case 0:
//start activity on selection of any item you want, here I am assuming first item.
Intent intent = new Intent(YourCurrentActivity.this, ActivityWithButton.class);
startActivity(intent);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
My main activity page for the android app is mainly a listView and on clicking an item in that list I would like a new activity to start. I have created the intent and such as follows in the onCreate method of my MainActivity class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
ListView lv = (ListView) findViewById(R.id.listview);
lv.setAdapter(adapter);
final MainActivity context = this;
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent myIntent = new Intent(context, SubActivity.class);
myIntent.addFlags(position); //Optional parameters
context.startActivity(myIntent);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
My new activity class looks like this:
public class SubActivity extends MainActivity{
ArrayList<String> sublist = new ArrayList<String>();
ArrayAdapter<String> subadapter;
ArrayList<ArrayList<String>> corrReceipts;
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
int position = intent.getFlags(); //this gives me the index of the receipt
corrReceipts = super.addedReceipts;
TextView tv = new TextView(this);
tv.setText("Hello");
subadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, sublist);
ListView slv = (ListView) findViewById(R.id.sublistview);
slv.setAdapter(subadapter);
sublist.addAll(corrReceipts.get(position));
subadapter.notifyDataSetChanged();
setContentView(slv);
}
}
I have my subActivity class extending MainActivity because it needs to access an array from which to get the information from. When I run this code, and click a list item, nothing happens. Any help as to what I'm doing wrong would be of great help!
All you want is an ItemClickListener not an ItemSelectedListener.
lv.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{
Intent myIntent = new Intent(context, SubActivity.class);
myIntent.addFlags(position); //Optional parameters
context.startActivity(myIntent);
}
});
OnItemSelectedListener is used for Spinners, and OnItemClickListener is used for ListViews.
I have two views named: first_view and second_view.
The first view consists of a button and an editable text view. The second view consists of a single text view.
In my first view, I want to put a number in the datable text view. As I click the button, it should display the number in the second view.
How can I code Java classes for the two views?
I am assuming that you want to setText within the same Activity, if not so then tell me, Ill change my answer.
Here is what you have to do.
public class MyActivity extends Activity {
Button btn;
EditText et;
TextView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.yourbtnID);
et = (EditText) findViewById(R.id.yourEtID);
tv = (TextView) findViewById(R.id.yourtvID);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String myText = et.getText().toString();
tv.setText(myText);
}
});
}
}
If you want to pass text between two Activities then use Intent.
In your current Activity do this.
Intent i = new Intent(YourCurrentActivity.this, YourNextActivity.class);
String str = yourEditText.getText().toString();
i.putExtra("edittextvalue" , str);
startActivity(i);
Then in next Activity do this..
Bundle extras = getIntent().getExtras();
String myEtText;
if (extras != null) {
myEtText = extras.getString("edittextvalue");
yourTextView.setText(myEtText);
}
if Two Views in the same Activity , you can do that
Button btn;
EditText txtInput;
TextView txtShow;
//btn=firstView.findViewWithTag(tag)
btn=firstView.findViewById(R.id.**);
txtInput=firstView.findViewById(R.id.**);
txtShow=secondView.findViewById(R.id.**);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String input=txtInput.getText().toString();
txtShow.setText(input);
}
});
if you have Two Activity :
Button btn;
EditText txtInput;
String VALUE_KEY="show";
private void test()
{
btn=(Button)findViewById(R.id.**);
txtInput=(Button)findViewById(R.id.**);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String input=txtInput.getText().toString();
Intent intent=new Intent(this, AnotherActivity.Class);
intent.putExtra(VALUE_KEY, input);
}
});
}
On the AnotherActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=this.getIntent();
String value=intent.getStringExtra(VALUE_KEY);
TextView view=(TextView)findViewById(R.id.txt);
view.setText(value);
}
Try this,
put your value in String: String et_str = EditText.getText().toString();
When you call the other intent,
Intent i = new Intent(first_view .class, second_view.class);
i.putExtra("REF",et_str);
StartActivity(i);
In The Second View, get this value using getExtra()