Data FROM fragment TO activity - android

I'm trying to pass data from a fragment to an activity using a method in the Activity:
public void setID(int i,int j) {
theme = i;
thread = j;
}
and then, from the fragment I use the Intent to create the activity and pass the data with:
private class ListItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3) {
int pos=position+1;
ThreadActivity threadactivity = new ThreadActivity();
threadactivity.setID(id,pos);
Intent q = new Intent(getActivity(), threadactivity.getClass());
startActivity(q);
}
}
but, when the activity starts the values of "i" and "j" are 0. What I'm doing wrong?

The activity you created and the one you started with the intent are different. You should pass the parameters into the intent and then get them back in the activity.
private class ListItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView arg0, View arg1, int position, long arg3)
{
Intent intent = new Intent(context, threadactivity.class);
intent.putExtra("theme", id);
intent.putExtra("thread", pos);
startActivity(intent);
}
}
then in the onCreate of the activity you would do something like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
theme = (int) getIntent().getExtras().get("theme");
thread = (int) getIntent().getExtras().get("thread");
}

This ThreadActivity threadactivity = new ThreadActivity(); is pointless because you don't control the creation of activity. The system does. So all data transfer between activities is done through intent, not getter and setter.
Intent q = new Intent(getActivity(),ThreadActivity.class);
q.putExtra("theme", i);
q.putExtra("thread" j);
startActivity(q);
Then you get the values in the next activity using getIntent().getIntExtra(VARNAME, defaultValue);

This is not the right way to pass data to activity. Use Intent to pass data.
Read this:
http://startandroid.ru/en/lessons/complete-list/241-lesson-28-extras-passing-data-using-intent.html

Please use like below
((YourActivity)getActivity()).yourMethod();
yourMethod() is declared in Activity.

Related

Unable to start intent from within ListActivity OnItemListClick

I am trying to use the OnItemListClick method of ListActivity to launch a new activity with more specific information about the list item. To do this I am using a bundle as advised by the top answer of How do I pass an object from one activity to another on Android? but no matter what I try the OnItemListClick doesn't seem to do anything. I also tried without passing the bundle thinking that was causing a problem as advised here Using an intent to start new activity from ListActivity and that also does nothing onClick. How am I supposed to make it so when an item in the listview is clicked it returns the object associated with that row and passes it to a new activity?
public class TaskList extends ListActivity {
private ArrayList<Task> stuffToDo, completed;
private TaskAdapter t_adapter;
public Button add;
private File saveFile;
private Bundle clickBundle = new Bundle();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_list);
stuffToDo = new ArrayList<Task>();
completed = new ArrayList<Task>();
t_adapter = new TaskAdapter(this, R.layout.row_rl, stuffToDo);
setListAdapter(t_adapter);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Make a bundle and put the Task selected in it then pass this to the
// intent to start the new activity
Intent launchIntent = new Intent(getApplicationContext(), TaskDisplay.class);
Log.d("Debug", "Intent made");
clickBundle.clear();
clickBundle.putSerializable("Task", stuffToDo.get(position));
Log.d("Debug", "Task put in bundle");
launchIntent.putExtras(clickBundle);
this.startActivity(launchIntent);
Log.d("Debug", "launched");
}
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.addtaskbutton:
promptUserForInfo();
t_adapter.notifyDataSetChanged();
// added for debug
saveInfo();
return true;
case R.id.cleartasks:
clearTasks();
t_adapter.notifyDataSetChanged();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
and my code that is in the more specific activity
// Set the bundle equal to the extra put with the intent
input = this.getIntent().getExtras();
// If there are extras(a task) then assign it to the empty task variable
if (input != null){
task = (Task) input.getSerializable("Task");
}
here this mean you are pointing towards list.
change Intent launchIntent = new Intent(this, TaskDisplay.class);
to Intent launchIntent = new Intent(youractivityname.this, TaskDisplay.class);
or to Intent launchIntent = new Intent(getApplicationContext(), TaskDisplay.class);
and make this super.onListItemClick(l, v, position, id); first line of list item click method.
Try this...
this.startActivity(launchIntent);

Android finishing activity not working

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
}

After ListView onClick,change activity

Here's my code
this.getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(this,lastview.class);
startActivity(i);
}
});
the "this" is a ListActivity,but I want the next activity is a normal activity
and my code is wrong in this line
Intent i = new Intent(this,lastview.class);
the wrong message is
The constructor Intent(new AdapterView.OnItemClickListener(){}, Class<lastview>) is undefined
how can I fix it ?
change this line
Intent i = new Intent(this,lastview.class);
like this, change your activity to MyListActivity
Intent i = new Intent( MyListActivity.this, lastview.class);
Here is the sample code .This will help you.
public class YourListActivity extends ListActivity {
private Context context;
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
context =this;
.....
......
this.getListView().setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Intent i = new Intent(context,lastview.class);
// else you can use the code like below
//Intent i = new Intent(YourListActivity.this ,lastview.class);
startActivity(i);
}
});
}
You can also write it like :
startActivity(new Intent(Yourclass_name.this,lastview.class));

Dynamic jump to a tab and change its content at the same time?

In the first tab I have a listview from where I extract a string, and in the second tab I have a textview. When i click an item in the list, I should go in the second tab and and the string value should be displayed in the textview.
public static String mystring;
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
mystring = listofHashMap.get(position).get("keyname").toString(); //it's ok here
Intent intent=new Intent(this, MyActivity.class);
startActivity(intent);
}
The code above(placed in ListViewActivity) is what i have tried and it works (but MyActivity is not anymore a tabcontent,it is a full-screen standalone activity), having the following code snippet in MyActivity:
tv = (TextView)findViewById(R.id.tvid);
lv = new ListViewActivity();
tv.setText(lv.mystring);
But, as I said I want that MyActivity to be a tabcontent,so I have tried this:
public MyTabActivity taba = new MyTabActivity();
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
mystring = listofHashMap.get(position).get("keyname").toString();
int i=1;//the tab where I want to go, and MyActivity is its content
taba.ChangeTab(i);
}
ChangeTab() is a static method within MyTabActivity(the activity that extends TabActivity ) which simply does setCurrentTab(i) .
So, doing this, MyActivity will always display the name of first clicked item, even I select other items in the list then.I belive that what i have to do is setContent() again for MyActivity or do something with that static String mystring, I have tried many solutions but with no result. I tried to be as accurate I could on what i want to do, please some help on this issue.
My suggestion would be to use a broadcast. I think that the timing would require a sticky broadcast. Maybe something like this:
public MyTabActivity taba = new MyTabActivity();
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
mystring = listofHashMap.get(position).get("keyname").toString();
// create the intent to send and give it the data
Intent i = new Intent("updateTextView");
i.putExtra("textFromList", mystring);
sendStickyBroadcast(i);
int i=1;//the tab where I want to go, and MyActivity is its content
taba.ChangeTab(i);
}
And then in MyActivity, write something like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// your onCreate method
registerReceiver(updateTextReceiver, new IntentFilter("updateTextView"));
}
protected void onDestroy() {
super.onDestroy();
// your onDestroy method
unregisterReceiver(updateTextReceiver);
}
private BroadcastReceiver updateTextReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
tv = (TextView)findViewById(R.id.tvid);
String myString = intent.getStringExtra("textFromList");
if (myString != null) {
tv.setText(myString);
}
}
};
For the sticky broadcast, there's an additional permission needed in the manifest. I'm not sure but maybe you could just send a regular broadcast if you swapped the order of sending the broadcast and changing the tab.

how can i call a intent from spinners onitemSelected method

I have a spinner control in my android app, I want the user to select an option from the spinner. Once the option is selected the user can be redirected to another intent. My problem is how do i call intent from onitemselcted method of the spinner??.
Or i am doing wring there is other way in which i can do this. I need the user to set the option from the dropdown first before proceeding to the next page.
If i put startactivity(intent) in my onitemselected method i get this
error The method startActivity(Intent) is undefined for the type
MyOnItemSelectedListener (Myonitemselectedlistner is my class which
implements OnItemSelectedListener)
this is my onitemslectedlistner code
class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Context ctx = view.getContext();
SharedPreferences myPrefs = ctx.getSharedPreferences("hello", android.content.Context.MODE_WORLD_READABLE );
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("city", parent.getItemAtPosition(pos).toString());
prefsEditor.commit();
Intent intent = new Intent();
intent.setClass(ctx, MainActivity.class);
startActivity(intent);
}
public void onNothingSelected(AdapterView parent) {
//do nithong
}
}
Try doing something like:
YourParentClassName.this.startActivity(intent);
Here's the fully tested implementation... THIS WORKS:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
if (position == 1){
Intent intent = new Intent(MyActivity.this, AnotherActivity.class);
MyActivity.this.startActivity(intent);
}
}
public void onNothingSelected(AdapterView<?> parentView) {
// To do ...
}
});
Your onitemselected method is inside a private class that implements OnItemSelectedListener, which doesn't define startActivity. The startActivity method is part of Activity.
You should create a method in the parent activity, and call startActivity from there. You can make a call from your private class into your parent activity.
make your activity implements this interface
public class MyApp implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Context ctx = view.getContext();
SharedPreferences myPrefs = ctx.getSharedPreferences("hello", android.content.Context.MODE_WORLD_READABLE );
SharedPreferences.Editor prefsEditor = myPrefs.edit();
prefsEditor.putString("city", parent.getItemAtPosition(pos).toString());
prefsEditor.commit();
Intent intent = new Intent();
intent.setClass(ctx, MainActivity.class);
MyApp.this.startActivity(intent);
}
public void onNothingSelected(AdapterView parent) {
//do nithong
}
}

Categories

Resources