我是Android的新手,并尝试在我的 listview 上创建一个带有搜索功能的联系人应用程序。当我搜索联系人时,它过滤正确并仅向我显示我搜索过的联系人,但当我尝试编辑/删除时,联系它会抛出 database / CursorIndexOutOfBoundsException 以及我用作手机超级联系的 textview 也不起作用...当我不搜索一切正常时(编辑,删除,来自文本视图等的电话)。我发布的带有搜索过滤器的类,如果你需要更多代码让我知道。我认为适配器没有显示正确的 listview 或类似的东西。 很抱歉提前感谢大帖子。
public class Promitheutis extends Activity implements SearchView.OnQueryTextListener {
Button add_btn;
ListView Contact_listview;
ArrayList<Contact> contact_data = new ArrayList<Contact>();
Contact_Adapter cAdapter;
DatabaseHandler db;
String Toast_msg;
SearchView search_view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
Contact_listview = (ListView) findViewById(R.id.list);
Contact_listview.setItemsCanFocus(false);
add_btn = (Button) findViewById(R.id.add_btn);
search_view = (SearchView) findViewById(R.id.search_view);
Set_Referash_Data();
search_view.setOnQueryTextListener(this);
} catch (Exception e) {
// TODO: handle exception
Log.e("some error", "" + e);
}
add_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent add_user = new Intent(Promitheutis.this,
Add_Update_User.class);
add_user.putExtra("called", "add");
add_user.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(add_user);
finish();
}
});
}
public void Set_Referash_Data() {
contact_data.clear();
db = new DatabaseHandler(this);
ArrayList<Contact> contact_array_from_db = db.Get_Contacts();
for (int i = 0; i < contact_array_from_db.size(); i++) {
int tidno = contact_array_from_db.get(i).getID();
//String name = contact_array_from_db.get(i).getName();
String epitheto = contact_array_from_db.get(i).getEpitheto();
String mobile = contact_array_from_db.get(i).getPhoneNumber();
// String email = contact_array_from_db.get(i).getEmail();
String eidos = contact_array_from_db.get(i).getEidos();
Contact cnt = new Contact();
cnt.setID(tidno);
//cnt.setName(name);
cnt.setEpitheto(epitheto);
//cnt.setEmail(email);
cnt.setEidos(eidos);
cnt.setPhoneNumber(mobile);
contact_data.add(cnt);
}
db.close();
cAdapter = new Contact_Adapter(Promitheutis.this, R.layout.listview_row,
contact_data);
Contact_listview.setAdapter(cAdapter);
cAdapter.notifyDataSetChanged();
}
public void Show_Toast(String msg) {
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
Set_Referash_Data();
}
public class Contact_Adapter extends ArrayAdapter<Contact> implements Filterable {
ValueFilter valueFilter;
Context context;
Activity activity;
int layoutResourceId;
Contact user;
ArrayList<Contact> data;
ArrayList<Contact> filterdata;
public Contact_Adapter(Activity act, int layoutResourceId,
ArrayList<Contact> data) {
super(act, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.activity = act;
this.data = data;
filterdata = data;
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Contact getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return data.indexOf(getItem(position));
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
//holder.name = (TextView) row.findViewById(R.id.user_epitheto_txt);
holder.epitheto = (TextView) row.findViewById(R.id.user_epitheto_txt);
//holder.email = (TextView) row.findViewById(R.id.user_email_txt);
holder.eidos = (TextView) row.findViewById(R.id.user_eidos_txt);
holder.number = (TextView) row.findViewById(R.id.user_mobile_txt);
holder.edit = (Button) row.findViewById(R.id.btn_update);
holder.delete = (Button) row.findViewById(R.id.btn_delete);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
user = data.get(position);
holder.edit.setTag(user.getID());
holder.delete.setTag(user.getID());
//holder.name.setText(user.getName());
holder.epitheto.setText(user.getEpitheto());
//holder.email.setText(user.getEmail());
holder.eidos.setText(user.getEidos());
holder.number.setText(user.getPhoneNumber());
// String url = holder.number.getText().toString();
//final Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
final String phone_no= holder.number.getText().toString().replaceAll("-", "");
holder.number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+phone_no));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
}
});
holder.edit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Edit Button Clicked", "**********");
Intent update_user = new Intent(activity,
Update_User.class);
update_user.putExtra("called", "update");
update_user.putExtra("USER_ID", v.getTag().toString());
activity.startActivity(update_user);
}
});
holder.delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
// TODO Auto-generated method stub
// show a message while loader is loading
AlertDialog.Builder adb = new AlertDialog.Builder(activity);
adb.setTitle("Διαγραφή;");
adb.setMessage("Είστε σίγουρος για τη διαγραφή; ");
final int user_id = Integer.parseInt(v.getTag().toString());
adb.setNegativeButton("Άκυρο", null);
adb.setPositiveButton("Ok",
new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// MyDataObject.remove(positionToRemove);
DatabaseHandler dBHandler = new DatabaseHandler(
activity.getApplicationContext());
dBHandler.Delete_Contact(user_id);
Promitheutis.this.onResume();
}
});
adb.show();
}
});
return row;
}
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
// TODO Auto-generated method stub
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Contact> filterList = new ArrayList<Contact>();
for (int i = 0; i < filterdata.size(); i++) {
if ( (filterdata.get(i).getEpitheto().toUpperCase() )
.contains(constraint.toString().toUpperCase())) {
Contact cnt = new Contact(filterdata.get(i)
.getEpitheto() , filterdata.get(i)
.getPhoneNumber() , filterdata.get(i)
.getEidos());
filterList.add(cnt);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = filterdata.size();
results.values = filterdata;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
// TODO Auto-generated method stub
data = (ArrayList<Contact>) results.values;
notifyDataSetChanged();
}
}
class UserHolder {
TextView name;
TextView epitheto;
TextView email;
TextView number;
TextView stathero;
TextView eidos;
TextView eponimia_user;
TextView afm;
TextView eidos2;
TextView eidos3;
Button edit;
Button delete;
}
}
@Override
public boolean onQueryTextSubmit(String query) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// TODO Auto-generated method stub
cAdapter.getFilter().filter(newText);
return false;
}
}