我正在尝试在我的Android应用程序中实现搜索功能,我想从活动发现中获取用户在搜索编辑文本中写入的内容,并打开一个名为活动搜索的新活动,其中结果可以显示,发布object是我的一个实现类,我从后端获取数据 每次我尝试它都会抛出一个异常 这是我为此部分的Activity发现的代码 `
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
// setContentView(R.layout.search_grid);
// gridView2 = (GridView) findViewById(R.id.search_grid);
RequestParams rp = new RequestParams();
final String url;
if (people.isChecked()) {
rp.add("user", search.getText().toString());
rp.add("counter", "");
url = "search_users";
} else if (tags.isChecked()) {
rp.add("tag", search.getText().toString());
rp.add("counter", "");
url = "search_exact_hashtag";
} else {
rp.add("location", search.getText().toString());
rp.add("counter", "");
url = "search_location";
}
final ArrayList<PostObject> addyExtras = new ArrayList<>();
//Toast.makeText(getApplicationContext()," something " + , Toast.LENGTH_SHORT).show();
PabloClient.post(url, rp, new JSONHandler(getApplicationContext(), new JSONHandler.OnFinishedListener() {
@Override
public void OnFinished(int statusCode, Header[] headers, JSONObject response, int result) {
try {
if (result == 1) {
// Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
JSONArray posts = response.getJSONArray("data");
setContentView(R.layout.search_grid);
//////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
for (int i = 0; i < posts.length(); i++) {
searchPosts.add(new PostObject(posts.getJSONObject(i)));
addyExtras.add(searchPosts.get(i));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Intent intent = new Intent(ActivityDiscover.this, ActivitySearch.class);
intent.putParcelableArrayListExtra("mylist", addyExtras);
startActivity(intent);
}
}));
}
});
`
以及我如何获得搜索活动的意图
searchPosts2 = getIntent().getParcelableArrayExtra("mylist");
答案 0 :(得分:1)
执行以下操作....
public class PostObject implements Parcelable
{
///......your class code
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
public PostObject(Parcel in) {
super();
// readFromParcel(in);
}
public static final Parcelable.Creator<PostObject> CREATOR = new Parcelable.Creator<PostObject>() {
public PostObject createFromParcel(Parcel in) {
return new PostObject(in);
}
public PostObject[] newArray(int size) {
return new PostObject[size];
}
};
}
答案 1 :(得分:1)
应在Bundle
的帮助下传递数组发送数组
从发件人方面开始,您需要在捆绑包中添加阵列列表
Bundle data = new Bundle();
data.putParcelableArrayList("mylist", addyExtras);
然后将捆绑包添加到Intent
Intent intent = new Intent(ActivityDiscover.this, ActivitySearch.class);
intent .putExtra("content", data);
startActivity(yourIntent);
Retrive Array
在接收方结束时从意图
获取捆绑包Bundle data = getIntent().getBundleExtra("content");
然后从bundle
获取数组列表searchPosts2 = data.getParcelableArrayList("mylist");