SmallSani
SmallSani

Reputation: 171

Returning Data from the listview in the activity

From the start activity

Intent intent = new Intent(StartActivity.this, MarkersActivity.class);
startActivityForResult(intent, GoMarkerReturn);  

call other activity in which there is CustomListAdapter extends BaseAdapter. The listviewhas a picture, when clicked, to close the current activity and return the result to the starting activity

public class CustomListAdapter extends BaseAdapter {
...
    public View getView(int position, View convertView, ViewGroup parent) {
        holder.imggo.setOnClickListener(new View.OnClickListener() {
            ...
            @Override
            public void onClick(View v) {
            int clickedPosition = (Integer)v.getTag();
            NewsItem newsItem = (NewsItem)listData.get(clickedPosition);                
            Long goID = newsItem.getID();

            Intent myIntent = new Intent(v.getContext(), StartActivity.class);
            myIntent.putExtra("goID", goID);
            setResult(0, myIntent);

setResult(0, myIntent) dont work!

Upvotes: 0

Views: 886

Answers (2)

SmallSani
SmallSani

Reputation: 171

public class CustomListAdapter extends BaseAdapter {
...
    public View getView(int position, View convertView, ViewGroup parent) {
    ...
        holder.imggo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(v.getContext(), StartActivity.class);
                myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                myIntent.putExtra("goID", goID);
                v.getContext().startActivity(myIntent);

and

public class StartActivity extends Activity{
...
    @Override
    protected void onResume() {
        Intent intent = getIntent();
        Long goID = intent.getLongExtra("goID", 0);
        if (goID > 0){
               ...

Upvotes: 0

prijupaul
prijupaul

Reputation: 2086

StartActivityForResult and setResult is used to passed values between activities. Here in your case you need to call finish() in the second activity so that the second activity is destroyed and the first activity comes to foreground.

This is a good tutorial to learn how it functions. DataTransfers

Upvotes: 1

Related Questions