Once the user chooses a product from my ListView, it then puts the selected text from that ListView into an EditText. The problem I am having is when the user selects a product from the list, and then presses back, it comes up with the list again instead of returning to the EditText activity.
I have tried using "finish();" after the activity starts but nothing seems to be working.
Activity that holds the EditText that launches the List activity:
EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
CPU.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent CPUList = new Intent(getApplicationContext(),
CPUList.class);
startActivityForResult(CPUList, 1);
Intent i = getIntent();
String product = i.getStringExtra("key");
EditText CPU = ((EditText) findViewById(R.id.autoCompleteTextView4));
CPU.setText(product);
}
});
List view class
#Override
public void onCreate(Bundle OnsaveInstanceState) {
super.onCreate(OnsaveInstanceState);
setContentView(R.layout.activity_cpulist);
ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
String CPUList[] = {
"CPU's go here", "CPU's go here", "CPU's go here", "CPU's go here" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, CPUList);
listViewCPU.setAdapter(adapter);
listViewCPU.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish();
}
});
You need to launch your activity in a way that it doesn't get added to back stack.
Here's how you do that: https://stackoverflow.com/a/12358563/375929
If I understand you correctly, you are calling finish() on the wrong Activity. If you want the list Activity to finish then that's where you need to call finish()
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent(getApplicationContext(),
ListmenuActivity.class);
i.putExtra("key", CPU);
startActivity(getIntent());
startActivity(i);
finish(); // finish here
}
and remove finish() from your EditText Activity
Another issue I see is it looks like you are starting that second bit of code with the first using startActivityForResult() but you aren't sending back a result in your second code. Instead, you seem to be starting another Activity. It seems that second bit should be more like
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent();
i.putExtra("key", CPU);
setResult(1, i);
finish(); // finish here
}
Related
I want to receive intent on the two activity classes, but not sure how since startActivity can only start one intent to only one activity class
private void populateListView()
{
Log.d(TAG, "Populating listview");
Cursor data = dbManager.getTitle();
final ArrayList<String> listTitle = new ArrayList<>();
while (data.moveToNext())
{
listTitle.add(data.getString(1));
}
final ListAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, listTitle);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
String name = adapterView.getItemAtPosition(position).toString();
Cursor data = dbManager.getTitleID(name);
int titleID = -1;
while (data.moveToNext())
{
titleID = data.getInt(0);
}
if (titleID > -1)
{
Intent intent= new Intent(List_Title_Activity.this, MainActivity.class);
//Intent intent2= new Intent(List_Title_Activity.this, ViewListContents.class);
intent.putExtra("id", titleID);
intent.putExtra("title", name);
startActivity(edit);
}
else
{
Toast.makeText(List_Title_Activity.this, "No id associated with that name", Toast.LENGTH_SHORT).show();
}
}
});
MainActivity and ViewListContent:
Intent receiveIntent = getIntent();
selectedID = receiveIntent.getIntExtra("id",-1);
1 Activity is 1 page in android. That is, you can call/view 1 page/Activity at a time. If you want to share same data to multiple pages in use shared preference / Database. Or if you are using 1 activity and multiple fragments in it, use interface.
if you want to send data from one activity to 2nd activity you can use simple intent and pass data from first activity to second . But if you want to send the data first activity to second and get data from second activity to first activity i suggest you to use startActivityForResult. for more see here
I have created a list view something like this: http://imgur.com/a/bhWhR in activity1
Onitemselect i want to launch another acitivity which shows description about listitem1 like below
http://imgur.com/a/agGma
but did not find any helpful material to create and activity with same layout to get different content like this one ::
I did explore for quite some time and I could not find any solution that helped me .
Use OnItemClickListener and use intent to open other activities on item clicks
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (i == 1) {
Intent intent = new Intent(MainActivity.this, Lu1Activity.class);
startActivity(intent);
} else if (i == 2) {
Intent intent = new Intent(MainActivity.this, Lu2Activity.class);
startActivity(intent);
} else if (i == 3) {
Intent intent = new Intent(MainActivity.this, Lu3Activity.class);
startActivity(intent);
} else if (i == 4) {
Intent intent = new Intent(MainActivity.this, Lu4Activity.class);
startActivity(intent);
}
}
});
If you want to use same 2nd activity for each intent and show different content then use a global variable to know which item is selected and change content of 2nd activity as you wish.
Add a variable like this
public static int itemNumber;
Use OnItemClickListner
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
itemNumber = i;
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
});
In OtherActivity, Set different content to your views depending on itemNumber's value.
Note: You can also use putExtra to extend data to the intent and use that to determine which item is clicked.
So im trying to figure out if I can pass a intent extra from a custom adapter to an activity which then passes that intent extra to another activity?
Is something like that possible or is there a better way to do what I want to do?
Activity A:
Intent intent = new Intent(A.this, B.class);
intent.putExtra("someString", "string");
startActivity(intent):
Activity B:
onCreate(...) {
String myString = getIntent().getStringExtra("someString");
MyAdapter myAdapter = new MyAdapter(B.this, myString);
}
MyAdapter :
Context myContext;
String myString;
MyAdapter(Context context, String string) {
this.myContext = context;
this.myString = string
}
Now you have the String from activity A into your adapter :)
I assume, that you know how to send intent extras to an activity. And what you are looking for is a way to forward that same intent to another activity. You can do it like this:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtras(getIntent());
All the answers mentioned above would work. What I ended up using was an onItemClickListener on my listview and got the information that way. Instead of starting the intent in the custom adapter, i started the intent in the activity the custom adapter was in.
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is where you get the item clicked and specifically what you want from the custom adapter.
String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString();
Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class);
goToNewActivity.putExtra("yourExtra", yourString);
startActivity(goToNewActivity);
}
});
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//This is where you get the item clicked and specifically what you want from the custom adapter.
String yourString = ((TextView) view.findViewById(R.id.yourTextView)).getText().toString();
Intent goToNewActivity = new Intent(YourActivity.this, MainActivity.class);
goToNewActivity.putExtra("yourExtra", yourString);
startActivity(goToNewActivity);
}
});
I have two activities. One has a a form with few EditTexts. The values in these inputs should come from a master list and so, on focus of the EditText, I am taking the control to the another activity which uses 'android.support.v7.widget.SearchViewwith aListView`
OnFocus of a EditText I invoke the SearchActivity with following code:
ETVehicleRegnLoc.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
startActivity(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"));
}
}
});
Once the value is selected I am sending the value back with the following code
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent intent = new Intent(SearchActivity.this , VehicleActivity.class);
Bundle bundle = new Bundle();
bundle.putString("SelectedVal", dataArray.get(arg2).toString());
intent.putExtras(bundle);
startActivity(intent);
// Toast.makeText(SearchActivity.this, dataArray.get(arg2).toString(), Toast.LENGTH_SHORT).show();
}
});
This works fine. But because I am calling the VehicleActivity through Intent, its clearing the entire form of the values which are already entered.
But when I press the "back" button on the mobile, the values persist. But the purpose of going to the SearchActivity is not met :)
I think I have to simulate the back button press. how can i do it??
Use
startActivityForResult(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"),2);
instead of
startActivity(new Intent(getApplicationContext(), SearchActivity.class).putExtra("fieldName","RegnNum"));
And in onItemClickListener Do
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(2,intent);
finish();
And get the data in OnActivityForResult of your VehicleActivity
I am trying to set the text of an EditText via a ListView. Once the user chooses a product from the ListView, it will then go back to the activity that holds the EditText, and change the text within the EditText.
I am relatively new to Android and Java, so excuse me for being an absolute nub.
List Java:
final ListView listViewCPU = (ListView) findViewById(R.id.listViewCPU);
listViewCPU.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
String CPUList[] = {
"Products go here", "Products go here", "Products go here", "Products go here", "Products go here", "Products go here" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, CPUList);
listViewCPU.setAdapter(adapter);
listViewCPU.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent(getApplicationContext(), ListmenuActivity.class);
i.putExtra(" ", CPU);
startActivity(i);
}
});
}
}
EditText Java:
I am getting the error on "EditText4" defining "Syntax error on token "EditText4", delete this token"
EditText CPU = (EditText) findViewById(R.id.autoCompleteTextView4);
CPU.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent CPUList = new Intent(getApplicationContext(),
CPUList.class);
startActivityForResult(CPUList, 0);
}
Intent i = getIntent();
String product = i.getStringExtra(" ");
EditText CPU = ((EditText)findViewById(R.id.EditText4));
EditText4.setText(getIntent(i).getStringExtra(""));
});
Should be CPU.setText not EditText4 as you dont really have that defined (as long as I can see)
you set this way
#Override
public void onItemClick(AdapterView<?> listview, View myView,
int pos, long mylng) {
String CPU = (String) listview.getAdapter().getItem(pos);
Intent i = new Intent(getApplicationContext(), ListmenuActivity.class);
i.putExtra("key", CPU);
startActivity(i);
}
});
get the value like that
Intent i = getIntent();
String product = i.getStringExtra("key");
EditText CPU = ((EditText)findViewById(R.id.EditText4));
CPU.setText(product);
EditText CPU = ((EditText)findViewById(R.id.EditText4));
You are mising a semicolon on this line :)