绑定与collections.sort不匹配

时间:2012-07-25 13:42:31

标签: java collections comparable

我有一个具有sqlite DB的应用程序,其内容被放入arraylist并可通过列表视图查看。我的问题是我无法按字母顺序获取该项目。我已实现Collections.sort()但收到绑定不匹配错误。任何想法

错误:

绑定不匹配:类型sort(List<T>)的通用方法Collections不适用于参数(ArrayList<LoginDetails>)。推断类型LoginDetails不是有界参数<T extends Comparable<? super T>>

的有效替代
private ListView loginList;
private Button webLogin;
private ListAdapter loginListAdapter;
private ArrayList<LoginDetails> loginArrayList;

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login_listview);


    loginList = (ListView)
    findViewById(R.id.loginlist);
    loginList.setOnItemClickListener(this);

    webLogin = (Button)
    findViewById(R.id.button3);
    webLogin.setOnClickListener(this);

    loginArrayList = new ArrayList<LoginDetails>();
    loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
    loginList.setAdapter(loginListAdapter);

    Collections.sort(loginArrayList ); //Bound mismatch error is here.
    }

LoginDetails:

public class LoginDetails {

private String sName;
private String wUrl;
private String uName;
private String pWord;

public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}

public String getwUrl() {
return wUrl;
}
public void setwUrl(String wUrl) {
this.wUrl = wUrl;
}

public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getpWord() {
return pWord;
}
public void setpWord(String pWord) {
this.pWord = pWord;
}

}

1 个答案:

答案 0 :(得分:4)

如API文档中所述,Collections.sort

  

列表中的所有元素都必须实现Comparable接口。

确保LoginDetails实施Comparable

例如:

public class LoginDetails implements Comparable<LoginDetails>{
    //...        

    @Override
    public int compareTo(LoginDetails another) {
        return uName.compareTo(another.uName);
    }

    //...