安卓:为什么CardView不与新用户输入的更新? [英] Android: why is CardView not updating with new user input?

查看:271
本文介绍了安卓:为什么CardView不与新用户输入的更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 RecyclerView 创建 CardView 期从用户输入(通过两个EditTexts)。

我能够正确地创建新卡。第一个 CardView 正确显示两个用户输入字段。当我创建了第二个 CardView 它显示正确的布局,但两个数据输入字段是不正确的。相反,显示了第二组的两个用户输入的,第二个 CardView 显示从第一个完全相同的两个用户输入 CardView

在创建后的第一个不知怎的 CardView 我需要清除在设置为对意图的字符串对象的onActivityResult() RecyclerView 活动。然后设置下一个用户输入到下一个 CardView 。不知道为什么,但Android的工作室还表示,即使在适配器的 onBindViewHolder 方法用于setSurname在接触类是从未使用过。

我是否需要使用的onStop()方法来完成 onClickSave 方法和销毁 CardViewActivity 一旦用户完成在领域的EditText输入查询的数据?缺少什么我在这里?

在这里用户输入的输入CardViewActivity文件:

  ...
公共无效onClickSave(视图v){
    。最终诠释stringToDo = cListenerEditText.getText()的toString()代替(,)。长度()。
    //如果只有两个用户输入的字段有数据
    否则如果(stringToDo大于0&放大器;&放大器; stringNotes1大于0){
        InputMethodManager IMM =(InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(cListenerEditText.getWindowToken(),0);
        字符串doMessage = cListenerEditText.getText()的toString()。
        。字符串note1Message = dListenerEditText.getText()的toString();
        意向意图=新的Intent();
        intent.putExtra(MESSAGE1,doMessage);
        intent.putExtra(MESSAGE2,note1Message);
        的setResult(1,意向);
        完();
    }//如果只stringToDo有数据以及其他都是空的。
    否则如果(stringToDo大于0&放大器;&放大器; stringNotes1 == 0&放大器;&放大器; stringNotes2 == 0&放大器;&放大器;
            stringDueDate == 0安培;&安培; stringDueTime == 0){
        InputMethodManager IMM =(InputMethodManager)
              getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(cListenerEditText.getWindowToken(),0);
        字符串doMessage = cListenerEditText.getText()的toString()。
        意向意图=新的Intent();
        intent.putExtra(MESSAGE1,doMessage);
        的setResult(1,意向);
        完();
    }

RecyclerView活动文件:

  ...
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);
    如果(要求code == 1){
        串doMessage = data.getStringExtra(MESSAGE1);
        串note1Message = data.getStringExtra(MESSAGE2);
        联系方式联系我们=新的联系人(doMessage,note1Message);
        mContactsAdapter.addItem(接触);
        mRecyclerView.scrollToPosition(0);
    }
}

适配器:

  ...
公共类ListContactsAdapter扩展RecyclerView.Adapter< RecyclerView.ViewHolder> {
...
私人列表<&列表项GT; buildItemsList(){
    清单<&列表项GT;项目=新的ArrayList<>();
    如果(mContacts.size()大于0){
        对于(联系人:mContacts){
            items.add(新ContactItem(接触));
        }
    }其他{
            的for(int i = 0; I< 1;我++){
            items.add(新EmptyItem());
        }
    }
    返回的物品;
}公共无效的addItem(联系方式联系我们){
    如果(mContacts.size()== 0){
        mItems.clear();
        notifyDataSetChanged();
    }
    mContacts.add(接触);
    mItems.add(新ContactItem(接触));
    notifyItemInserted(0);
}@覆盖
公共无效onBindViewHolder(最终RecyclerView.ViewHolder viewHolder,最终诠释位置){
    整型= getItemViewType(位置);
    如果(类型== ListItem.CONTACT_TYPE){
        ContactItem项目=(ContactItem)mItems.get(位置);
        最终的联系方式联系= item.getContact();
        ContactViewHolder架=(ContactViewHolder)viewHolder;
        holder.cardBlankText2.setText(contact.getName()++ contact.getSurname());
    }
}

数据模型文件:

 公共类联系{私人字符串名称;
私人字符串姓氏;公众联系(字符串名称,弦乐姓){
    this.name =名称;
    this.surname =姓;
}公共字符串的getName(){
    返回名称;
}公共无效setname可以(字符串名称){
    this.name =名称;
}公共字符串getSurname(){
    返回姓;
}公共无效setSurname(字符串姓){
    this.surname =姓;
}
}


解决方案

看你的code我看到实现此的addItem 方法的问题:

  mItems.add(新ContactItem(接触));
notifyItemInserted(0);

看来你是在底部增加项目和顶部通知插入。你应该保持插入和通知中的地位而言对齐。特别是,如果要插入在上面,你应该改变你的code如下:

  mItems.add(0,新ContactItem(接触));
notifyItemInserted(0);

I have a RecyclerView that creates CardViews from user input (via two EditTexts).

I am able to create the new Cards properly. The first CardView shows the two user input fields correctly. When I create the second CardView it shows up correctly on the layout but the two data input fields are not correct. Instead of showing the second set of two user inputs, the second CardView shows the exact same two user inputs from the first CardView.

Somehow after creating the first CardView I need to clear the String objects in the intents that are set to the onActivityResult() in the RecyclerView activity. Then set the next user inputs to the next CardView. Not sure why but Android Studio also says setSurname in Contact class is never used even though it is used in the Adapter's onBindViewHolder method.

Do I need to use onStop() method to complete the onClickSave method and destroy the CardViewActivity once the user is done inputing data in the EditText fields? What am I missing here?

CardViewActivity file where user enters input:

...
public void onClickSave(View v) {
    final int stringToDo = cListenerEditText.getText().toString().replace(" ", "").length();
    // if only the two user input fields have data
    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 intent = new Intent();
        intent.putExtra("MESSAGE1", doMessage);
        intent.putExtra("MESSAGE2", note1Message);
        setResult(1, intent);
        finish();
    }    

// If only stringToDo has data and others are empty.
    else if (stringToDo > 0 && stringNotes1 == 0 && stringNotes2 == 0 &&
            stringDueDate == 0 && stringDueTime ==0) {
        InputMethodManager imm = (InputMethodManager)
              getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(cListenerEditText.getWindowToken(), 0);
        String doMessage = cListenerEditText.getText().toString();
        Intent intent = new Intent();
        intent.putExtra("MESSAGE1", doMessage);
        setResult(1, intent);
        finish();
    }

RecyclerView Activity file:

...  
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
    super.onActivityResult(requestCode,resultCode,data);
    if(requestCode==1) {
        String doMessage=data.getStringExtra("MESSAGE1");
        String note1Message=data.getStringExtra("MESSAGE2");
        Contact contact = new Contact(doMessage,note1Message);
        mContactsAdapter.addItem(contact);
        mRecyclerView.scrollToPosition(0);            
    }
}  

Adapter:

...
public class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...
private List<ListItem> buildItemsList() {
    List<ListItem> items = new ArrayList<>();
    if (mContacts.size() > 0) {
        for (Contact contact : mContacts) {
            items.add(new ContactItem(contact));
        }
    } else {
            for (int i=0; i<1; i++) {
            items.add(new EmptyItem());
        }
    }
    return items;
}

public void addItem(Contact contact) {
    if (mContacts.size()==0) {
        mItems.clear();
        notifyDataSetChanged();
    }
    mContacts.add(contact);
    mItems.add(new ContactItem(contact));
    notifyItemInserted(0);
}

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

Data Model file:

public class Contact {

private String name;
private String surname;

public Contact(String name, String surname) {
    this.name = name;
    this.surname = surname;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}
}

解决方案

Looking at your code I see a problem with implementation of addItem method here:

mItems.add(new ContactItem(contact));
notifyItemInserted(0);

It seems you are adding item on bottom and notify insertion on top. You should keep insertion and notification aligned in terms of position. In particular, if you want to insert on top you should change your code as follows:

mItems.add(0, new ContactItem(contact));
notifyItemInserted(0);

这篇关于安卓:为什么CardView不与新用户输入的更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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