Amit Tiwari
Amit Tiwari

Reputation: 3692

Update values from one activity to another

I have one activity which contains a list(similar to fb or instagram feed) and I have buttons for liking that item and it also shows number of likes. Now suppose I click on the image, it opens an exploded view of image in a separate activity which also has like button. If I do a like on the image in exploded view activity, I want to reflect the same thing on the image in previous(feed) screen. I know one way is to refresh the feed from backend on onResume() method of the first activity. But I don't want to refresh the whole page, lose possible scroll and disrupt user experience just for reflecting a change in UI and some variables. Is there any standard way of doing these kind of things? How does facebook or other similar apps achieve this? If there is any library which could help, do let me know that too. Thanks.

Upvotes: 0

Views: 2453

Answers (4)

Alberto S.
Alberto S.

Reputation: 7649

Maybe you could use startActivityForResult() and onActivityResult().

That way you could setup a code that implies that the user made something in the second activity that needs the first to refresh data.

Upvotes: 0

Iatsenko Anton
Iatsenko Anton

Reputation: 157

My decision onActivityResult() + notifyDataSetChanges() my code for 10 minutes not perfect but :

     public class MainActivity extends Activity {

        private AdapterListView adapterListView;
        private int adapterUpdateItemPosition;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main_activity);
            ListView listView = (ListView) findViewById(R.id.listView);
            ArrayList<SomeObject> list = new ArrayList<>();
            for (int i = 0 ; i < 20 ; i++ )
                list.add(new SomeObject());
            adapterListView = new AdapterListView( (LayoutInflater) getApplicationContext()
                    .getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE), list);
            listView.setAdapter(adapterListView);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    adapterUpdateItemPosition = position;
                    startActivityForResult(new Intent(MainActivity.this, SecondActivity.class), SecondActivity.REQUEST_UPDATE);
                }
            });
        }



        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(resultCode == RESULT_OK){
                if (requestCode == SecondActivity.REQUEST_UPDATE){
                    adapterListView.updateListView(adapterUpdateItemPosition, data.getBooleanExtra(SecondActivity.ADAPTER_POSITION, false));
                }
            }
        }
    }

public class SecondActivity extends Activity {
    public static final String ADAPTER_POSITION = "ADAPTER_POSITION";
    public static final int REQUEST_UPDATE = 2321;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_grid);

        findViewById(R.id.tv_item_grid).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_OK, new Intent().putExtra(ADAPTER_POSITION, true));
                finish();
            }
        });
    }
}

public class AdapterListView extends BaseAdapter {

    private LayoutInflater lInflater;
    private ArrayList<SomeObject> objects;

    public AdapterListView(LayoutInflater lInflater, ArrayList<SomeObject> objects) {
        this.lInflater = lInflater;
        this.objects = objects;
    }


    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public SomeObject getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = lInflater.inflate(R.layout.item_grid, parent, false);
        }
        SomeObject p = getItem(position);
        if(p.isLike()) {
            ((TextView) view.findViewById(R.id.tv_item_grid)).setText("LIKE");
        } else  ((TextView) view.findViewById(R.id.tv_item_grid)).setText("NOT LIKE");

        return view;
    }

    public void updateListView(int position, boolean isLike){
        getItem(position).setLike(isLike);
        notifyDataSetChanged();
    }
}

public class SomeObject{
    private boolean like = false;

    public boolean isLike() {
        return like;
    }

    public void setLike(boolean like) {
        this.like = like;
    }


}

Upvotes: 1

Shilpi
Shilpi

Reputation: 508

You can use LocalBroadcastManager to send broadcast to which your previous activity will be listening.

Intent intent = new Intent(LIST_UPDATED);
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);

Upvotes: 0

Nanoc
Nanoc

Reputation: 2381

Just refresh the downloaded data from where the listview adapter feeds.

Then call notifyDataSetChanged() on the adapter.

That way you update your data locally and refresh your list.

Is not possible to just update one row of a listview, is that what are you looking for?

Upvotes: 0

Related Questions