Can you populate a RecyclerView with two xml adapters with different structure?

3

I'm reading two xml with two different structures, I can populate a RecyclerView with the two adapters of each xml.

In this picture you can see what I want to do.

Now I have the files that are for a parser xml, a class for each xml, a parser for each, an adapter for each one, what I do not know, if you can join them in the fragment class where I load the data and the charge in the recyvlerView.

This is the fragment code I was using but I get an error and the application is closed

public class fqsomos  extends Fragment{
private RecyclerView reciclador,recicladorNosotros;
private LinearLayoutManager layoutManager;
private adaptadorQsomos adaptador;
private adaptadorNosotros adaptadorNosotros;
private CircularProgressView loader ;
private SwipeRefreshLayout refreshLayout;
ProgressDialog loading = null;
private ProgressBar spinner;
private ProgressBar spinnerNosotros;
  private final static String URL ="http://svconstructions.com.ec/sv/qsomosapp.php";
private final static String URL_nosotros ="http://svconstructions.com.ec/sv/qsomosapp.php";
private static final String LOGTAG = "LogsProyectos";
final Activity activity = getActivity();
public fqsomos() {
    // Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.lqsomos, container, false);
    reciclador = (RecyclerView) view.findViewById(R.id.reciclador);
    recicladorNosotros = (RecyclerView) view.findViewById(R.id.recicladorNosotros);
    layoutManager = new LinearLayoutManager(getActivity());

    reciclador.setLayoutManager(layoutManager);
    recicladorNosotros.setLayoutManager(layoutManager);

    spinner = (ProgressBar)view.findViewById(R.id.progressbarqsomos);
    spinnerNosotros = (ProgressBar)view.findViewById(R.id.progressbarNosotros);
    adaptador = new adaptadorQsomos();
    adaptador.setHasStableIds(true);
    //spinner.setVisibility(View.GONE );
    reciclador.setAdapter(adaptador);
//    recicladorNosotros.setAdapter(adaptadorNosotros);
    new TareaDescargaXml_Qsomos().execute(URL);
   // new TareaDescargaXml_Nosotros().execute(URL_nosotros);
    return view;
}
public class TareaDescargaXml_Qsomos extends AsyncTask<String, Void, List<qsomo>> {

    @Override
    protected void onPreExecute() {
        // show the progress bar
        //    spinner.setVisibility(View.VISIBLE);

    }
    @Override
    protected List<qsomo> doInBackground(String... urls) {
        try {
            return parsearXmlDeUrl(urls[0]);
        } catch (IOException e) {
            Log.e( LOGTAG,"Error en la red",e);
            return null; // null si hay error de red
        } catch (XmlPullParserException e) {
            Log.e( LOGTAG,"Error al leer xml",e);
            return null; // null si hay error de parsing XML
        }
    }

    @Override
    protected void onPostExecute(List<qsomo> result) {
        spinner.setVisibility(View.GONE );
        // Actualizar contenido del proveedor de datos
        qsomo.Ultimas_qsomos = result;
        // Actualizar la vista del adaptador
        adaptador.notifyDataSetChanged();

    }
}

private List<qsomo> parsearXmlDeUrl(String urlString)
        throws XmlPullParserException, IOException {
    InputStream stream = null;
    parseQsomos parserXml = new parseQsomos();
    List<qsomo> entries = null;

    try {
        stream = descargarContenido(urlString);
        entries = parserXml.parsear(stream);

    } finally {
        if (stream != null) {
            stream.close();
        }
    }

    return entries;
}


// nosotros
//***************--------------------------------------*******************
public class TareaDescargaXml_Nosotros extends AsyncTask<String, Void, List<nosotro>> {

    @Override
    protected void onPreExecute() {
        // show the progress bar
        //    spinner.setVisibility(View.VISIBLE);

    }
    @Override
    protected List<nosotro> doInBackground(String... urls) {
        try {
            return parsearXmlDeUrlNosotros(urls[0]);
        } catch (IOException e) {
            Log.e( LOGTAG,"Error en la red",e);
            return null; // null si hay error de red
        } catch (XmlPullParserException e) {
            Log.e( LOGTAG,"Error al leer xml",e);
            return null; // null si hay error de parsing XML
        }
    }

    @Override
    protected void onPostExecute(List<nosotro> result) {
        spinnerNosotros.setVisibility(View.GONE );
        // Actualizar contenido del proveedor de datos
        nosotro.Listado_nosotros = result;
        // Actualizar la vista del adaptador
        adaptadorNosotros.notifyDataSetChanged();

    }
}

private List<nosotro> parsearXmlDeUrlNosotros(String urlString)
        throws XmlPullParserException, IOException {
    InputStream stream = null;
    parseNosotros parserXml = new parseNosotros();
    List<nosotro> entries = null;

    try {
        stream = descargarContenido(urlString);
        entries = parserXml.parsear(stream);

    } finally {
        if (stream != null) {
            stream.close();
        }
    }

    return entries;
}

private InputStream descargarContenido(String urlString) throws IOException {
    java.net.URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Iniciar la petición
    conn.connect();
    return conn.getInputStream();
}

}

and in the Layout of this fragment I have two recyclerView.

    
asked by Alldesign Web 25.08.2016 в 00:11
source

1 answer

3

If you are looking to make a "list" with different elements, the first thing you should do is create an interface

public interface Item {
    int getViewType();
}

Following this we create the objects that represent our elements, to exemplify use Item1 and Item2, both must implement the Item interface.

Item1

public class Item1 implements Item {
    private String text;

    public Item1(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Override
    public int getViewType() {
        return 1;
    }
}

Item2

public class Item2 implements Item {
    private String text;

    public Item2(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
    @Override
    public int getViewType() {
        return 2;
    }
}

Now we create the adapter for the RecyclerView, which will serve us for both views, whose Holders are declared inside it, it is important to pay attention in the getViewType method since that is where we get the type of Item to display

HetAdapter

public class HetAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private final int ITEM1 = 1;
private final int ITEM2 = 2;

private List<Item> items = new ArrayList<>();

public HetAdapter(List<Item> items) {
    this.items = items;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    RecyclerView.ViewHolder viewHolder;
    switch (viewType){
        case ITEM1: viewHolder = new Item1Holder(inflater.inflate(R.layout.item_1,parent,false));
            break;
        case ITEM2: viewHolder = new Item2Holder(inflater.inflate(R.layout.item_2,parent,false));
            break;
        default: viewHolder = new Item1Holder(inflater.inflate(R.layout.item_1,parent));
    }
    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (getItemViewType(position)){
        case ITEM1:
            Item1 item1 = (Item1) items.get(position);
            Item1Holder item1Holder = (Item1Holder)holder;
            item1Holder.text.setText(item1.getText());
            break;
        case ITEM2:
            Item2 item2 = (Item2) items.get(position);
            Item2Holder item2Holder = (Item2Holder)holder;
            item2Holder.text.setText(item2.getText());
            break;
    }

}

@Override
public int getItemViewType(int position) {
    return items.get(position).getViewType();
}

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

class Item1Holder extends RecyclerView.ViewHolder{
    TextView text;
    public Item1Holder(View itemView) {
        super(itemView);
        text = (TextView)itemView.findViewById(R.id.text);
    }
}

class Item2Holder extends RecyclerView.ViewHolder{
    TextView text;
    public Item2Holder(View itemView) {
        super(itemView);
        text = (TextView)itemView.findViewById(R.id.text);
    }
}
}

Now we only have to populate the RecyclerView

public class MainActivity extends AppCompatActivity {

private RecyclerView.LayoutManager layoutManager;
private RecyclerView recyclerView;
private HetAdapter adapter;
private List<Item> items = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    items.add(new Item1("ITEM_1_1"));
    items.add(new Item2("ITEM_2_1"));
    items.add(new Item1("ITEM_1_2"));
    items.add(new Item2("ITEM_2_2"));
    items.add(new Item1("ITEM_1_3"));
    recyclerView = (RecyclerView)findViewById(R.id.recycler);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this);
    adapter = new HetAdapter(items);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
}
}

And the result

    
answered by 31.08.2016 / 05:20
source