2

我正在制作一个应用程序,其中我显示的人的状态为在线和离线。目前 ListView 是根据 api 设置的。我想根据此人的在线状态对 listView 进行排序。

例如。如果没有。的人在线,然后列表视图首先显示他们。

我在我的项目中实现了延迟加载图像。

这是我的 MainActivity.class

public class MainActivity extends Activity {

ProgressDialog dialog;

static final String URL = "Some_URL";
// XML node keys
static final String KEY_RESPONSE = "Response"; // parent node
static final String KEY_NAME = "Name";
static final String KEY_DESCRIPTION = "Description";
static final String KEY_ID = "ConsultantID";
static final String KEY_OFFLINE = "OnlineStatus";
static final String KEY_THUMB_URL = "ProfilePicture";

ListView lv;
LazyAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv=(ListView)findViewById(R.id.list);




    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


    XMLfunctions parser = new XMLfunctions();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_RESPONSE);
    // looping through all song nodes <song>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_DESCRIPTION, parser.getValue(e, KEY_DESCRIPTION));
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_OFFLINE, parser.getValue(e, KEY_OFFLINE));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

        // adding HashList to ArrayList
        songsList.add(map);
    }

     adapter=new LazyAdapter(this, songsList);
       lv.setAdapter(adapter);

       lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> Parent, View view, int Position,
                long Id) {
            // TODO Auto-generated method stub
            @SuppressWarnings("unchecked")
            HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(Position);
            Intent i = new Intent ("com.fedorvlasov.lazylist.SHOWLARGE");
            i.putExtra("ID",o.get(KEY_ID));
            i.putExtra("NAME", o.get(KEY_NAME));
            i.putExtra("DESCRIPTION",o.get(KEY_DESCRIPTION));
            i.putExtra("STATUS", o.get(KEY_OFFLINE));
            i.putExtra("IMAGE",o.get(KEY_THUMB_URL));
            startActivity(i);

            //Toast.makeText(LazyAdapter2.this, "position '" + adapter.getItem(Position) + "' was clicked.", Toast.LENGTH_LONG).show();



        }

    });

}

}

这是我的适配器类,名为 LazyAdater

public class LazyAdapter extends BaseAdapter {


private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}


public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return data.get(position);

}

public long getItemId(int position) {
    return position;
}



public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);


    TextView title = (TextView)vi.findViewById(R.id.title); // title name
    TextView description = (TextView)vi.findViewById(R.id.artist); // descriptiom
    TextView duration = (TextView)vi.findViewById(R.id.duration); // id
    TextView Status = (TextView)vi.findViewById(R.id.status); //status
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position); 

    // Setting all values in listview
    title.setText(song.get(MainActivity.KEY_NAME));
    description.setText(song.get(MainActivity.KEY_DESCRIPTION));
    Status.setText(song.get(MainActivity.KEY_OFFLINE));
    duration.setText(song.get(MainActivity.KEY_ID));
    imageLoader.DisplayImage(song.get(MainActivity.KEY_THUMB_URL), thumb_image);
    return vi;


}}

我已经通过一些比较器进行了审查.. 但我不了解如何实现它.. 在此先感谢..

4

1 回答 1

2

尝试这个

Collections.sort(songsList, new MyCustomComparator());

public static class MyCustomComparator implements Comparator<HashMap<String, String>> {

    @Override
    public int compare(HashMap<String, String> lhs,
            HashMap<String, String> rhs) {
        if(lhs.get("onLineCheck") is true)
            return 1;
        else
            return -1;

        return 0;
    }

}

在这条线之前

adapter=new LazyAdapter(this, songsList);
于 2012-07-12T10:33:53.500 回答