简体   繁体   中英

2 different layouts for the type of each row

I know that there are similar questions, but i couldn't really understand, so i decided to ask again.

I am trying to do something similar to a chat, so in the list view there would be 2 types of rows, the ones of the received items and the ones of the sent items. The ones that were received will inflate the recvRow.xml layout, and the ones that were sent will inflate the sentRow.xml layout.

I have a list of items that are suposed to be shown ( name, img, etc) and i have a boolean that represents if the object was sent or received.

The problem is that those layouts have different buttons and if for example i receive something first, it will obviously load the recvRow.xml , but then if I try to send something, it will throw a nullpointerexception because on one of the buttons that only exists in the sentRow.xml layout (and doesn't exist in the recvRow.xml).

Since i am checking that boolean that i told you guys before, i don't see the need to use the getItemViewType method.

Here is my code for the getView method:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    viewHolder = null;
    if (convertView == null) {

        viewHolder=new ViewHolder();
        LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        System.out.println("Transf Rec Adapter : Going to draw AN ITEM : "+ listOfItems.get(position).getAppName());
        System.out.println("Transf Rec Adapter : The received bool is : "+ listOfItems.get(position).isReceived());

        if(listOfItems.get(position).isReceived()){

            view=layoutInflater.inflate(R.layout.highway_transf_record_recv_row,parent,false);
     viewHolder.deleteFile=(ImageView) view.findViewById(R.id.transfRecRowDelete);
     viewHolder.installButton = (ImageView) view.findViewById(R.id.transfRecRowInstall);

        }else{//se enviado
            view=layoutInflater.inflate(R.layout.highway_transf_record_sent_row,parent,false);

        viewHolder.reSendButton=(ImageView) view.findViewById(R.id.transfRecReSendButton);

        }
        viewHolder.appImageIcon=(ImageView) view.findViewById(R.id.transfRecRowImage);
        viewHolder.appNameLabel=(TextView) view.findViewById(R.id.transfRecRowText);
        viewHolder.appVersionLabel = (TextView) view.findViewById(R.id.transfRecRowAppVersion);
        viewHolder.senderInfo = (TextView) view.findViewById(R.id.apkOrigin);
        viewHolder.rowImageLayout = (RelativeLayout) view.findViewById(R.id.transfRecRowImageLayout);
        viewHolder.appInfoLayout = (RelativeLayout) view.findViewById(R.id.appInfoLayout);


    }else{
        view= convertView;
        viewHolder = (ViewHolder) convertView.getTag();
    }



    if(listOfItems.get(position).isReceived()){
        System.out.println("INSIDE THE GET VIEW TRANSF REC ADAPTER :  RECEIVED SOMETHING");
        viewHolder.senderInfo.setText("You received this app :");


        myOnClickListenerToInstall = new MyOnClickListenerToInstall(position);
        viewHolder.installButton.setOnClickListener(myOnClickListenerToInstall);

        myOnClickListenerToDelete= new MyOnClickListenerToDelete(position);
        viewHolder.deleteFile.setOnClickListener(myOnClickListenerToDelete);

    }else{
        System.out.println("INSIDE THE GET VIEW TRANSF REC ADAPTER :  SENT SOMETHING");
        viewHolder.senderInfo.setText("You sent this app :");
        ReSendListener reSendListener=new ReSendListener(position);

        viewHolder.reSendButton.setOnClickListener(reSendListener);//null pointer exception here

    }
    viewHolder.appNameLabel.setText(listOfItems.get(position).getAppName());
    viewHolder.appImageIcon.setImageDrawable(listOfItems.get(position).getIcon());
    viewHolder.appVersionLabel.setText(listOfItems.get(position).getVersionName());

    view.setTag(viewHolder);

    return view;
}

The id's are right and everything exists on the viewHolder.

You need to use getItemViewType because of recycling. Otherwise you can try to recycle a received item into a non-received view, which will screw things up royally. However it is ok for getItemViewType to just return 0 or 1 based on that boolean. It doesn't need to be very complicated for 2 view types.

You should override getItemViewType() in your adapter. This method will indicate the type of view which should be inflated for each row. You also need to override getViewTypeCount() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM