在我的应用程序中,我正在搜索模块我之前的问题是How to show json response in other activity?,因为我在服务器中发送请求,然后我得到响应并作为响应我得到一些数据,我想要的数据在下一页显示,我不知道该怎么做,任何人都可以帮忙吗?
class AttemptLogin extends AsyncTask<String, String, String> {
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Processing..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@SuppressWarnings("unused")
@Override
protected String doInBackground(String...args) {
//Check for success tag
//int success;
Looper.prepare();
String userids = strtext.toString();
String contri=spcountry.getText().toString();
String states=spstate.getText().toString();
String city=spcity.getText().toString();
System.out.println("Email : " + userids);
System.out.println("Email : " + agesfrom);
System.out.println("Days : " + agesto);
System.out.println("Months : " + heightfroms);
System.out.println("Years : " + heighttos);
System.out.println("User : " + language);
System.out.println("Password : " + religion);
System.out.println("Gender : " + marriage);
System.out.println("First NM : " + contri);
System.out.println("Last NM : " + states);
System.out.println("Profile : " + city);*/
try {
//Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_login_id", userids));
/* params.add(new BasicNameValuePair("age_from", agesfrom));
params.add(new BasicNameValuePair("age_to", agesto));
params.add(new BasicNameValuePair("height_from", heightfroms));
params.add(new BasicNameValuePair("height_to", heighttos));
params.add(new BasicNameValuePair("language", language));
params.add(new BasicNameValuePair("religion", religion));
params.add(new BasicNameValuePair("maritalstatus", marriage));
params.add(new BasicNameValuePair("country", contri));
params.add(new BasicNameValuePair("state", states));
params.add(new BasicNameValuePair("city", city));
*/
params.add(new BasicNameValuePair("version", "apps"));
Log.d("request!", "starting");
// getting product details by making HTTP request
json = jsonParser.makeHttpRequest (
SEARCH_URL, "POST", params);
//check your log for json response
Log.d("Request attempt", json.toString());
final String str = json.toString();
JSONObject jobj = new JSONObject(json.toString());
final String msg = jobj.getString("searchresult");
return json.getString(TAG_SUCCESS);
}catch (JSONException e) {
e.printStackTrace();
}
return null;
}
// After completing background task Dismiss the progress dialog
protected void onPostExecute(String file_url) {
//dismiss the dialog once product deleted
pDialog.dismiss();
Intent intent=new Intent(getActivity(),SearchResults.class);
intent.putExtra("id", strtext);
intent.putExtra("whole", json.getString(TAG_SUCCESS));
startActivity(intent);
}}
Searchresult.java
Id=this.getIntent().getStringExtra("id");
System.out.println("searching id"+Id);
results=this.getIntent().getStringExtra("whole");
System.out.println("Results"+results);
nomathc=(TextView)findViewById(R.id.no_match);
答案 0 :(得分:3)
显示下一个活动的响应可以通过以下方式完成
解决方案1 响应来自字符串,因此使用
将整个响应传递给下一个活动 intent.putExtras("Key",response);
解决方案2 制作一个单独的getter setter类,通过使用这个类,你可以在整个应用程序中设置和获取值。
像这样第1步
public class SingleObject {
//create an object of SingleObject
private static SingleObject instance = new SingleObject();
private String name;
private String age;
//make the constructor private so that this class cannot be
//instantiated
private SingleObject(){}
//Get the only object available
public static SingleObject getInstance(){
return instance;
}
// here u can declare getter setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
第2步 在像这样的第一个活动中使用它
Singleton tmp = Singleton.getInstance( );
tmp.setName("");
第3步
并在下一个活动中
Singleton tmp = Singleton.getInstance( );
String name = tmp.getName;
解决方案3 :制作一个静态的hashmap Arraylist并在任何活动中使用它
或者可能有更多解决方案......
答案 1 :(得分:2)
据我所知,您需要知道的是如何将JSON结果绑定到列表视图。如果这就是你的意思,请继续阅读...
我希望您在SearchResults
活动的XML视图中已经有了一个列表视图。如果没有添加一个(说R.id.listView1
)。
然后创建一个row_listitem.xml
文件来格式化列表中的一个项目的样子。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/item_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/item_mothertongue"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
...
</LinearLayout>
</RelativeLayout>
现在您需要创建自定义适配器(例如ResultAdapter
)。
public class ResultAdapter extends BaseAdapter {
private JSONArray jsonArray;
private LayoutInflater inflater = null;
public ResultAdapter(Activity a, JSONArray b) {
jsonArray = b;
inflater = (LayoutInflater)a.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return title.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.row_listitem, null);
try {
JSONObject jsonObject = jsonArray.getJSONObject(position);
TextView name = (TextView) vi.findViewById(R.id.item_name);
String nameText = jsonObject.getString("name").toString();
name.setText(nameText);
TextView location = (TextView) vi.findViewById(R.id.item_location);
String locationText = jsonObject.getString("location").toString();
location.setText(locationText);
...
} catch (JSONException e) {
e.printStackTrace();
}
return vi;
}
}
然后,您可以修改SearchResults
活动。
public class SearchResults extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchresults);
ListView list = (ListView) findViewById(R.id.listView1);
String results = this.getIntent().getStringExtra("whole");
JSONArray jsonArray = new JSONArray(results);
ResultAdapter adapter = new ResultAdapter(SearchResults.this, jsonArray);
list.setAdapter(adapter);
}
...
}
不检查代码是否存在编译错误。但是我猜你会得到漂移。干杯!
答案 2 :(得分:1)
将对象存储在需要两个参数(对象和唯一键)的静态变量中。
// This class will hold the OBJECT containing a key in order to Access the
//location in our Custom Stack
class Holder{
private Object obj = null;
private String key = null;
public Holder(Object obj, String key){
this.obj = obj;
this.key = key;
}
}
// This will serve as the storage for your JSON value
class GlobalStorage{
public static ArrayList<Holder> stack = new ArrayList<Holder>();
public static Object getValue(String key){
for(int x = 0 ; x < stack.size() ; x++){
if(stack.get(x).equals(key)){
return stack.get(x);
}
}
return null;
}
}
//In your OnPost
protected void onPostExecute(String file_url){
// your own code here ..
GlobalStorage.add(new Holder("HI THIS IS MY JASON RESPONSE","whole"));
// your own code here..
}
Searchresult.java
// myJson holds the value
String myJson = (String)GlobalStorage.getValue("whole");
答案 3 :(得分:1)
使用bundle
将您的回复从一个活动转移到另一个活动
-create实现Parcelable
的类 public class ResponseData implements Parcelable{}
- 接下来将其保存在意图中:
intent.putExtra(key, new ResponseData(someDataFromServer));
-step - 回复它:
Bundle data = getIntent().getExtras(); ResponseData response=
Response response =(ResponseData ) data.getParcelable(key);
- 显示它:
textView.setText(data from response);
之后将其添加到适配器以显示给用户
在其他情况下,您可以将其保存在应用程序上下文或数据库中(不推荐)
答案 4 :(得分:0)
广播通知:
protected void onPostExecute(String file_url) {
//dismiss the dialog once product deleted
pDialog.dismiss();
Intent intent=new Intent(getActivity(),SearchResults.class);
sendNotificationResponse(json.getString(TAG_SUCCESS));
startActivity(intent);
}
private void sendNotificationResponse(String response) {
Intent intent = new Intent("KEY_INTENT");
intent.putExtra("KEY_RESPONSE", response.toString());
LocalBroadcastManager.getInstance(getActivity()).sendBroadcast(intent);
}
然后在SearchResult.java中:
// handler for received intent
private BroadcastReceiver responseReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String response = extras.getString("KEY_RESPONSE");
//If you want it back as a JSON object
JSONObject object = new JSONObject(response);
//DO SOMETHING WITH THIS RESPONSE HERE
}
}
};
@Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(responseReceiver,
new IntentFilter("KEY_INTENT"));
}
@Override
public void onDestroy() {
super.onDestroy();
// Unregister since the activity is not visible
LocalBroadcastManager.getInstance(this).unregisterReceiver(responseReceiver);
}
我相信应该这样做。
答案 5 :(得分:0)
我只是添加了hashmap并将其称为下一个活动,它正常工作,
HashMap<String, String> hmp = new HashMap<String, String>();
hmp.put(TAG_AGE, Ages+" years");
hmp.put(TAG_CAST, Casts);
hmp.put(TAG_IMAGE, Images);
hmp.put(TAG_LOCATION, Locations);
hmp.put(TAG_MATCH_ID, match_Detail_id);
hmp.put(TAG_NAME, Names);
hmp.put(TAG_PROFILE, Profiles);
alhmp.add(hmp);
在下一个活动中
Intent intent = getIntent();
aList = (ArrayList<HashMap<String, String>>) intent.getSerializableExtra("match_data");
adapter = new CustomAdapterSearch(SearchResults.this, aList);
setListAdapter(adapter);