更新来自intentservice的活动数据

时间:2016-01-15 06:32:28

标签: android android-recyclerview intentservice

我有一个带有以下代码的活动,我试图通过创建类的实例并调用要更新的方法来更新来自IntentService的数据,但是它在mAdapter中因空指针异常而崩溃。这是将数据从IntentService传递给活动的正确方法吗?

代码:

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



            }
        });


    }

}

意图服务:

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

2 个答案:

答案 0 :(得分:1)

您不能在Intent服务代码中使用类似的MainActivity实例。 有多种方法可以更新来自LocalBroadcastReceiversMessengersBoundedService等服务的用户界面。

您可以找到使用LocalBroadcastManager here

从服务更新UI的示例

答案 1 :(得分:0)

  

更新来自intentservice的活动数据

通过创建扩展Activity的类的Object或任何其他主应用程序组件,不是组件之间通信的正确方法。

对于进程间通信(如果您希望将数据从IntentService发送到Activity),请使用LocalBroadcastManager