RecyclerView与本地主机一起使用,但不显示在线服务器中的项目

时间:2018-08-21 23:12:03

标签: android android-recyclerview

大家好我的问题很奇怪。我有一个可以与排球和MySQL配合使用的应用程序。当我使用xampp启动应用程序时,它运行良好。该列表保存在适配器中,并且适配器在“回收者”视图中运行良好。但是,当我将网址更改为托管在我的网站上的mysql数据库时,recycler视图不会显示数据。正确接收了JSON数据并将其正确保存在Arraylist中(请与Toast消息一起检查)。我在真实设备中有同样的问题 Android Studio不会向我抛出任何错误或警告

Rubros

public class Rubros {
    String nombre;
    String rubro_id;

    public Rubros(String nombre, String rubro_id) {
        this.nombre = nombre;
        this.rubro_id = rubro_id;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getRubro_id() {
        return rubro_id;
    }

    public void setRubro_id(String rubro_id) {
        this.rubro_id = rubro_id;
    }
}

ListaRubros.class

public class ListaRubros extends AppCompatActivity {

RecyclerView rv_rubros;
RubroAdapter adapter;
List <Rubros> listarubros;
String categoria;
String categoria_id;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_rubros);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //Buscamos la categoria
    Bundle bundle = getIntent().getExtras();
    categoria = bundle.getString("categoria");
    setTitle(categoria);
    categoria_id = bundle.getString("categoria_id");

    //Declaramos variable textview
    listarubros= new ArrayList<>();
    //cargamos la lista de rubros
    rv_rubros = (RecyclerView) findViewById(R.id.rv_rubros);
    //Preparamos el adapter
    rv_rubros.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    rv_rubros.setLayoutManager(llm);
    //guardamos la lista en adapter
    cargarRubros(categoria_id);
    adapter = new RubroAdapter(listarubros);
    rv_rubros.setAdapter(adapter);
    //agregamos el onClick Listener
    adapter.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String rubro=listarubros.get(rv_rubros.getChildAdapterPosition(view)).getNombre();
            String rubro_id=listarubros.get(rv_rubros.getChildAdapterPosition(view)).getRubro_id();
            Intent intent = new Intent(ListaRubros.this,ListaNegocios.class);
            intent.putExtra("rubro",rubro);
            intent.putExtra("rubro_id",rubro_id);
            startActivity(intent);
        }
    });

}

public void cargarRubros(String categoria){

        RequestQueue queue = Volley.newRequestQueue(this);
        String url ="http://10.0.3.2/listin/plantilla/sql/vistarubros_categoria_id.php?cc="+categoria; //Creamos la url

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                response = response.replace("][",",");


                if (response.length()>0){
                    try {
                        JSONArray ja = new JSONArray(response);
                        for(int i=0;i<ja.length();i+=2){
                            try {
                                Rubros rubro = new Rubros(ja.getString(i),ja.getString(i+1));
                                listarubros.add(rubro);
                                Toast.makeText(getApplicationContext(),listarubros.get(0).getNombre(), Toast.LENGTH_SHORT).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    queue.add(stringRequest);
}

}

RubrosAdapter

public class RubroAdapter extends RecyclerView.Adapter<RubroAdapter.DatosRubroViewHolder>
    implements View.OnClickListener{

List<Rubros> listarubros;
private View.OnClickListener listener;

public RubroAdapter(List<Rubros> listarubros) {
    this.listarubros = listarubros;
}

@NonNull
@Override
public RubroAdapter.DatosRubroViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.vista_rv_rubros,parent,false);
    v.setOnClickListener(this);
    DatosRubroViewHolder holder = new DatosRubroViewHolder(v);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull RubroAdapter.DatosRubroViewHolder holder, int position) {
    holder.rubro.setText(listarubros.get(position).getNombre());
    holder.rubro_id.setText(listarubros.get(position).getRubro_id());
}

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

public void setOnClickListener(View.OnClickListener listener){
    this.listener=listener;
}

@Override
public void onClick(View view) {
    if(listener!=null){
        listener.onClick(view);
    }
}

public static class DatosRubroViewHolder extends RecyclerView.ViewHolder{
    TextView rubro;
    TextView rubro_id;
    public DatosRubroViewHolder(View itemView){
        super(itemView);
        rubro = (TextView) itemView.findViewById(R.id.tv_rubro);
        rubro_id = (TextView) itemView.findViewById(R.id.tv_rubro_id);
    }
}

}

编辑 我已经解决了问题。我忘了添加adapter.notifyDataSetChanged();将数据保存在arraylist中之后

0 个答案:

没有答案