0

I have listviewadapter , it contains some fields like : status, notes, salesname, insertdate (txtTgglInsert), image. I want to do sorting based insertdate(txtTgglInsert) descending(based on recent status). How could I do this one on my programm with calling this adapter?

my adapter :

import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewTimelineAdapter extends BaseAdapter{
    private static ArrayList<URLPostClass> DataProcessorResult;
    private LayoutInflater myInflater;
    Context mycontext;

    public ListViewTimelineAdapter(Context context, ArrayList<URLPostClass> results) {
        DataProcessorResult = results;
        myInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        //Log.d("count", String.valueOf(DataProcessorResult.size()));
        return DataProcessorResult.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return DataProcessorResult.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }   

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) 
    { 
        final ViewHolder holder; 
        final Context mycontext=parent.getContext();       

        if(convertView == null) {
            convertView = myInflater.inflate(R.layout.custom_viewtimeline, null);
            holder = new ViewHolder();
            holder.txtJenisStatus = (TextView) convertView.findViewById(R.id.jenisstatus);
            holder.txtStatus = (TextView) convertView.findViewById(R.id.status);
            holder.txtSales = (TextView) convertView.findViewById(R.id.sales);
            holder.txtTgglInsert = (TextView) convertView.findViewById(R.id.tgglinsert);

            holder.imgPosting= (ImageView) convertView.findViewById(R.id.imgposting);
            convertView.setTag(holder);            
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtJenisStatus.setText(DataProcessorResult.get(position).getJenisStatus());
        holder.txtStatus.setText(DataProcessorResult.get(position).getRemark());
        holder.txtSales.setText(DataProcessorResult.get(position).getNamaSales());
        holder.txtTgglInsert.setText(DataProcessorResult.get(position).getTgglInsert());

        holder.txtStatus.setTextSize(TypedValue.COMPLEX_UNIT_PX, mycontext.getResources().getDimensionPixelSize( R.dimen.lbltitlelistviewitem));
        holder.txtJenisStatus.setTextSize(TypedValue.COMPLEX_UNIT_PX, mycontext.getResources().getDimensionPixelSize( R.dimen.lbllistviewitem));
        holder.txtSales.setTextSize(TypedValue.COMPLEX_UNIT_PX, mycontext.getResources().getDimensionPixelSize( R.dimen.lbllistviewitem));
        holder.txtTgglInsert.setTextSize(TypedValue.COMPLEX_UNIT_PX, mycontext.getResources().getDimensionPixelSize( R.dimen.lbllistviewitem));

        try {
            String image_url = DataProcessorResult.get(position).getPath();

            if (image_url.length()>0) {
                ImageLoader imgLoader = new ImageLoader(mycontext); 
                imgLoader.DisplayImage(image_url, 0, holder.imgPosting);
                holder.imgPosting.getLayoutParams().width= mycontext.getResources().getDimensionPixelSize( R.dimen.photolistviewitem);
                holder.imgPosting.getLayoutParams().height= mycontext.getResources().getDimensionPixelSize( R.dimen.photolistviewitem);             
            }else {
                holder.imgPosting.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            //Toast.makeText(mycontext, e.toString(), Toast.LENGTH_SHORT).show();
        }

        //if (position % 2 == 1) {convertView.setBackgroundColor(Color.WHITE);} else {convertView.setBackgroundColor(Color.rgb(208,212,208)); }        
        return convertView;
    } 
    static class ViewHolder { 
        TextView txtJenisStatus;
        TextView txtStatus;
        TextView txtSales;
        TextView txtTgglInsert;
        ImageView imgPosting;
    }

}
Agoeng Putz
  • 19
  • 1
  • 8

2 Answers2

1

Since your Adapter is based on an ArrayList of objects, you can change your implementation to subclass ArrayAdapter (Docs: http://developer.android.com/reference/android/widget/ArrayAdapter.html)

public class ListViewTimelineAdapter extends ArrayAdapter<URLPostClass> {

after that, sorting would be trivial:

ArrayList<URLPostClass> myListData = ...
ArrayAdapter<URLPostClass> adapter = new MyArrayAdapter<URLPostClass>(context, R.layout.my_layout, myListData);
adapter.sort(new Comparator<URLPostClass>() {

    @Override public int compare(URLPostClass lhs, URLPostClass rhs) {
        return rhs.getTgglInsert().compareTo(lhs.getTgglInsert()); // flipped for reverse order
    }

});
myListView.setAdapter(adapter);
sddamico
  • 2,130
  • 16
  • 13
  • so the compiler can sort it automatically ? without conversion date to microseconds, because in PHP, I need to convert it – Agoeng Putz Jul 15 '14 at 04:30
  • What do you mean by "convert it"? In Java, the `Date` class already implements the `Comparable` interface so therefore `Date` already implements the `compareTo()` method. – sddamico Jul 15 '14 at 16:18
  • rhs.getTgglInsert return String value – Agoeng Putz Jul 16 '14 at 03:52
  • Yes, if the value is a String you'll most likely want to convert it to a type that makes sense for what it represents. – sddamico Jul 17 '14 at 02:00
0

Considering that you've got a Date field your Comaparator class would be like this

public class CustomDateComparator implements Comparator<DataProcessorResult> {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("dd MMM yyyy");


@Override
public int compare(DataProcessorResult lhs, DataProcessorResult rhs) {
    // TODO Auto-generated method stub
    Date lhsDate = null;
    Date rhsDate = null;

    try {
        lhsDate = simpleDateFormat.parse(lhs.getTgglInsert());
        rhsDate = simpleDateFormat.parse(rhs.getTgglInsert());
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if(lhsDate == rhsDate) {
        return lhs.getNamaSales().compareToIgnoreCase(rhs.getNamaSales());
    }else {
        return lhsDate.compareTo(rhsDate);
    }
    }
}

And then you can do this

Collections.sort(results, new CustomDateComparator());

just before you pass it to your custom adapter class.

Hope this helps you.

VikramV
  • 1,111
  • 2
  • 13
  • 31
  • Hii VikramV, i got error unparseable both of simpleDateFormat.parse(lhs.getTgglInsert()) and simpleDateFormat.parse(rhs.getTgglInsert()); – Agoeng Putz Jul 16 '14 at 08:11