I have activity A where it has a ADD button when I click on the button it displays the list of users with check boxes in Second Activity and when I select the users and click the conform button it should display in all the users Activity A.I am using adapter to display the users.
from this button I am getting the list of checked values
private void checkButtonClick()
{
conform = (Button)findViewById(R.id.Confirm);
conform.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected...\n");
ArrayList<Namelist> stateList = contactadapteruser.listexample;
Bundle b=null;
Namelist state;
for(int i=0;i<stateList.size();i++)
{
state = stateList.get(i);
b = new Bundle();
b.putString("selected",state.getName());
if(state.isSelected())
{
responseText.append("\n" + state.getName());
}
}
Intent i= new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
}
});
}
from this button I am getting the I am retrieving the values
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i2 = getIntent();
Bundle b1 = i2.getExtras();
String name1 = b1.getString("selected");
tweet.setText(name1+",\t");
}
});
My problem I am unable to get the users which are checked.
Can any one help me .
use start activity for result to start your second activity.
Intent intent = new Intent(this, SecondActivity.class);
startActivityForResult(intent, 1);
from second activity before closing you can send
Intent return_intent = new Intent();
return_intent.putExtra("your_user_list",user_list);
setResult(RESULT_CODE,return_intent);
finish();
Now in your FirstActivity override onActivityResult() method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_CODE){
// GET YOUR USER LIST HERE AND USE IT FOR YOUR PURPOSE.
user_list=data.getExtra("your_user_list");
}
}
}
You can use broadcast sender and receiver also for your case. But above method is good for your task.
ArrayList<Namelist> stateList = contactadapteruser.listexample;
// convert your ArrayList to a String[]
String[] arr = new String[stateList.size()];
for (int i = 0; i < arr.length; i++)
{
arr[i] = stateList.get(i).getName();
}
Then using this question as a source, put the String array in your intent:
Bundle b = new Bundle();
b.putStringArray("selected", arr);
Intent i = new Intent(Contactselectuser.this,Contactrepresentativedetails.class);
i.putExtras(b);
startActivity(i);
Then to read the String array from your other activity:
Bundle b = getIntent().getExtras();
String[] array = b.getStringArray("selected");
Related
I have a buttonClickListener that is intended to uninstall multiple apps so I've run a for loop for that and I want to start onActivityResult after the completion of all uninstallation process. SO far onActivityResult runs after each uninstallation. I want only one onActivityResult at the end. Is there any way I can do that?
btnSelection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < appList.size(); i++) {
AllApps singleApp= appList.get(i);
if (singleApp.isSelected()) {
String app_pkg_name = singleApp.getPackageName();
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + app_pkg_name));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, UNINSTALL_REQUEST_CODE);
}
}
}
});
And this is the onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==UNINSTALL_REQUEST_CODE){
//things to do
}
}
First , is not possible but you can just simply execute onActivityResult for the last command and in order to do that, you need to have a filtered list first
btnSelection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// filter selected apps
List<AllApps> temp = new ArrayList<>();
for(AllApps app : appList){
if(app.isSelected())
temp.add(app);
}
// invoke app uninstallations
for (int i = 0; i < temp.size()-1; i++) {
startActivity(uninstallAppIntent(temp.get(i)));
}
// invoke get result for last entry
startActivityForResult(uninstallAppIntent(temp.get(temp.size()-1)), UNINSTALL_REQUEST_CODE);
}
});
// to avoid code duplicity
Intent uninstallAppIntent(Allapps singleApp){
String app_pkg_name = singleApp.getPackageName();
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + app_pkg_name));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
return intent;
}
One possible solution:
You can put variable i on data of intent and check if i==app.size() on onActivityResult.
Its A Simple use case of a If statement . you can take a boolean or just check appropriate condition. Checkout the code below .
btnSelection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AllApps singleApp=null;
for (int i = 0; i < appList.size(); i++) {
if (appList.get(i).isSelected()) {
singleApp=appList.get(i);
}
}
if(singleApp!=null){
String app_pkg_name = singleApp.getPackageName();
Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE);
intent.setData(Uri.parse("package:" + app_pkg_name));
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
startActivityForResult(intent, UNINSTALL_REQUEST_CODE);
}
}
});
NOTE IF appList contains multiple selected items then the last indexed item will be used . In this case if you want first selected item the you should break the loop .
Im trying to pass data from my TestSearch class to GoogleSearch class. I assigned the values of the tEdit test to a string variable and I want to pass that to GoogleSearch class. But my app get crash when I run it.
Button disp=(Button)findViewById(R.id.btn_Search);
disp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditText inputTxt = (EditText) findViewById(R.id.editText1);
String str = inputTxt.getText().toString().toLowerCase().trim();
ArrayList<HashMap<String, String>> userList = controller.searchBook(str);
if (userList.size() != 0) {
//Do something
}
else
{
Intent intent = new Intent("com.example.captchalib.GoogleSearch");
intent.putExtra("message", str);
startActivity(intent);
}
}
});
In my GoogleSearch Class I used Below Code to catch the Intent
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_menu);
Intent intent = getIntent();
String message = intent.getStringExtra("message");
((TextView)findViewById(R.id.Receive)).setText(message);
}
why this is happening and how to solve it ?
Logcat
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.captchalib.GoogleSearch (has extras) }
android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1545)
android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
Change this line:
Intent intent = new Intent("com.example.captchalib.GoogleSearch");
To:
Intent intent = new Intent(TestSearch.this, GoogleSearch.class);
I want to send some Data to another activity say SecondActivity and then getting Array data from That SecondActivity to Main activity using OnActivityResult
This is a app like when i press upload button i already have path i want to send that path to another activity and then getting the result in array from another activity to same mainactivity and then want to view that array in textview
CSVUpload
public class CSVUploader extends Activity {
Button btnUpload;
EditText txtName;
EditText txtMessageName;
Bundle extras = getIntent().getExtras();
String FullPath = extras.getString("FullPath");
#Override
protected void onCreate(Bundle savedInstanceState) {
try{
File csvfile = new File(FullPath);
FileInputStream csvStream = new FileInputStream(csvfile);
BufferedReader in = new BufferedReader(new InputStreamReader(csvStream));
String line;
String[] name = null;
String[] number = null;
int iCount=0;
while ((line = in.readLine()) != null){
String[] RowData = line.split(",");
name[iCount] = RowData[0];
number[iCount] = RowData[1];
iCount++;
/* ContentValues values = new ContentValues();
values.put(key, value);
values.put(CsvProvider.NUMBER, number);
values.put("status",status);
getContentResolver().insert(CsvProvider.CONTENT_URI, values);
*/ }
in.close();
Bundle b =new Bundle();
Intent intent = new Intent();
b.putStringArray("name", name);
b.putStringArray("number", number);
intent.putExtras(b);
setResult(RESULT_OK, intent);
finish();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
FirstActivity
public void uploadfile(View view){
edittext = (EditText)findViewById(R.id.txtFile);
Toast.makeText(NewMessage.this, FullPath, 2000).show();
if(FullPath != null)
{
Intent intent1 = new Intent(this, CSVUploader.class);
intent1.putExtra("FullPath", FullPath);
startActivityForResult(intent1, 2);
}
else
{
Toast.makeText(NewMessage.this, "No File Selected", 2000).show();
}
}
// Listen for results.
public void onActivityResult(int requestCode, int resultCode, Intent data){
// See which child activity is calling us back.
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PATH){
if (resultCode == RESULT_OK) {
curPathName = data.getStringExtra("GetPath");
curFileName = data.getStringExtra("GetFileName");
FullPath = curPathName+"/"+curFileName;
edittext.setText(curFileName);
/* Toast.makeText(NewMessage.this, resId, duration);*/
}
}
if (requestCode==2){
if (resultCode == RESULT_OK) {
Bundle b=this.getIntent().getExtras();
String[] name=b.getStringArray("name");
String[] number=b.getStringArray("number");
String[] status;
}
}
try to use this code. I am not sure this will work properly
MainActivity.java
public void uploadfile(View view){
Intent intent1 = new Intent(this, CSVUploader.class);
intent1.putExtra("FullPath", FullPath);
startActivity(intent1);
}
Replace your code likewise
CSVUploader.java
Bundle b=new Bundle();
Intent i = new Intent(this,MainActivity.class);
b.putStringArray("name",name);
b.putStringArray("number",number)
i.putExtras(b);
startActivity(i);
You can retrive it in MainActivity.java as
Bundle b=this.getIntent().getExtras();
String[] name=b.getStringArray("name");
String[] number=b.getStringArray("number");
You need to put the retrieving code into onCreate instead of where you have it now.
#Override
protected void onCreate(Bundle savedInstanceState) {
String FullPath = getIntent().getStringExtra("FullPath");
By the way why you want to use another activity to do the task. Why dont you do it in the same activity.
in My Main Activity I am pasing data using the data using intent using the putextra()
method
private void editHandler()
{
//send the details to another form
if (itemID > 0)
{
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtra("bundle", values);
startActivity(intent);
}
in the next Activity I am receiving that intent
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addor_edit);
todoNote = (EditText)findViewById(R.id.txt_todoNote);
todoDate = (EditText) findViewById(R.id.txt_dateTODO);
todoTime = (EditText) findViewById(R.id.txt_timeTODO);
todoalarm =(ToggleButton) findViewById(R.id.toggle_alarm);
alarmEnable = (ImageView) findViewById(R.id.img_alarmEnable);
canceltodo = (Button) findViewById(R.id.btn_cancelTODO);
maketodo = (Button) findViewById(R.id.btn_makeTODO);
//Receiving intent
Bundle bundle = getIntent().getBundleExtra("bundle");
getValuesForEdit(bundle);
todoalarm.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(todoalarm.getText().equals("ON"))
{
alarmEnable.setImageResource(R.drawable.dark_alarm);
alarm = 1;
}
else
{
alarmEnable.setImageResource(0);
alarm = 0;
}
}
});
maketodo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
addnewTODO();
}
});
}
private void getValuesForEdit(Bundle bundle)
{
// if the Edit Button is pressed get all values from listView
ID = bundle.getInt("id");
todoNote.setText(bundle.getString("text"));
todoDate.setText(bundle.getString("date"));
todoTime.setText(bundle.getString("time"));
alarm = bundle.getInt("alarm");
if (alarm == 1)
{
todoalarm.setText("ON");
alarmEnable.setImageResource(R.drawable.dark_alarm);
}
}
and The application crashes
am I doing wrong with intents?? which is the right way to pass data between
activities?? suggestions and advises are needed...
thanks!!
Intent intent = new Intent(this,AddorEdit.class);
intent.putExtra("text",singleItem.getText());
intent.putExtra("date", singleItem.getDate());
intent.putExtra("time", singleItem.getDate());
intent.putExtra("id", singleItem.getItemID());
intent.putExtra("alarm", singleItem.getAlarm());
startActivity(intent);
//While get this data:
ID = getIntent().getIntExtra("id");
todoNote.setText(getIntent().getStringExtra("text"));
todoDate.setText(getIntent().getStringExtra("date"));
todoTime.setText(getIntent().getStringExtra("time"));
alarm = getIntent().getIntExtra;
Try this
Bundle values = new Bundle();
singleItem = (TODOListItem) adapter.getItem(itemID);
Intent intent = new Intent(this,AddorEdit.class);
values.putString("text",singleItem.getText());
values.putString("date", singleItem.getDate());
values.putString("time", singleItem.getDate());
values.putInt("id", singleItem.getItemID());
values.putInt("alarm", singleItem.getAlarm());
intent.putExtras(values);
startActivity(intent);
and
Bundle bndl = this.getIntent().getExtras()
Picture 1. This is my create_layout.
Picture 2. This is my custom contact list when I clicked on add member button.
Picture 3. NOW HERE IS THE PROBLEM. When press on the select button. I wanted to list the chosen contact value back to my first image layout. But it is opening a duplicate of my first layout and appearing there.
Here is my code.
#Override
public void onClick(View v) {
StringBuilder checkedcontacts= new StringBuilder();
System.out.println("............"+ma.mCheckStates.size());
for(int i = 0; i < name1.size(); i++)
{
if(ma.mCheckStates.get(i)==true)
{
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
}
else
{
System.out.println("..Not Checked......"+name1.get(i).toString());
}
}
Intent i = new Intent (getApplicationContext(), CreateTab.class);
i.putExtra("str",checkedcontacts.toString());
startActivity(i);
finish();
}
});
I know the problem is that I make an intent so that when user clicks on select button, it will point back to CreateTab class which will repeat the onCreate. But how can I prevent from the onCreate again?
you should you use onActivityResult() method to read the returned result.
Do this in your CreateTab.class
Intent i = new Intent(getApplicationContext(), AddMember.class);
startActivityForResult(i, 100); // 100 is some code to identify the returning result
Method to read the result from newly created activity
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
String str = data.getExtras().get("str");
}
}
Send result back to old activity when StartActivityForResult() is used
Intent i = new Intent();
i.putExtra("str", checkedcontacts.toString());
// Setting resultCode to 100 to identify on old activity
setResult(100,i);
and close your AddMember activity
finish()
so your click event should look like this;
#Override
public void onClick(View v) {
StringBuilder checkedcontacts= new StringBuilder();
System.out.println("............"+ma.mCheckStates.size());
for(int i = 0; i < name1.size(); i++)
{
if(ma.mCheckStates.get(i)==true)
{
checkedcontacts.append(name1.get(i).toString());
checkedcontacts.append("\n");
}
else
{
System.out.println("..Not Checked......"+name1.get(i).toString());
}
}
Intent i = new Intent();
i.putExtra("str", checkedcontacts.toString());
// Setting resultCode to 100 to identify on old activity
setResult(100,i);
finish();
}
});
http://developer.android.com/training/basics/intents/result.html
Don't start previous activity again, just update selector Activity's result, and call finish().
Intent resultIntent = new Intent(this, PreviousActivity.class);
resultIntent.putExtra("selection",checkedcontacts.toString());
setResult(RESULT_OK,resultIntent);
finish();