2012-07-12 39 views
2

我正在製作一個應用程序,在該應用程序中,我將其狀態顯示爲ONLINE和OFFLINE的人數爲no。 目前ListView是根據api設置的。我想根據該人的在線狀態對listView進行排序。如何使用自定義適配器在列表視圖中進行排序

for ex。如果沒有。的人在線,然後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; 


}} 

我已經被一些COMPARETOR審查..但林不understading如何實現它.. 在此先感謝..

+0

之前使用樹形圖,它已經被排序,直到除非你需要任何特定類型的排序。 – Programmer 2012-07-12 09:52:20

+0

我沒有得到你...請你詳細說明它更多.. – 2012-07-12 10:17:28

回答

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); 
+0

這工作正常..但有一點校正.. – 2012-07-12 10:54:54

+0

但應用後,我沒有得到所有在線人在一起...我得到一個在線然後離線然後再次在線就這樣..我想推測所有的在線peopple第一 – 2012-07-12 10:55:47

+0

測試我的應用程序後,我發現在應用您的代碼後,列表視圖中的人民的立場剛剛翻轉.. 我的意思是第一個去上一個位置,反之亦然。 – 2012-07-12 11:01:39

相關問題