Bad0
Bad0

Reputation: 133

Custom ListView Row Android

i checked previous answers and documents about this topic but i can't adapt it to my situation. I have two arraylists full of my special classes objects. I have two different type of rows in xml. I want to create two different rows and fill each of it with related arraylists items in my Adapter Class. Rough code:

public class KanalAdapter extends BaseAdapter {

Context context;
ArrayList<OfficialKanal> officialKanals;
ArrayList<NormalKanal> normalKanals;
LayoutInflater lala;
public KanalAdapter(Context context , ArrayList<OfficialKanal> officiallar, ArrayList<NormalKanal> normaller){
    this.context = context;
    officialKanals = officiallar;
    Log.i("tago" , "tagtag");
    normalKanals = normaller;
    lala = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return normalKanals.size()+officialKanals.size();
}

public Object getItem(int i) {
    return ??
}

public long getItemId(int i) {
    return ??
}

@Override
public int getItemViewType(int position) {
    return ??
}

@Override
public int getViewTypeCount() {
    return 2;
}

public View getView(int position, View convertView, ViewGroup viewGroup) {
    KanalHolder holder;
    int type = getItemViewType(position);
    if(convertView==null){
        holder = new KanalHolder();
        switch (type){
            case 0:
                convertView = lala.inflate(R.layout.normalkanal,null);
                holder.image1 = (ImageView) convertView.findViewById(R.id.imageView5);
                holder.tv1 = (TextView) convertView.findViewById(R.id.textView4);
                Log.i("tago", "tagtagtag");
                holder.tv2 = (TextView) convertView.findViewById(R.id.textView8);
                holder.buton1=(Button) convertView.findViewById(R.id.button8);
                break;
            case 1:

                break;

        }
        convertView.setTag(holder);
    }else{
        holder = (KanalHolder)convertView.getTag();
    }
    holder.tv1.setText(normalKanals.get(position).getKanaladi());
        holder.image1.setImageResource(R.mipmap.aliprof);
    return convertView;
}


static class KanalHolder{
    public ImageView image1;
    public TextView tv1 , tv2;
    public Button buton1;
}

I can handle getView part someway but how should i use other methods ??

Upvotes: 0

Views: 31

Answers (2)

Ozgur
Ozgur

Reputation: 3766

Here is solution.

First, put all of Kanal objects to one ArrayList. There is no need for two ArrayList. Also make it's type Object.

I didn't test it but this should work for you, or at least you will get the idea.

ArrayList<Object> kanals = new ArrayList<>();
int OFFICIAL_KANAL = 1, NORMAL_KANAL = 2;

public KanalAdapter(Context context , ArrayList<OfficialKanal> officiallar, ArrayList<NormalKanal> normaller){

    ArrayList<Object> kanals = new ArrayList<>();

    for (int i = 0; i < officiallar.size(); i++) {
        kanals.add(officiallar.get(i));
    }

    for (int i = 0; i < normaller.size(); i++) {
        kanals.add(normaller.get(i));
    }

}

public Object getItem(int i) {
    return kanals.get(i);
}

public long getItemId(int i) {
    return 0;
}

@Override
public int getItemViewType(int position) { 
   //here is the main logic
   //android will take care of listview 

    Object item = getItem(position);
    if(item instanceof NormalKanal)
       return NORMAL_KANAL;
    if(item instanceof OfficialKanal)
       return OFFICIAL_KANAL;

    return -1; 
}

@Override
public int getViewTypeCount() {
    return 2; //because we have two different views.
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
    KanalHolder holder;
    Object currentKanal = getItem(position);
    if(convertView==null){

       holder = new KanalHolder();

       if(currentKanal instanceof NormalKanal) {
         //inflate
       }

       if(currentKanal instanceof NormalKanal) {
         //inflate etc..
       }

       convertView.setTag(holder);

    }else{
        holder = (KanalHolder)convertView.getTag();
    }

    if(currentKanal instanceof NormalKanal) {
         //setText or setImage etc..
    }

    if(currentKanal instanceof NormalKanal) {
         //setText or setImage etc..
    }

    return convertView;
}

Upvotes: 1

Ahmed Gamal
Ahmed Gamal

Reputation: 338

You can extend ArrayAdapter<Object> and pass one array list that contains objects of the two items you have then you won't have to override these methods array adapter handle these for you.

I recommend to create abstract class that suites your two classes and make them implement that abstract so you can extend ArrayAdapter<Your Abstract> instead of using Object.

And check this question : Android ListView with different layouts for each row

getview will look like that then "This snippet is from the link in the answer above check it for more details

@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; int type = getItemViewType(position); System.out.println("getView " + position + " " + convertView + " type = " + type); if (convertView == null) { holder = new ViewHolder(); switch (type) { case TYPE_ITEM: convertView = mInflater.inflate(R.layout.item1, null); holder.textView = (TextView)convertView.findViewById(R.id.text); break; case TYPE_SEPARATOR: convertView = mInflater.inflate(R.layout.item2, null); holder.textView = (TextView)convertView.findViewById(R.id.textSeparator); break; } convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } holder.textView.setText(mData.get(position)); return convertView; }   }

Upvotes: 0

Related Questions