Android - RecyclerView﹕ No adapter attached; skipping layout?

2.3k views Asked by At

I create a Search Layout it good work .But it get me crash often :

RecyclerView﹕ No adapter attached; skipping layout

Here is my SearchActivity.java :

public class SearchActivity extends Activity implements RadioGroup.OnCheckedChangeListener{
    public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
    public static final String DIR_DATABASE = DIR_SDCARD + "/Android/data/";
    public ArrayAdapter adapter;
    public Cursor cursor_id;
    public Cursor cursor_final;
    public static String PACKAGE_NAME;
    EditText editText;
    DB db = new DB(SearchActivity.this);
    public Cursor cursor;
    public SQLiteDatabase sql;
    RecyclerView recList;
    Context context;
    RadioGroup radioGroup;
    int RadioID = R.id.rbNormal;
    int b = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        context = getApplicationContext();
        PACKAGE_NAME = getApplicationContext().getPackageName();
        CreateFile();
        db.GetPackageName(PACKAGE_NAME);
        try {
            db.CreateandOpenDataBase();
        } catch (IOException e) {
            e.printStackTrace();
        }
        sql = db.openDataBase();

        editText = (EditText) findViewById(R.id.search);

        final Button btnSearch = (Button) findViewById(R.id.btnSearch);
        final TextView tv = (TextView) findViewById(R.id.text);

        recList = (RecyclerView) findViewById(R.id.cardList);
        recList.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);

        radioGroup = (RadioGroup) findViewById(R.id.rgSearch);
        radioGroup.setOnCheckedChangeListener(this);

        recList.setLayoutManager(llm);

        btnSearch.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent motionevent) {
                if (editText.getText().length() >= 2){
                    tv.setVisibility(View.VISIBLE);
                }
                int action = motionevent.getAction();
                if (action == MotionEvent.ACTION_UP) {
                    if (editText.getText().length() < 2) {
                        Toast.makeText(SearchActivity.this, "Please enter two character!",
                                Toast.LENGTH_SHORT).show();
                        return true;
                    } else {
                        InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);


                        ContactAdapter ca = new ContactAdapter(createList(editText.getText()),context);
                        if(ca != null){
                            recList.setAdapter(ca);
                            tv.setVisibility(View.VISIBLE);
                            tv.setText(String.valueOf(b) + " " + getResources().getString(R.string.result));
                        }
                    }
                }
                return false;
            }
        });
    }


    private List<StructSearch> createList(Editable editable) {
    b = 0;
        String _Text = editable.toString();
        String sq = null;
        if (RadioID == R.id.rbNormal) {
            sq = "Question like '%"+_Text+"%'";
        }
        else if (RadioID == R.id.rbAnd) {
            String[] parts = Pattern.compile(" ", Pattern.LITERAL).split(_Text);
            for(String Result : parts) {
                sq += " Question like '%" + Result + "%' and";
            }
            sq = sq.substring(4, sq.length()-3);
        } else if (RadioID == R.id.rbOr) {
            String[] parts = Pattern.compile(" ", Pattern.LITERAL).split(_Text);
            for(String Result : parts) {
                sq += " or Question like '%" + Result + "%'";
            }
        }

        List<StructSearch> result = new ArrayList<StructSearch>();
        try {
            cursor = sql.rawQuery("SELECT * FROM WebSite_QaListDB WHERE "+sq, null);
                    if (cursor != null && cursor.moveToFirst()) {
                            do {
                                StructSearch ci = new StructSearch();
                                ci.Q_QaID = cursor.getInt(cursor.getColumnIndex("QaID"));
                                ci.Q_Question = cursor.getString(cursor.getColumnIndex("Question"));
                                ci.Q_Answer = cursor.getString(cursor.getColumnIndex("Answer"));
                                ci.Q_Title = cursor.getString(cursor.getColumnIndex("Title"));

                                cursor_id = sql.rawQuery("SELECT Item2ID FROM WebSite_ReleatedDB WHERE Item1ID =" + ci.Q_QaID, null);

                                try {
                                    if (cursor_id != null && cursor_id.moveToFirst()) {
                                        do {
                                            ci.R_Item2ID = cursor_id.getInt(cursor_id.getColumnIndex("Item2ID"));
                                            cursor_final = sql.rawQuery("SELECT Title FROM WebSite_CategoryDB WHERE CategoryID =" + ci.R_Item2ID
                                                    + " AND parentID != 0" ,null);
                                            try {
                                                if (cursor_final != null && cursor_final.moveToFirst()){
                                                    do {
                                                        ci.C_Title = cursor_final.getString(cursor_final.getColumnIndex("Title"));
                                                    }while (cursor_final.moveToNext());
                                                }
                                            }catch (Exception e){
                                                Log.i("xxx", "You have an error");
                                            }finally {
                                                if (cursor_final != null){
                                                    cursor_final.close();
                                                }
                                            }

                                        } while (cursor_id.moveToNext());
                                    }
                                }catch (Exception e){
                                    Log.i("xxx", "You have an error");
                                }finally {
                                    if (cursor_id != null) {
                                        cursor_id.close();
                                    }
                                }
                                result.add(ci);
                                b++;
                            } while (cursor.moveToNext());
                        adapter.notifyDataSetChanged();
                    }
        } catch (Exception e) {
            Log.i("xxx", "You have an error");
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return result;
    }

    private boolean CreateFile(){
        File file = new File(DIR_DATABASE + PACKAGE_NAME + "/BankDB");
        boolean flag =file.mkdirs();
        if(flag){
            Log.i("A_LOG","Create File");
            return true;
        }else {
            Log.e("A_LOG","Do not Create File");
            return false;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        RadioID = 0;
        RadioID = checkedId;
    }
}

All of code good work I think my error is from here :

                if(ca != null){
                    recList.setAdapter(ca);
                    ...
                }

If you need see my Adapter.java and DB.java or layouts I can show it.

I am using from Android Studio.

Image of layout(Search), my layout has a RadioGroup for optional search on my DataBase:

layoutSearch

1

There are 1 answers

3
Kinjal On

Instead of this

ContactAdapter ca = new ContactAdapter(createList(editText.getText()),context);

Write this code

ContactAdapter ca = new ContactAdapter(createList(editText.getText()),getActivity());

OR

ContactAdapter ca = new ContactAdapter(createList(editText.getText()),SearchActivity.this);

OR

ContactAdapter ca = new ContactAdapter(createList(editText.getText()),this);

I am not sure but it might be help to you.