android没有适配器附加跳过布局RecyclerView在搜索中

时间:2017-03-25 16:15:49

标签: android android-recyclerview

嘿朋友我正在尝试使用RecyclerView实现搜索视图,但收到错误no adapter attached skipping layout无法解决此问题我需要帮助 这是我的所有代码plz帮助

//我的连接器类

public class Connector {

public static HttpURLConnection connect(String urlAddress)
{
    try {
        URL url=new URL(urlAddress);
        HttpURLConnection conn=(HttpURLConnection) url.openConnection();
        //set propertiecs
        conn.setRequestMethod("POST");
        conn.setConnectTimeout(20000);
        conn.setReadTimeout(20000);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        //return
        return conn;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

}

//我的DataPackager类

public class DataPackager {

String query;

public DataPackager(String query) {
    this.query = query;
}
public String packData()
{
    JSONObject jo=new JSONObject();
    StringBuffer queryString=new StringBuffer();
    try {
     jo.put("Query",query);

        Boolean firstValue=true;
        Iterator it=jo.keys();
        do {
           String key=it.next().toString();
            String value=jo.get(key).toString();
            if (firstValue){
                firstValue=false;

            }else {
                queryString.append("&");
            }

            queryString.append(URLEncoder.encode(key,"UTF-8"));
            queryString.append("=");
            queryString.append(URLEncoder.encode(value,"UTF-8"));
        }while (it.hasNext());

        return queryString.toString();



    } catch (JSONException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

}

//我的Parser类

    public class Parser extends AsyncTask<Void,Void,Integer> {
   String data;
RecyclerView rv;
Context c;

ArrayList<String>name=new ArrayList<>();

public Parser(Context c, String data, RecyclerView rv) {
    this.data = data;
    this.rv = rv;
    this.c = c;
}

@Override
protected Integer doInBackground(Void... params) {
    return this.parse();
}

@Override
protected void onPostExecute(Integer integer) {
    super.onPostExecute(integer);

        if(integer==1)
            {
            rv.setAdapter(new MyAdapter(c,name));
            }else{
            Toast.makeText(c,"Unable to pass",Toast.LENGTH_SHORT).show();
        }
}

private int parse()
{
    try{
        JSONArray ja=new JSONArray(data);
        JSONObject jo=null;
        name.clear();

        for (int i=0;i<ja.length();i++){
            jo=ja.getJSONObject(i);
            String nameo=jo.getString("Name");
            name.add(nameo);

        }
        return 1;

    } catch (JSONException e) {
        e.printStackTrace();
    }
    return 0;
}

}

//我的SenderReciver类

    public class SenderReciver extends AsyncTask<Void,Void,String> {
Context c;
String query;
ProgressDialog pd;
String urlAddress;
RecyclerView rv;
ImageView noDataImg,noNetworkimg;

public SenderReciver(Context c, String query, String urlAddress,RecyclerView rv,ImageView...imageViews) {
    this.c = c;
    this.query = query;
    this.urlAddress = urlAddress;
    this.rv = rv;

     this.noDataImg = imageViews[0];
    this.noNetworkimg = imageViews[1];

}


@Override
protected void onPreExecute() {
    super.onPreExecute();
    pd=new ProgressDialog(c);
    pd.setTitle("Search");
    pd.setMessage("finding user plz wait");
    pd.show();
}

@Override
protected String doInBackground(Void... params) {
    return this.sendAndRecive();

}


@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    pd.dismiss();

    rv.setAdapter(null);
    if(result !=null){

        if(! result.contains("null"))
        {
            Parser p=new Parser(c,result,rv);
            p.execute();


            noNetworkimg.setVisibility(View.INVISIBLE);
            noDataImg.setVisibility(View.INVISIBLE);
        }else{
            noNetworkimg.setVisibility(View.INVISIBLE);
            noDataImg.setVisibility(View.VISIBLE);
        }

    }else{
        noNetworkimg.setVisibility(View.VISIBLE);
        noDataImg.setVisibility(View.INVISIBLE);
    }
}

private String sendAndRecive()
{
    HttpURLConnection conn = Connector.connect(urlAddress);
    if (conn==null)
    {
        return null;
    }
    try
    {
        OutputStream os=conn.getOutputStream();
        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(os));
        bw.write(new DataPackager(query).packData());

        bw.flush();
        //RELEASE RES
        bw.close();
        os.close();
        //resopns
        int responseCode=conn.getResponseCode();
        StringBuffer response =new StringBuffer();

        if(responseCode==conn.HTTP_OK)
        {
            InputStream is=conn.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            String line;
            if (br !=null)
            {
                while ((line=br.readLine()) !=null)
                {
                    response.append(line+"\n");
                }
            }else{
                return null;
            }

            br.close();
            is.close();

            return response.toString();


        }else
        {
            return String.valueOf(responseCode);
        }


    }catch(IOException e){
        e.printStackTrace();
    }
    return null;
}

}

//我的MyAdapter类

public class MyAdapter extends RecyclerView.Adapter<MyHolder> {

Context c;
ArrayList<String> name;
public MyAdapter(Context c, ArrayList<String> name){
    this.c=c;
    this.name=name;

}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,parent,false);
     MyHolder holder=new MyHolder(v);

    return holder;
}

@Override
public void onBindViewHolder(MyHolder holder, int position) {
    holder.nameTxt.setText(name.get(position));

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

} //我的MyHolder课程

public class MyHolder extends RecyclerView.ViewHolder {
TextView nameTxt;

public MyHolder(View itemView) {
    super(itemView);

    nameTxt = (TextView) itemView.findViewById(R.id.nameTxt);
}

}

最后是我的搜索课

public class Search extends AppCompatActivity {
RecyclerView rv;
SearchView sv;
ImageView nodataImg,noNetworkImg;

String urlAddress="http://10.0.2.2/shareknowledge/search.php";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);
    rv=(RecyclerView) findViewById(R.id.rv);
    rv.setLayoutManager(new LinearLayoutManager(this));

    sv=(SearchView)findViewById(R.id.sv);

    nodataImg=(ImageView) findViewById(R.id.nodataImg);
    noNetworkImg=(ImageView) findViewById(R.id.noserver);

    sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            SenderReciver sr=new SenderReciver(Search.this,query,urlAddress,rv,nodataImg,noNetworkImg);
            sr.execute();
            return false;
        }

        @Override
        public boolean onQueryTextChange(String query) {
            SenderReciver sr=new SenderReciver(Search.this,query,urlAddress,rv,nodataImg,noNetworkImg);
            sr.execute();

            return false;
        }
    });

}

}

即使我附上

,为什么会出现此错误
if(integer==1)
{
    rv.setAdapter(new MyAdapter(c,name));
}else{
    Toast.makeText(c,"Unable to pass",Toast.LENGTH_SHORT).show();
}

然后还有一部分是真正的PLZ帮助

0 个答案:

没有答案