简体   繁体   中英

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. Is this the right way to pass data from IntentService to an activity?

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. There are multiple approach to update UI from service like, LocalBroadcastReceivers , Messengers , BoundedService etc.

You can find an example to update UI from service using LocalBroadcastManager here

update data from intentservice on an activity

By creating Object of class which is extending Activity or any other main application component is not right way for communication between components.

For with in process communication(as in your case want to send data from IntentService to Activity) use LocalBroadcastManager

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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