【问题标题】:How to display intent data in listview?如何在列表视图中显示意图数据?
【发布时间】:2014-12-09 11:54:32
【问题描述】:

我正在创建应用程序,在我的活动中我使用了 getIntent.getstringExtra("string"),此字符串包含 json 响应,在该响应中我有不同的字段需要在列表视图中显示,所以如何做到这一点

检查这个问题How to display response in next activity

class AttemptLogin extends AsyncTask<String, String, String> {

    boolean failure = false;
    JSONObject jobj;
    String resultname;
    @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) {

        Looper.prepare();
        String userids = strtext.toString();
        String agesfrom = spfrom.getText().toString();
        String agesto=spto.getText().toString();
        String heightfroms=spheightfrom.getText().toString();
        String heighttos=spheightto.getText().toString();
        String language=splang.getText().toString();
        String religion = sprelg.getText().toString();
        String marriage=spmrgstatus.getText().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();

             jobj = new JSONObject(json.toString());
             final String msg = jobj.getString("searchresult");
             resultname=jobj.getString(TAG_NAME);

             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",resultname);

            startActivity(intent);

}}

我的下一个活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view_searchresult);

    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);

{"搜索结果": [ {"match_detail_id":369,"profile_id":"GM686317","name":"Sonal Patel","image":"","location":"Rajkot ,Gujarat ,India","mothertongue":"Gujarati ","re​​ligion":"Hindu","occupation":"Professor / Lecturer","education":"Masters - Arts/Science/Commerce/ Others"} , {"match_detail_id":396,"p​​rofile_id":"GM780609","name":"Hetal Trivedi","image":"","location":"Rajkot , Gujarat , India","mothertongue":"Gujarati ","宗教":"印度教","职业":"行政专业","教育":"学士 - 艺术/科学/商业/其他"} , {"match_detail_id":1078,"profile_id":"GM540027","name":"Shruti Dave","image":"","location":"Rajkot , Gujarat , India","mothertongue":"Gujarati ","宗教":"印度教","职业":"教育专业","教育":"硕士 - 艺术/科学/商业/其他"} ] }

我的适配器类

public class CustomAdapterSearch extends BaseAdapter{

private Context context;
private ArrayList<HashMap<String,String>> listData;
private AQuery aQuery;

private static final String TAG_NAME="name";
private static final String TAG_PROFILE="profile_id";
private static final String TAG_IMAGE="image";
private static final String TAG_CAST="cast";
private static final String TAG_AGE="age";
private static final String TAG_LOCATION="location";


public CustomAdapterSearch(Context context,ArrayList<HashMap<String,String>> listData) {
    this.context = context;
    this.listData=listData;
    aQuery = new AQuery(this.context);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = LayoutInflater.from(context).inflate(R.layout.list_item_searchresult,   null);
        holder.propic = (ImageView) convertView.findViewById(R.id.propicsearch);
        holder.txtproname = (TextView) convertView.findViewById(R.id.txtpronamesearch);
        holder.txtproid = (TextView) convertView.findViewById(R.id.txtproidsearch);
        holder.txtprofilecast = (TextView) convertView.findViewById(R.id.txtprofilecastsearch);
        holder.txtprofileage = (TextView) convertView.findViewById(R.id.txtprofileagesearch);
        holder.txtprofileplace = (TextView) convertView.findViewById(R.id.txtprofileplacesearch);

        convertView.setTag(holder);
    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtproname.setText(listData.get(position).get(TAG_NAME));
    holder.txtproid.setText(listData.get(position).get(TAG_PROFILE));
    holder.txtprofilecast.setText(listData.get(position).get(TAG_CAST));
    holder.txtprofileage.setText(listData.get(position).get(TAG_AGE));
    holder.txtprofileplace.setText(listData.get(position).get(TAG_LOCATION));

    aQuery.id(holder.propic).image(listData.get(position).get(TAG_IMAGE),true,true,0,R.drawable.ic_launcher);

    // image parameter : 1 : memory cache,2:file cache,3:target width,4:fallback image
    return convertView;
}
class ViewHolder{
    ImageView propic;
    TextView txtproname;
    TextView txtproid;
    TextView txtprofilecast;
    TextView txtprofileage;
    TextView txtprofileplace;
}

}

【问题讨论】:

  • 我建议不要通过intent发送json数据。因为如果 json 的长度太大,那么它将不起作用。可能你得到 javaBinder 错误。
  • 在使用意图传递数据之前,只需解析 json 数据并添加到 bean。这只是解析数据并添加到列表中,只需从您想要的任何地方调用
  • 简单来说就是解析数据并设置为静态类并从另一个活动调用。
  • How to display intent data in listview? ??为什么会有这样一个令人困惑的话题?您想在列表视图中使用来自 json 文本的数据。你已经在你的活动中检索了 json 文本,意图得到额外的。所以现在继续使用该活动的代码。在此处删除您下载 json 的代码,因为这现在无关紧要。不要参考你的其他帖子。你最好继续在那里而不是开始一个新的帖子。
  • 相反,您应该在此处发布一段 json 文本。在列表视图中告诉您想要从中获得什么数据。显示了您尝试过的代码。

标签: android json android-listview


【解决方案1】:

你试试这个代码吗?

public ArrayList<String> name = new ArrayList<>();
public ArrayList<String> description = new ArrayList<>();

try {
    JSONObject jsonObj = new JSONObject(jsonStr);
    JSONArray building = jsonObj.getJSONArray("data");
    for (int i = 0; i < building.length(); i++) {
        JSONObject c = building.getJSONObject(i);
        String namec = c.getString("title");
        String descriptionc = c.getString("description");
        name.add(namec);
        description.add(descriptionc);
    }
    } catch (JSONException e) {
    e.printStackTrace();
 }

 for (int i = 0; i < name.size(); i++) {
      la.add(new JSONList(name.get(i), description.get(i)));
 }
 lv.setAdapter(la);

jsonStr 是你的 json 字符串,“data”是 json 数组名。

la 是 ListAdapter(自定义适配器),lv 是 ListView。

我建议使用 AsyncTask 将 Json 打印到 ListView。

编辑

 public ArrayList<String> user_login_id = new ArrayList<>();
 public ArrayList<String> age_from = new ArrayList<>();

在 DoInBackground 中

 try {
     JSONObject jsonObj = new JSONObject(json.getString(TAG_SUCCESS));
     JSONArray data = jsonObj.getJSONArray("searchresult");
     for (int i = 0; i < data.length(); i++) {
         JSONObject c = data.getJSONObject(i);
         String user_login_idc = c.getString("user_login_id");
         String age_fromc = c.getString("age_from");
         ...
         user_login_id.add(user_login_idc);
         age_from.add(age_fromc);
         ...
     }
     } catch (JSONException e) {
     e.printStackTrace();
     }

在 onPostExecute 中

 for (int i = 0; i < age_from.size(); i++) {
      la.add(new JSONList(user_login_id.get(i), age_from.get(i) ...));
 }
 lv.setAdapter(la);

编辑

所以,你可以试试这个代码。

List.class

public class List {
Drawable Propic;
String txtproname, txtproid, txtprofilecast, txtprofileage, txtprofileplace;

public List(drawable propic, String txtproname, String txtproid, String txtprofilecast, String txtprofileage, String txtprofileplace) {
this.propic = propic;
this.txtproname = proname;
this.txtproid = proid;
this.txtprofileage = txtprofileage;
this.txtprofileplace = txtprofileplace;
}

public Drawable getpropic() { return propic;}
public String gettxtproname() { return txtproname;}
public String txtproid() { return txtproid;}
public String txtprofileage() { return txtprofileage;}
public String txtprofileplace() { return txtprofileplace;}
}

所以,你可以简单地使用你的适配器

final List data = this.getItem(position)
holder.txtproname.setText(data.getproname());
holder.txtproid.setText(data.gettxtproid());
holder.txtprofilecast.setText(data.gettxtprofilecast());
holder.txtprofileage.setText(data.gettxtprofileage());
holder.txtprofileplace.setText(data.gettxtprofileplace());

在 onPostExecute 中

 for (int i = 0; i < age_from.size(); i++) {
      la.add(new List(user_login_id.get(i), age_from.get(i) ...));
 }
 lv.setAdapter(la);

【讨论】:

  • 您是否遵循我在当前问题中提到的问题?
  • 您的意思是“将 json 打印到列表视图中”吗?如果是这样,您可以将 json String 转换为 JSONObject(JSONObject jsonObj = new JSONObject(jsonStr);),并将 JSONObject 转换为 JSONArray(JSONArray building = jsonObj.getJSONArray("searchresult");)
  • 我在这里编辑我的问题你能编辑你的建议吗?
  • @Johnson,您最好删除此处的所有代码,因为这非常令人困惑。您已经有了 json (getIntent.getstringExtra("string")),所以发布下一个活动的代码。
  • 我建议@Johnson 阅读这篇文章(自定义列表视图教程)Link。自己试试吧。
猜你喜欢
  • 1970-01-01
  • 2020-01-06
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多