我的这个类有几个属性,如下所示:
public class FileIn {
public String id;
public void setID(String id_) {
id = id_;
}
public String getID() {
return id;
}
...
}
有20个属性。 然后我将这些数据放在一个ArrayList中:
public ArrayList<FileIn> dfor_A = new ArrayList<FileIn>();
好吧,以后我需要获取一个元素的索引,但我知道id
dfor_A.get(-unknow index-).getID();
我如何搜索并获取索引?
答案 0 :(得分:0)
使用地图,例如HashMap中
Map <String, FileIn> map = new HashMap <> ();
map.put (fileIn.getID (), fileIn);
后
FileIn fileIn = map.get (fileIn.getID ());
如果你真的想保留一个ArrayList,那么你需要循环
for (int i = 0; i < dfor_A.length; i++) {
FileIn fi = dfor_A.get (i);
if (fi.getID().equals (id)) {
return i; // this is the index
}
}
答案 1 :(得分:0)
解决方案1: 我想你必须循环
public class loginFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {
final String TAG = this.getClass().getName();
Button btnLogin;
EditText etUsername, etPassword;
TextView tvRegister;
CheckBox cbRemember;
SharedPreferences pref;
SharedPreferences.Editor editor;
boolean checkFlag;
int x = 0;
public static final String PREFS_NAME = "MyPrefsFile";
String userId = "userID";
@Override
public void onStart() {
super.onStart();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_login, container, false);
etUsername = (EditText) v.findViewById(R.id.etFirstname);
etPassword = (EditText) v.findViewById(R.id.etPassword);
btnLogin = (Button) v.findViewById(R.id.btnLogin);
tvRegister = (TextView) v.findViewById(R.id.tvRegister);
cbRemember = (CheckBox) v.findViewById(R.id.cbRemember);
cbRemember.setOnCheckedChangeListener(this);
checkFlag = cbRemember.isChecked();
pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
editor = pref.edit();
final String username = pref.getString("username", "");
String password = pref.getString("password", "");
HashMap postData = new HashMap();
postData.put("username", username);
postData.put("password", password);
if (!username.equals("") && (!password.equals(""))) {
PostResponseAsyncTask task1 = new PostResponseAsyncTask(getActivity(), postData,
new AsyncResponse() {
@Override
public void processFinish(String s) {
if (s.contains("renter")) {
Log.d(TAG, s);
Toast.makeText(getActivity(), "Renter Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(getActivity(), RenterTabs.class);
startActivity(in);
getActivity().finish();
} else if (s.contains("owner")) {
Log.d(TAG, s);
Toast.makeText(getActivity(), "Owner Login Successful!", Toast.LENGTH_SHORT).show();
Intent in = new Intent(getActivity(), OwnerTabs.class);
startActivity(in);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Login Failed!", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/authenticate.php");
}
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HashMap postData = new HashMap();
postData.put("username", etUsername.getText().toString());
postData.put("password", MD5.encrypt(etPassword.getText().toString()));
PostResponseAsyncTask task1 = new PostResponseAsyncTask(getActivity(), postData,
new AsyncResponse() {
@Override
public void processFinish(String s) {
Log.d(TAG,s);
if (s.contains("renter")) {
if (checkFlag) {
editor.putString("username", etUsername.getText().toString());
editor.putString("password", MD5.encrypt(etPassword.getText().toString()));
editor.apply();
Log.d(TAG, pref.getString("password", ""));
}
Intent in = new Intent(getActivity(), RenterTabs.class);
startActivity(in);
getActivity().finish();
} else if (s.contains("owner")) {
if (checkFlag) {
editor.putString("username", etUsername.getText().toString());
editor.putString("password", MD5.encrypt(etPassword.getText().toString()));
editor.apply();
}
editor.putString("username", etUsername.getText().toString());
editor.putString("password", MD5.encrypt(etPassword.getText().toString()));
editor.apply();
Intent in = new Intent(getActivity(), OwnerTabs.class);
startActivity(in);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Login Failed!", Toast.LENGTH_SHORT).show();
}
}
});
task1.execute("http://carkila.esy.es/authenticate.php");
}
});
tvRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent in = new Intent(getActivity(), RegisterActivity.class);
startActivity(in);
}
});
return v;
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkFlag = isChecked;
Log.d(TAG, "checkflag: " + checkFlag);
}
}
解决方案2: 如果你考虑它的良好表现。我建议你使用高级功能
第1步:实施比较器
/*function perform operation dfor_A.indexOf(item.id);*/
public int getIndexOf(String id,List dfor_A)
{
for (int i = 0; i < dfor_A.size(); i++) {
FileIn fi = dfor_A.get (i);
if (fi.getID().equals (id)) {
return i; // this is the index
}
}
return -1;
}
第2步:对列表进行排序
public class FileInCustomComparator implements Comparator<FileIn> {
@Override
public int compare(FileIn fileIn1, FileIn fileIn2) {
return fileIn1.getId()-fileIn2.getId();//id consider as int here
}
}
步骤3:使用优化的内置算法搜索已排序列表
Collections.sort( for_A /*list here*/, new FileInCustomComparator());