吐司获取意图数据,为什么不RecyclerView CardView? [英] Toast gets intent data why doesn't CardView in RecyclerView?

查看:222
本文介绍了吐司获取意图数据,为什么不RecyclerView CardView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通行证的EditText 数据传回CardViews的 RecyclerView 列表中的用户输入的活动。一切工作正常,除了一个问题。我创建的列表中的第一个 CardView 后,创建的下一个CardView不与用户输入的数据串更新。第二CardView显示从第一输入相同的数据串(在第一CardView)。例如,如果我输入上的EditText行字苹果那么第一个CardView显示苹果正确。后来我回到输入活动,然后我输入单词绿色和第二CardView将被创建,但它会再次出现apple一词,而不是预期的绿色的。

I have a user input Activity that passes EditText data back to a RecyclerView list of CardViews. Everything is working fine except for one problem. After I've created the first CardView in the list, the next CardView created doesn't update with the user input data strings. The second CardView shows the same data strings from the first input (on the first CardView). For example, if I input the word "apple" on the EditText line then the first CardView shows "apple" correctly. Later I go back to the input Activity and then I input the word "green" and the second CardView will be created but it will show the word "apple" again instead of the expected "green".

我设置在创建CardView时祝酒它们显示在的onActivityResult 方法接收正确的数据(即第一个数据输入显示单词苹果正确地用于第一干杯和第二数据输入的绿色示出了用于第二组吐司的)。因此,尽管在祝酒词也许正确接收数据的东西不与RecyclerView的电源适配器没有得到适当的更新工作。任何想法?

I set up toasts for when the CardView is created and they show the correct data being received in the onActivityResult method (meaning the first data input shows the word "apple" correctly for the first toasts and the second data input "green" shows for the second set of toasts). So while the toasts are receiving the data correctly perhaps something is not working with the RecyclerView's adapter not getting updated properly. Any ideas?

CardViewActivity输入文件:

CardViewActivity input file:

...
public void onClickSave(View v) {
...
else if (stringToDo > 0 && stringNotes1 > 0 ) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(cListenerEditText.getWindowToken(), 0);
        String doMessage = cListenerEditText.getText().toString();
        String note1Message = dListenerEditText.getText().toString();
        Intent returnIntent2 = new Intent();
        returnIntent2.putExtra("MESSAGE1", doMessage);
        returnIntent2.putExtra("MESSAGE2", note1Message);
        boolean success = true;
        if (success) {
            setResult(ListContactsActivity.RESULT_OK, returnIntent2);
        }
        else {
            setResult(ListContactsActivity.RESULT_CANCELED, returnIntent2);
        }
        finish();
    }

RecyclerViewActivity文件:

RecyclerViewActivity file:

...
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    if((requestCode==1)&&(resultCode==RESULT_OK)&&(data !=null)){
        String doMessage=data.getStringExtra("MESSAGE1");
        String note1Message=data.getStringExtra("MESSAGE2");
        Contact contact = new Contact(doMessage,note1Message);
        mContactsAdapter.addItem(contact);
        mRecyclerView.scrollToPosition(0);
        Toast toast2 = Toast.makeText(this, doMessage, Toast.LENGTH_LONG);
        toast2.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP,0,0);
        toast2.show();
        Toast toast3 = Toast.makeText(this, note1Message, Toast.LENGTH_LONG);
        toast3.setGravity(Gravity.CENTER_VERTICAL | Gravity.BOTTOM,0,0);
        toast3.show();
    }
}

适配器文件:

public class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private static class EmptyViewHolder extends RecyclerView.ViewHolder {
    public EmptyViewHolder(View itemView) {
        super(itemView);
    }
}

private class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView cardBlankText2;

    public ContactViewHolder(View itemView) {
        super(itemView);
        cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemTapListener != null) {
            Contact contact = mContacts.get(getLayoutPosition());
            mOnItemTapListener.onItemTap(contact, getLayoutPosition());
        }
    }
}

private Context mContext;
private LayoutInflater mLayoutInflater;

private List<Contact> mContacts;
private List<ListItem> mItems;

private OnItemTapListener mOnItemTapListener;

public ListContactsAdapter(Context context, List<Contact> contacts) {
    mContext = context;
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mContacts = contacts;
    mItems = buildItemsList();
}

public void setOnItemTapListener(OnItemTapListener listener) {
    mOnItemTapListener = listener;
}

private List<ListItem> buildItemsList() {
    List<ListItem> items = new ArrayList<>();
    if (mContacts.size() > 0) {
        for (Contact contact : mContacts) {
            items.add(new ContactItem(contact));
        }
    } else {
        // when R. list is first created or when the number of cards
        // is deleted until there are zero, show the defaultcard_layout
        // and "Click the + above to get started".
        for (int i=0; i<1; i++) {
            items.add(new EmptyItem());
        }
    }
    return items;
}

public void addItem(Contact contact) {
    if (mContacts.size()==0) {
        // if list is empty we must
        // remove empty cards first
        mItems.clear();
        notifyDataSetChanged();
    }
    // in any case we need to add
    // item on the top of the list and scroll to the top position
    mContacts.add(contact);
    mItems.add(new ContactItem(contact));
    notifyItemInserted(0);
}

public void removeItem(Contact contact, int position) {
    mContacts.remove(contact);
    if (mContacts.size()==0) {
        // if no more contacts in list,
        // we rebuild from scratch
        mItems.clear();
        mItems.addAll(buildItemsList());
        notifyDataSetChanged();
    } else {
        // else we just need to remove
        // one item
        mItems.remove(position);
        notifyItemRemoved(position);
    }
}

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

@Override
public int getItemViewType(int position) {
    return mItems.get(position).getType();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == ListItem.EMPTY_TYPE) {
        View itemView = mLayoutInflater.inflate(R.layout.defaultcard_layout, parent, false);
        return new EmptyViewHolder(itemView);
    } else {
        View itemView = mLayoutInflater.inflate(R.layout.singlecard_layout, parent, false);
        return new ContactViewHolder(itemView);
    }
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
    int type = getItemViewType(position);
    if (type == ListItem.CONTACT_TYPE) {
        ContactItem item = (ContactItem) mItems.get(position);
        final Contact contact = item.getContact();
        ContactViewHolder holder = (ContactViewHolder) viewHolder;
        holder.cardBlankText2.setText(contact.getName() + " " + contact.getSurname());
    }
}

public interface OnItemTapListener {
    void onItemTap(Contact contact, int position);
}

}

推荐答案

太复杂。让我们简单。
这个问题似乎是内的onActivityResult在推动数据到列表中。

Too much complication. Let us make it simple. The problem seems to be within onActivityResult in pushing data to the list.

请改变片段如下。

最初声明全局自定义列表和自定义适配器。 (你的情况:联系)

AdapterContactList conAdapter;
List<Contact> conList;

在初始化的onCreate您的列表,并添加空单适配器和其连接到RecyclerView。

conList= new ArrayList<>();
conAdapter= new AdapterContactList (this, conList);
recyclerView.setAdapter(conAdapter);

然后在你的onActivityResult做如下图所示。

if (resultCode == RESULT_OK && requestCode == 101) {
  String doMessage = data.getStringExtra("MESSAGE1");
  String doMessage1 = data.getStringExtra("MESSAGE2");
  Contact con = new Contact(doMessage, doMessage1);
  conList.add(con);
  conAdapter.notifyDataSetChanged();
}

就是这样,现在你会看到数据在回收站视图中正确填充。并希望您Recyclerview适配器和部分剩下的就是在这种情况下正常工作。

That's it, Now you will see the data populating correctly in the recycler view. And Hope your Recyclerview Adapter and rest of the part is working fine in this case.

查询让我知道。

这篇关于吐司获取意图数据,为什么不RecyclerView CardView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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