I´m new to app development and I´m trying to figure out how to send an user input like name, to a list view located in a diferent activity.
Thanks in advance.
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button mbuttonNext;
private EditText meditTextName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditTextName = (EditText) findViewById(R.id.editTextName);
mbuttonNext = (Button) findViewById(R.id.buttonNext);
mbuttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ClientList.class);
startActivity(intent);
}
});
}
}
ListViewActivity:
public class ClientList extends AppCompatActivity {
private ListView listaClientes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
listaClientes = (ListView) findViewById((R.id.listView));
}
}
In the main activity you need to need intent.putExtra
mbuttonNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ClientList.class);
intent.putExtra("VariableName", "Fred");
startActivity(intent);
}
});
Than in listView
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client_list);
listaClientes = (ListView) findViewById((R.id.listView));
Bundle bundle = getIntent().getExtras(); //get the intent and data passed
//next check that bundle is not null
if (bundle != null) {
String name = bundle.getString("VariableName");
//try loging out the value
Log.i("Name", name);
}
}
I believe this will solve your problem
When starting the new activity:
Intent newIntent = new Intent(MainActivity.this, ClientList.class);
newIntent.putExtra("com.example.myapp.NAME", "someName");
startActivity(newIntent);
Then in ClientList:
Intent intent = getIntent();
String name = intent.getStringExtra("com.example.myapp.NAME");
Can then use name variable as source data for the ListView.
Usually you should create an Adapter that will "set" the items in the ListView. In order for you to send/transfer the user input to another activity you can use "putExtra()". You can do that the following way:
intent.putExtra("str1",item)
where str1 is the key to get your item in the activity you started. To do that you should do:
Bundle b = getIntent().getExtras();
Obj a = (TypeCast) b.get("str1");
After that you should read how to create an Adapter and set it in a ListView in order for your ListView to show the item.
Related
I am trying to pass text from a custom dialog to the home activity. The bundle always comes up as null instead of the value I'm trying to pass and I can't figure out why. I've tried looking at similar questions but I have yet to find a solution.
Dialog
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_ingredients_dialog);
Button addButton = findViewById(R.id.addButton);
addButton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(AddIngredientsDialog.this,
android.R.layout.select_dialog_item, fruits);
editText.setAdapter(arrayAdapter);
text = editText.getText().toString();
Intent intent = new Intent(AddIngredientsDialog.this, AddIngredientsActivity.class);
intent.putExtra("Text", text);
startActivity(intent);
dialog.dismiss();
}
});
}
Home Activity
ingredientList = findViewById(R.id.listView);
ArrayList<String> ingredients = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_ingredients);
//bundle
Bundle extras = getIntent().getExtras();
text = extras.getString("Text");
button = findViewById(R.id.add);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
ingredients.add(text);
adapter = new ArrayAdapter<>(AddIngredientsActivity.this, android.R.layout.simple_list_item_1, ingredients);
ingredientList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
}
In your home activity, try using getIntent().getStringExtra("Text"), instead of extras.getString("Text");
//bundle
text = getIntent().getStringExtra("Text")
Problem could be here
`text = editText.getText().toString();`
Make sure you've written something in EditText
It is better using startActivityForResult to start your dialog Activity. Check this answer Sending data back to the Main Activity in Android
In my MainActivity, I create a variable called "pengar". I then send this variable to my second activity using putExtra on the intent. In the second activity, I edit the variable, however as I use the back button to return to my main activity, I can't use putExtra.
Write below code in your MainActivity
protected void onCreate(Bundle savedInstancesState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
pengar = extras.getString("key2");
}
}
public void nextActivity(View view)
{
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("key1",pengar);
startActivity(intent);
}
Write below code in your SecondActivity
protected void onCreate(Bundle savedInstancesState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras1 = getIntent().getExtras();
if (extras1 != null) {
var2 = extras1.getString("key1");
}
public void backButton(View view)
{
Intent intent2= new Intent(this,MainActivity.class);
intent2.putExtra("key2",var2);
startActivity(intent2);
}
}
Hope this helpful
I'm trying to display the name inputted by the user on a basic form I created using Explicit intent on a second activity on a TextView but when I click the button the app crashes with the following "Unfortunately, formIntent has stopped" but my code has no errors. I got this to work with a Toast message on new activity but with a TextView. Any help would be appreciated.
Here's my First Activity `public class MainActivity extends AppCompatActivity {
Button submit;
TextView name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button)findViewById(R.id.btnSubmit);
name = (TextView)findViewById(R.id.editTextname);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//from sending Activity
String userName = name.getText().toString();
String value = "Thank you " + userName + "your request is being processed" ;
Intent sendIntent = new Intent(v.getContext(), Main2Activity.class);
sendIntent.putExtra("userName", value);
//Verify that the intent will resolve to an activity
if (sendIntent.resolveActivity(getPackageManager()) != null)
{
startActivity(sendIntent);
}
}
});
}
}
`
Here's My second Activity
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle data = getIntent().getExtras();
if(data == null){
return;
}
String getName = getIntent().getExtras().getString("userName");
final TextView inputMessage = (TextView)findViewById(R.id.display);
inputMessage.setText(getName);
}
}
Please change this line to your activity name
Intent sendIntent = new Intent(MainActivity.this, Main2Activity.class);
i am new to android, i have created two activities where main activity consist one button to go to secondActivity in second activity where the user inputs name and the data is taken back to main activity using intents.The problem i am facing is i want to display the data in listview
MainActivity.java
public class MainActivity extends Activity {
Button addcontact;
ListView listview;
//TextView name;
ArrayAdapter<String> listAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listView1);
TextView name = (TextView) findViewById(R.id.txtname);
addcontact = (Button) findViewById(R.id.addbtn);
addcontact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivityForResult(i, 1);
savedata();
}
});
}
private void savedata() {
Intent intent = getIntent();
String fName = intent.getStringExtra("fname");
List<String> ListElements = new ArrayList<String>(Arrays.asList(fName));
ArrayAdapter<String> adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_list_item_1, ListElements);
listview.setAdapter(adapter);
}
}
SecondActivity.java
public class SecondActivity extends Activity {
EditText edname;
Button addbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
edname =(EditText) findViewById(R.id.edname);
addbtn = (Button) findViewById(R.id.savebtn);
addbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
intent.putExtra("fname", edname.getText().toString());
startActivity(intent);
}
});
}
}
I would suggest you don't recreate your first Activity. Instead of that approach, use startActivityForResult().
In short - Starting another activity doesn't have to be one-way. You can also start another activity and receive a result back. To receive a result, call startActivityForResult() (instead of startActivity()).
For more chek this link.
FirstActivity.java
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
btnOk = (Button) findViewById(R.id.btnOk);
btnOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Name = etName.getText().toString();
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
// Create a bundle object
intent.putExtra("NAME", Name);
startActivity(intent);
}
});
My Second Activity.java is as follows:
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
String UName;
String iName;
ListView lvName;
Button btnBack;
Bundle savedInstanceState;
#Override
protected void onCreate(Bundle b) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
b = getIntent().getExtras();
lvName = (ListView) findViewById(R.id.lvNames);
btnBack = (Button) findViewById(R.id.btnBack);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
UName=b.getString("NAME");
list.add(UName);
adapter.notifyDataSetChanged();
lvName.setAdapter(adapter);
lvName.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),
ThirdActivity.class);
// Create a bundle object
intent.putExtra("ITEMNAME", item);
startActivity(intent);
}
});
btnBack.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
}
});
}
My problem is whenever I click on back button, activity is restarted. So that first item in list is overwritten by second item. I want to get the output as list(items one by one) in second activity, whatever the data I enter in first activity. How can I do this?
For example when I enter ABC in edittext its displaying the same data ABC in list.Its good.
But when I click on back button and again enter any data in edittext its not displaying the data as second item in list but it is over writing the first item.This is the thing I wanna get to be solved...
You can put all items into intent in the first activity:
intent.putStringArrayListExtra(ITEMS, list);
And then you can extract them from intent in the second activity:
List<String> items = getIntent().getStringArrayListExtra(ITEMS);
use the extends Application class
public class MyGlobal extends Application {
public ArrayList<String> selectedAppsPackageNames = new ArrayList<String>();
}
for getting this in your activity
private MyGlobal mMyGlobal = (MyGlobal) getApplicationContext();
declare in manifest inside the Application Tag(give the path)
<application
android:name="com.afbb.lockaddicted.core.MyControlList"
..............
.........
</application>
You Just have to create the global list somewhere in your app like you can create Singletone or Application Class where you have to store your username List.
In below code you have to update your arrayList in first activity and you have to get your nameList in SecondActivity
public class MySingleton
{
private static MySingleton _instance;
private ArrayList<String> nameList;
private MySingleton()
{
}
public static MySingleton getInstance()
{
if (_instance == null)
{
_instance = new MySingleton();
}
return _instance;
}
public ArrayList<String> getNameList(){
return nameList;
}
public void setNameList(ArrayList<String> nameList(){
nameList = nameList;
}
}
You can save the names in the FirstActivity to shared preferences, with an id. Then retrieve those values in the SecondActivity. Also do not forget to clear the shared preferences when you destroy your FirstActivity.
FirstActivity
public class FirstActivity extends Activity {
private EditText etName;
private Button btnOk;
private String name;
private int id = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
etName = (EditText) findViewById(R.id.etName);
btnOk = (Button) findViewById(R.id.btnOK);
btnOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
id++;
SharedPreferences.Editor editor = getSharedPreferences("shared_pref", MODE_PRIVATE).edit();
editor.putString(Integer.toString(id), etName.getText().toString());
editor.commit();
Intent intent = new Intent(getApplicationContext(),
SecondActivity.class);
startActivity(intent);
}
});
}
#Override
protected void onDestroy() {
super.onDestroy();
getSharedPreferences("shared_pref", MODE_PRIVATE).edit().clear().commit();
}
}
SecondActivity
public class SecondActivity extends Activity {
private ArrayList<String> list = new ArrayList<String>();
private ArrayAdapter<String> adapter;
private String UName;
private String iName;
private ListView lvName;
private Button btnBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
lvName = (ListView) findViewById(R.id.lvNames);
btnBack = (Button) findViewById(R.id.btnBack);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
lvName.setAdapter(adapter);
SharedPreferences sharedPreferences = getSharedPreferences("shared_pref", MODE_PRIVATE);
Map<String,String> names = (Map<String, String>) sharedPreferences.getAll();
if(names != null){
for(String value: names.values()){
list.add(value);
}
adapter.notifyDataSetChanged();
}
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}