我有一个位置列表视图,我希望用户从中选择。
在他们选择一个项目后,我希望应用程序根据该位置返回特定条目。因为我想在ListView中显示一组信息,我使用的是适配器;在结果列表中,我设置为将数组作为包传递:
//when listItem is clicked
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
//in checked, in listview get Item positions for checked items
SparseBooleanArray checked = l.getCheckedItemPositions();
//assign selected items to new String ArrayList
List<String> selectedItem = new ArrayList<String>();
//determine size of items checked (number)
for (int i = 0; i < checked.size(); i++) {
// Item position in adapter
position = checked.keyAt(i);
// Add location if it is checked i.e.) == TRUE!
if (checked.valueAt(i))
//add selected items to madapter
selectedItem.add(madapter.getItem(position));
}
//create string output array, store new string array with selected items
String[] outputStrArr = new String[selectedItem.size()];
for (int i = 0; i < selectedItem.size(); i++) {
outputStrArr[i] = selectedItem.get(i);
}
Intent intent = new Intent(getApplicationContext(),
CustomMidwifeAdapter.class);
// Create a bundle object
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
// Add the bundle to the intent.
intent.putExtras(b);
Intent intentstart = new Intent(getApplicationContext(),
MidwifeResultList.class);
// start the ResultActivity
startActivity(intentstart);
}
正如您所看到的,我的直觉是将数据传递给适配器,因为这是我查询Parse数据的地方:
public class CustomMidwifeAdapter extends ParseQueryAdapter<ParseObject> {
public static final String TAG = CustomMidwifeAdapter.class.getSimpleName();
protected Context mContext;
protected List<ParseUser> mUsers;
protected ParseRelation<ParseObject> mMidwiferyfirm;
protected ParseUser mCurentUser;
Bundle b = getIntent().getExtras();
//store selected items in array
String[] resultArr = b.getStringArray("selectedItems");
public CustomMidwifeAdapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
// Here we can configure a ParseQuery to display
// midwives
ParseQuery query = new ParseQuery("midwifefirm");
query.whereEqualTo("userType", "midwife");
return query;
}
});
}
然而,我收到一个错误,无法解决方法getIntent ...我在想这是因为我无法使用它,除非在一个Activity ..我可以将它传递给我的ListActivity,但后来我不是确定如何在我的ParseQuery中使用它。
我一直在寻找答案,并且有一些参考需要使用上下文,但不确定这是否适用于我的问题。
这是我的MidwifeListActivity
public class MidwifeResultList extends ListActivity {
public static final String TAG = MidwifeResultList.class.getSimpleName();
private ParseQueryAdapter<ParseObject> mainAdapter;
protected ParseRelation<ParseUser> mPatientRelation;
protected ParseUser mCurrentUser;
protected List<ParseObject> mMidwife;
private CustomMidwifeAdapter midwifeListAdapter;
//calls Adapter; Adapter manages data model
//adapts it to individual entries in widget
//inflates, assigns it to rows
ArrayAdapter<String> madapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
//initialize main ParseQueryAdapter
mainAdapter = new CustomMidwifeAdapter(this);
//which keys in Midwife object
mainAdapter.setTextKey("practicename");
mainAdapter.setTextKey("education");
mainAdapter.setTextKey("yearsinpractice");
mainAdapter.setTextKey("practicephilosophy");
mCurrentUser = ParseUser.getCurrentUser();
mPatientRelation = mCurrentUser.getRelation(ParseConstants.KEY_PATIENT_RELATION);
setListAdapter(mainAdapter);
mainAdapter.loadObjects();
}
protected void onListItemClick (ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (getListView().isItemChecked(position)) {
mCurrentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e != null) {
Log.e(TAG, e.getMessage());
}
}
});
Intent intent = new Intent(this, SelectedMidwife.class);
intent.putExtra("practicename", "practicename");
startActivity(intent);
}
else {
//remove friend
}
}
}