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??
You are passing and reading extras incorrectly. Do the following:
itemInfo.putExtra("Itemis", position);
and txt2.setText(String.valueof(getIntent().getIntExtra("Itemis")));
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.