将值从一项活动更新到另一项活动 [英] Update values from one activity to another

查看:56
本文介绍了将值从一项活动更新到另一项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,其中包含一个列表(类似于fb或instagram feed),并且我有一个喜欢该项目的按钮,并且还显示了喜欢的次数.现在,假设我单击该图像,它将在一个单独的活动(也具有赞"按钮)中打开该图像的分解图.如果我在分解视图活动中对图像进行了点赞,我想在上一个(提要)屏幕上在图像上反映相同的事物.我知道一种方法是在第一个活动的onResume()方法上从后端刷新提要.但是我不想刷新整个页面,失去可能的滚动以及破坏用户体验,只是为了反映UI和一些变量的变化.有什么标准的方法可以做这类事情吗? Facebook或其他类似应用程序如何实现此目的?如果有任何图书馆可以提供帮助,请也让我知道.谢谢.

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.

推荐答案

我的决定onActivityResult()+ notifyDataSetChanges()10分钟的代码并不完美,但是:

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


}

这篇关于将值从一项活动更新到另一项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆