I'm trying to display the name of the Listview item on the action bar as it's title on the next activity screen using intents.
Here is my code.
MathematicsDBEntry.java
public class MathematicsDBEntry extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Intent i = getIntent();
String newActionBarTitle = i.getStringExtra("Position");
super.onCreate(savedInstanceState);
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(newActionBarTitle);
setContentView(R.layout.activity_mathematics_dbentry);
}
}
MathsActivity.java
public class MathsActivity extends ListActivity implements AdapterView.OnItemClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maths);
// storing string resources into Array
String[] math_data = getResources().getStringArray(R.array.maths_list_data);
ListView listView = getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, math_data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView) view;
//Toast.makeText(this, textView.getText().toString() + " " + position, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), MathematicsDBEntry.class);
intent.putExtra("Position", textView.toString());
startActivity(intent);
}
}
But all I get is the garbage value shown below.
You need to use the getText() method of TextView to get the data from it.
You need to change:
intent.putExtra("Position", textView.toString());
to:
intent.putExtra("Position", textView.getText().toString());
Related
How can I make this custom array list clickable to go to the others activities because I tried the intents but it doesn't work
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Names> namesArrayList = new ArrayList<Names>();
namesArrayList.add(new Names(R.drawable.call_centre, "Call Centre"));
namesArrayList.add(new Names(R.drawable.soco_academy_icon, "Academy"));
NamesAdapter NamesListAdapter = new NamesAdapter(this, namesArrayList);
ListView list = (ListView) findViewById(R.id.List_View);
list.setAdapter(NamesListAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
In onItemClick() you can determine which item was clicked by doing this:
Name selectedName = NamesListAdapter.getItem(position);
Then you can do whatever you want with that.
I'm trying to display different texts for a textview according to the listview listitem click. For now Am creating Lot of new activities to achieve this task. Following Codes are one of that scenario. Is there any simple way to show the different texts in a Same Activity(BSC Activity). I dont want to create Post Activity. For an example If listview position == 0 item clicked
R.String.Bsc
have to display in bsc Activity. If position == 1 clicked
R.String.Post
Have to display in bsc activity.
ListView class
public class AHSMLS extends AppCompatActivity{
ListView list;
String[] itemname ={
"Degree in Physiotherapy",
"Post Graduate options"
};
Integer[] imgid={
R.drawable.mlsico,
R.drawable.mls
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cp_listview_main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Physiotherapy");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ImageView img = (ImageView)findViewById(R.id.thumbnail);
img.setImageResource(R.drawable.physiotherapy);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (position == 0) {
Intent intent = new Intent(getApplicationContext(), Bsc.class);
startActivity(intent);
}else if (position == 1){
Intent intent = new Intent(getApplicationContext(), Post.class);
startActivity(intent);
}
}
});
}
}
Bsc activity
public class Bsc extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_text_view);
TextView tv3 = (TextView)findViewById(R.id.durationtextView);
tv3.setText("•\t 3 or 4 Year");
TextView tv4 = (TextView)findViewById(R.id.institutiontextView);
tv4.setText(R.string.bsc);
}
}
Post activity
public class Post extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_text_view);
TextView tv3 = (TextView)findViewById(R.id.durationtextView);
tv3.setText("•\t 3 or 4 Year");
TextView tv4 = (TextView)findViewById(R.id.institutiontextView);
tv4.setText(R.string.Post);
}
}
If i got your question correctly. Is this one You looking For? With this solution you dont need your post class. You can change your texts with the Bsc class.
ListView class
public class AHSMLS extends AppCompatActivity{
ListView list;
String[] itemname ={
"Degree in Physiotherapy",
"Post Graduate options"
};
Integer[] imgid={
R.drawable.mlsico,
R.drawable.mls
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cp_listview_main_activity);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Physiotherapy");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ImageView img = (ImageView)findViewById(R.id.thumbnail);
img.setImageResource(R.drawable.physiotherapy);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), Bsc.class);
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putInt("x", position);
//Add the bundle to the intent
intent.putExtras(bundle);
startActivity(intent);
}
}
});
}
}
Bsc activity
public class Bsc extends AppCompatActivity {
TextView tv3;
TextView tv4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multiple_text_view);
tv3 = (TextView)findViewById(R.id.durationtextView);
tv4 = (TextView)findViewById(R.id.institutiontextView);
//Get the bundle
Bundle bundle = getIntent().getExtras();
//Extract the data…
int stuff = bundle.getInt("x");
if(stuff == 0){
tv3.setText("•\t 3 or 4 Year");
tv4.setText(R.string.bsc);
}else if(stuff == 1){
tv3.setText("•\t 3 or 4 Year");
tv4.setText(R.string.Post);
}
}
}
So you dont need Your Post activity Here. Good luck Bro!
(sorry about the crappy formatting... still have never figured out how to preserve indentation when pasting code)
Anyhoo.... I think this is what Reaz Murshed is try to say:
public class AHSMLS extends AppCompatActivity{
// START NEW CODE
TextView tv3;
TextView tv4;
// END NEW CODE
ListView list;
String[] itemname ={
"Degree in Physiotherapy",
"Post Graduate options"
};
Integer[] imgid={
R.drawable.mlsico,
R.drawable.mls
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cp_listview_main_activity);
// START NEW CODE
tv3 = (TextView)findViewById(R.id.durationtextView);
tv3.setText("•\t 3 or 4 Year");
tv4 = (TextView)findViewById(R.id.institutiontextView);
// END NEW CODE
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Physiotherapy");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ImageView img = (ImageView)findViewById(R.id.thumbnail);
img.setImageResource(R.drawable.physiotherapy);
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
if (position == 0) {
// START NEW CODE
tv4.setText(R.string.bsc);
}else if (position == 1){
tv4.setText(R.string.Post);
// END NEW CODE
}
}
});
}
}
I am having trouble receiving the position or the value of a clicked ListViewItem in a SecondActivity.
My MainActivity:
public class MainActivity extends Activity {
String[] ingredients;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ingredients = getResources().getStringArray(R.array.ingredients);
final ListView ingredientList = (ListView) findViewById(R.id.ingredientList);
ArrayAdapter <String> listAdapter = new ArrayAdapter <String> (this,android.R.layout.simple_list_item_1, ingredients);
ingredientList.setAdapter(listAdapter);
ingredientList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView <?> parent, View view, int pos, long id) {
int itemPosition = pos;
String itemName = (String) ingredientList.getItemAtPosition(pos);
// THIS TOAST WORKS FINE: Toast.makeText(getApplicationContext(), "Position: " + itemPosition + " ListItem: " + itemName, Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
In the SecondActivity I want to display the value or the name of the clicked ListItem in a TextView like a title and I want to have another TextView and put in a text about the clicked ListViewItem.
Right now I got this:
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView titleView = (TextView) findViewById(R.id.textView);
TextView descriptionView = (TextView) findViewById(R.id.textView2);
}
I hope you can help me! Thank you!!
PutExtra in firstActivity and getExtra in the secondActivity is the solution. check : How to use putExtra() and getExtra() for string data
In your main activity use this :
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("position", itemPosition);
intent.putExtra("name", itemName);
startActivity(in);
and in SecondActivity.java get these two values like this :
String itemNm=getIntent().getStringExtra("name");
int pos=getIntent().getExtras().getInt("position");
hope this will help you.
I have a gridView with imageViews in one Activity and I want to load one of them in other activity on a new ImageView when select it, but i have no results
Activity 1:
public class Level extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level);
GridView gridview = (GridView) findViewById(R.id.gridviewLevel);
ImageAdapter ia = new ImageAdapter(this);
gridview.setAdapter(ia);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(Level.this, "" + position, Toast.LENGTH_SHORT).show();
initImageCheck( v.getId());
}
});
}
public void initImageCheck(int id){
Intent intentIC = new Intent(this, ImageCheck.class);
intentIC.putExtra("ID", id);
startActivity(intentIC);
}
}
Activity 2:
public class ImageCheck extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_check);
Bundle extras = getIntent().getExtras();
int id = extras.getInt("ID");
ImageView imgView = (ImageView) findViewById(R.id.imageViewCheck);
imgView.setImageResource(id);
}
}
Whats the problem??? Thanks.
v.getId() is the ID of the View, not of the image resource.
Have ImageAdapter also hold the id of resource used per item, and also provide a method like getResourceIdForItem(int position).
String[] songList = {
"1",
"2",
"3",
};
Spinner sp;
TextView selection;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection = (TextView) findViewById(R.id.selection);
Spinner spin = (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter<Object> aa = new ArrayAdapter<Object>(
this,
android.R.layout.simple_spinner_item,
songList);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
}
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
selection.setText(songList[position]);
String song = songList[position];
Intent intent = new Intent(this, Tabview.class);
Bundle b = new Bundle();
b.putString("song", song);
intent.putExtras(b);
startActivityForResult(intent, 0);
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
{
I have this code written right now. Pretty much what I'm trying to do is use the spinner selection to open a new activity (which I have done) BUT, I am also trying to use httpget to download information based on a link.
So, for ex:
If I press "1", it will open up a new activity, then it will call an httpget method, then it will download the data based on what you pressed. and that data will change for each option you press (EX. "1"= google.com, "2"=facebook.com, etc.) and then that data will be displayed in the activity.
I also want to only use ONE activity to display the data for each selection.
Also in my "Tabview.class" I have:
Bundle b = new Bundle();
String song = b.getString("song");
Thanks for any help!
What exactly is the error you get? The code seems to work. If you wanted to choose something according to the selection, you can use a simple if condition on the bundled string. Or if the issue here is about http get, take a look at the documentation here. You can use an AyncTask to make an http request without blocking.
For a reference, I have attached my code which just an improved version of your code.
public class ActivityxActivity extends Activity {
String[] songList = {"ONE","TWO","THREE"};
Intent intent1;
Bundle strs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner sp = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<Object> aa = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, songList);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(aa);
sp.setOnItemSelectedListener(new MyOnItemSelectedListener());
intent1 = new Intent(getApplicationContext(),AnotherActivity.class);
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
strs = new Bundle();
strs.putInt("item", pos);
strs.putString("song", parent.getItemAtPosition(pos).toString());
intent1.putExtras(strs);
startActivity(intent1);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {}
}
}
And in the other activity,
public class AnotherActivity extends Activity {
Bundle data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data = getIntent().getExtras();
Integer item = data.getInt("item");
//Use a switch(item) here to switch to different links based on selection
TextView tv = (TextView) findViewById(R.id.tv1);
tv.setText("Another Activity, Item is :" + item.toString());
}
}
Make sure you add your activity in the Android Manifest file.
Thanks