简体   繁体   中英

How to pass current data from Adapter to Adapter?

Heterogeneous RecyclerView

Hello friends I have a simple doubt

Here i am adding singleLineText

`addSingleLine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String singleLineText = singleline.getText().toString();
            if(singleLineText.length() != 0)
            {
                mAdapter.addItem(singleLineText,null);
                mAdapter.notifyDataSetChanged();
                Log.e(TAG,"adding single line text");
            }
            singleline.getText().clear();
        }
    });`

On this part i am adding MultiLineText

` addMultiLine.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String multiLineText = multiline.getText().toString();
            String myList[] = multiLineText.split(",");
            for(String item : myList)
            {
                mAdapter.addItem(null,item);
                mAdapter.notifyDataSetChanged();
                Log.e(TAG,"adding multi line text");
            }
            multiline.getText().clear();
        }
    });
}`

My Adapter part of code:

` public void addItem(String singleLineText, String item) {
    Model model = new Model();
    if(item == null) {
        model.setText1(singleLineText);
        model.settingSingleLineText(true); // How to identify single line
    }
    else
    {
        model.setText2(item);
        model.settingMultiLineText(true); // How to identify multiple line
    }
    modelList.add(model);
}`

GetViewType Method:

` public int getItemViewType(int position) {
   if (modelList.get(position).IfSingleLine() != null)
        return VERTICAL;
    else {
        return HORIZONTAL;
    }
}`

Model class code snippet:

private Boolean checkSingleLine = null;

public Boolean IfSingleLine()
{
    return checkSingleLine;
}
public void settingSingleLineText(Boolean txt1)
{
    checkSingleLine = txt1;
}
public void settingMultiLineText(Boolean txt2)
{
    checkMultiLine = txt2;
}   

` Problem: How to identify the singleLineText and multiLineText by using the Model Class??

You have a problem with your if (modelList.get(position).IfSingleLine() != null) . IfSingleLine() will never be null . You want to check if it is true or false and this is not how you check for that.

Change your getItemViewType to the following and you will get correct orientation result from this function.

public int getItemViewType(int position) {
   if (modelList.get(position).IfSingleLine())
        return VERTICAL;
    else {
        return HORIZONTAL;
    }
}`

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