Update values from one activity to another

℡╲_俬逩灬. 提交于 2019-12-23 05:26:41

问题


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.


回答1:


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;
    }


}



回答2:


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?




回答3:


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);



回答4:


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.



来源:https://stackoverflow.com/questions/33500624/update-values-from-one-activity-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!