简体   繁体   English

从活动中的intentservice更新数据

[英]update data from intentservice on an activity

I have an activity with the following code and I am trying to update the data from IntentService by creating the instance of the class and calling the method to update but it's crashing with a null pointer exception at mAdapter. 我有一个使用以下代码的活动,我正在尝试通过创建类的实例并调用要更新的方法来更新IntentService的数据,但它在mAdapter处因空指针异常而崩溃。 Is this the right way to pass data from IntentService to an activity? 这是将数据从IntentService传递到活动的正确方法吗?

COde: 码:

public class MainActivity extends Activity {

    RecyclerView  rv;
    SimpleStringRecyclerViewAdapter mAdapter;

    public static final TypedValue mTypedValue = new TypedValue();
    public static int mBackground;
    public static List<String> mValues;

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

         rv = (RecyclerView) findViewById(R.id.recyclerview);
        setupRecyclerView(rv);

        send_msg("");
    }

    public void setupRecyclerView(RecyclerView recyclerView) {

        mAdapter = new SimpleStringRecyclerViewAdapter(getBaseContext(),
                getRandomSublist(Cheeses.sCheeseStrings, 3));
        recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
        recyclerView.setAdapter(mAdapter);
    }

    private List<String> getRandomSublist(String[] array, int amount) {
        ArrayList<String> list = new ArrayList<>(amount);
        Random random = new Random();
        while (list.size() < amount) {
            list.add(array[random.nextInt(array.length)]);
        }
        return list;
    }

    public static class SimpleStringRecyclerViewAdapter
            extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {



        public static class ViewHolder extends RecyclerView.ViewHolder {
            public String mBoundString;

            public final View mView;
            public final ImageView mImageView;
            public final TextView mTextView;

            public ViewHolder(View view) {
                super(view);
                mView = view;
                mImageView = (ImageView) view.findViewById(R.id.avatar);
                mTextView = (TextView) view.findViewById(android.R.id.text1);
            }

            @Override
            public String toString() {
                return super.toString() + " '" + mTextView.getText();
            }
        }

        public String getValueAt(int position) {
            return mValues.get(position);
        }

        public SimpleStringRecyclerViewAdapter(Context context, List<String> items) {
            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
            mBackground = mTypedValue.resourceId;
            mValues = items;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.cheesecake_homepage_list_item, parent, false);
            view.setBackgroundResource(mBackground);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.mBoundString = mValues.get(position);
            holder.mTextView.setText(mValues.get(position));

            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Context context = v.getContext();
                    /*Intent intent = new Intent(context, CheeseDetailActivity.class);
                    intent.putExtra(CheeseDetailActivity.EXTRA_NAME, holder.mBoundString);

                    context.startActivity(intent);*/
                }
            });

        }

        @Override
        public int getItemCount() {
            return mValues.size();
        }
    }



    public void send_msg( String  set_text){

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {

                List<String> uu = getRandomSublist(Cheeses.sCheeseStrings, 1);
                mValues.addAll(uu);

                mAdapter.notifyDataSetChanged();



            }
        });


    }

}

In intent service: 在意向服务中:

MainActivity test = new MainActivity();
test.send_msg("");

You can not use MainActivity instance like that in your Intent service code. 您不能在Intent服务代码中使用MainActivity实例。 There are multiple approach to update UI from service like, LocalBroadcastReceivers , Messengers , BoundedService etc. 有多种方法可从LocalBroadcastReceiversMessengersBoundedService等服务更新UI。

You can find an example to update UI from service using LocalBroadcastManager here 您可以在此处找到使用LocalBroadcastManager从服务更新UI的示例

update data from intentservice on an activity 从活动中的intentservice更新数据

By creating Object of class which is extending Activity or any other main application component is not right way for communication between components. 通过创建扩展了Activity或任何其他主要应用程序组件的类的Object,组件之间的通信方式不正确。

For with in process communication(as in your case want to send data from IntentService to Activity) use LocalBroadcastManager 对于进行中的通信(例如,您希望将数据从IntentService发送到Activity),请使用LocalBroadcastManager

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM