Mon adaptateur.
public class customDecisionAdapter extends BaseAdapter {
Context context;
private ArrayList<String> list1;
private ArrayList<String> list2;
public customDecisionAdapter(Context context, ArrayList<String>list1, ArrayList<String>list2) {
this.context= context;
this.list1= list1;
this.list2= list2;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View convertView = view;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customedecisio,viewGroup,false);
}
TextView t1 = (TextView) convertView.findViewById(R.id.textDisease);
TextView t2 = (TextView) convertView.findViewById(R.id.textTotal);
// Verify value of position not greater than size of ArrayList.
if(position < list1.size())
t1.setText(list1.get(position));
if(position< list2.size())
t2.setText(list2.get(position));
return convertView;
}
@Override
public int getCount()
{
if(list1.size() < list2.size())
return list2.size();
else
return list1.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
}
4 Réponses :
Vous pouvez simplement utiliser définir code> car il se comporte plus de moins comme une liste mais n'autorise pas les doublons. public class customDecisionAdapter extends BaseAdapter {
Context context;
private Set<String> set = new TreeSet<String>;
public customDecisionAdapter(Context context, ArrayList<String>list1, ArrayList<String>list2) {
this.context= context;
this.set.addAll(list1);
this.set.addAll(list2);
}
// rest of the code
Je ne pense pas que vous comprenez la question de l'OP. En outre, un hashset perd l'ordre des articles
essayez sous la solution ci-dessous. strong> Je pense qu'il y a un problème avec vos deux conditions.
Ici vous ne gérez que si elle est partie. vous devriez gérer une partie d'autre aussi. p>
S'il vous plaît ajouter une explication de ce que vous avez fait et pourquoi il corrige le problème
Il se bloque: (((
Envoyez-moi votre rapport d'accident. BCZ, ce n'est pas possible.
@VishruTmavani désolé, j'écris un mauvais endroit. Ça marche. Merci: 3
Vous pouvez utiliser la méthode de l'arraylist et getter personnalisées, à compter de cette option. N'utilisez pas deux arraylistes distincts. Faire une classe modèle comme ci-dessous: -
public class Model {
private String Disease;
private String Total;
public Model(String disease, String total) {
Disease = disease;
Total = total;
}
public String getDisease() {
return Disease;
}
public void setDisease(String disease) {
Disease = disease;
}
public String getTotal() {
return Total;
}
public void setTotal(String total) {
Total = total;
}
}
Use List<Model> list = new Arraylist<>();
Désolé, je ne comprends pas. Mais j'ai deux classes de modèle Liste privée
Ce n'est pas un bon moyen d'utiliser deux arraylistes. Il devrait y avoir une arraylist et vous pouvez personnaliser votre arraylist en définissant la classe modèle.
**You should be using LinkedHashSet if you want to avoid duplicates and maintain order.**
//Your Wrapper Class
public class Model {
private String Disease;
private String Total;
public Model(String disease, String total) {
Disease = disease;
Total = total;
}
public String getDisease() {
return Disease;
}
public void setDisease(String disease) {
Disease = disease;
}
public String getTotal() {
return Total;
}
public void setTotal(String total) {
Total = total;
}
}
//Your Adapter
public class customDecisionAdapter extends BaseAdapter {
Context context;
private LinkedHashSet<Model> list1;
public customDecisionAdapter(Context context, LinkedHashSet<Model> list1) {
this.context= context;
this.list1= list1;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
View convertView = view;
if(convertView==null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.customedecisio,viewGroup,false);
}
TextView t1 = (TextView) convertView.findViewById(R.id.textDisease);
TextView t2 = (TextView) convertView.findViewById(R.id.textTotal);
// Verify value of position not greater than size of ArrayList.
t1.setText(list1.get(position).getDisease());
t2.setText(list1.get(position).getTotal());
return convertView;
}
@Override
public int getCount()
{
return list1.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
}
// Inside your activity onCreate method
LinkedHashSet<Model> listset = new LinkedHashSet<Model>();
listset.add(new Model("Malaria","200"));
listset.add(new Model("Cold","200"))
...//add more items here
//Then call your adapter and pass this list
customDecisionAdapter mAdapter = new customDecisionAdapter(listset,this);
Je suis désolé. Doit être 2 arraylist.
Puis-je savoir pourquoi vous utilisez 2 arraylist?