0

doInBackground で URl からの JSON 解析を実行し、結果を Listview のように表示します。リスト項目をクリックすると、リスト項目の詳細が表示されます。LinkedIn 統合を実行しました。そこから、リストに表示される接続のリストを取得しました。したがって、各接続項目をクリックすると、名、姓、見出し、pictureurl などの接続に関する詳細が表示されます。

コード:

       userProfile.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
        new GetContacts().execute();
        }


  class GetContacts extends AsyncTask<Void, Void, Void> {
        String firstname;
        String id;
        String headline;
        String lastname;
        String pictureUrl;
        String url;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog
                pDialog = new ProgressDialog(LinkedInSampleActivity.this);
                pDialog.setMessage("Please wait...");
                pDialog.setCancelable(false);
                pDialog.show();

            }

            @Override
            protected Void doInBackground(Void... params) {
                DefaultHttpClient httpclient = new DefaultHttpClient();
                try{
                    HttpGet httpget = new HttpGet(
                            "url");
                    HttpResponse response = httpclient.execute(httpget);
                    String jsonResp = EntityUtils.toString(response.getEntity());
                    Log.d("HTTP","Rsponse : "+ jsonResp);

                    if (jsonResp!= null) {

                    JSONObject jsonObject1=new JSONObject(jsonResp);
                    JSONArray jsonArray = jsonObject1.getJSONArray("values");
                    for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject2 = jsonArray.getJSONObject(i);
                    String firstname = jsonObject2.getString("firstName");
                    String lastname = jsonObject2.getString("lastName");
                    String headline = jsonObject2.getString("headline");
                    String pictureUrl = jsonObject2.getString("pictureUrl");
                    String id = jsonObject2.getString("id");
                    JSONObject jsonObject3 = jsonObject2.getJSONObject("siteStandardProfileRequest");
                    String url = jsonObject3.getString("url");
                    Log.d("HTTP", "firstname : " + firstname + "\n" + "lastName :"
                            + lastname + "\n" + "headline : " + headline + "\n"
                            + "pictureUrl :" + pictureUrl + "\n" + "id :"
                            + id + "\n" + "Url :" + url);

                    HashMap<String, String> contact = new HashMap<String, String>();
                    contact.put(TAG_ID, id);
                    contact.put(TAG_FNAME, firstname);
                    contact.put(TAG_LNAME, lastname);
                    contact.put(TAG_HLINE, headline);
                    contact.put(TAG_PURL, pictureUrl);
                    contact.put(TAG_URL, url);
                    contactList.add(contact);

                    }
                }else {
                    Log.e("HTTP", "Couldn't get any data from the url");
                } 
                }catch (Exception e) {
                    e.printStackTrace();
            }   
                return null;
                }   

            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                // Dismiss the progress dialog
                if (pDialog.isShowing())
                    pDialog.dismiss();

                ListAdapter adapter = new SimpleAdapter(
                        LinkedInSampleActivity.this, contactList,
                        R.layout.list_item, new String[] {TAG_FNAME}, new int[] {R.id.textView1 });
                setListAdapter(adapter);
                lv = getListView();
                lv.setOnItemClickListener(new OnItemClickListener() {
                      public void onItemClick(AdapterView<?> parent, View view,
                          int position, long id) {
                          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
                          i.putExtra("id", id);
                          i.putExtra("firstname", firstname);
                          i.putExtra("lastname", lastname);
                          i.putExtra("headline", headline);
                          i.putExtra("pictureUrl", pictureUrl);
                          i.putExtra("url", url);
                        startActivity(i);
                      }
                    });

            }   }
    });
}

SingleListItem.java クラスでは、次のように書きました。

 public class SingleListItem extends Activity{
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.single_list_item);
     TextView tv = (TextView)findViewById(R.id.fname);
       Intent i = this.getIntent();
       if(i!=null){
           String id = i.getStringExtra("id");
           String firstname = i.getStringExtra("firstname");
           String lastname = i.getStringExtra("lastname");
           String headline = i.getStringExtra("headline");
           String pictureUrl = i.getStringExtra("pictureUrl");
           String url = i.getStringExtra("url");
           Log.v("LV","id :"+id+"\n"+"firstname :"+firstname+"\n"+"lastname :"+lastname+"\n"+"headline :"+headline+"\n"+"pictureUrl :"+pictureUrl);
          //tv.setText("id :"+id+"\n"+"firstname :"+firstname+"\n"+"lastname :"+lastname+"\n"+"headline :"+headline+"\n"+"pictureUrl :"+pictureUrl);   
       }else{
           Toast.makeText(getApplicationContext(), "Intent no get", Toast.LENGTH_SHORT).show();
       }

}
}

コンパイル後、テキストの null 値を取得しています。

08-01 06:04:29.330: V/LV(1759): id :null
08-01 06:04:29.330: V/LV(1759): firstname :null
08-01 06:04:29.330: V/LV(1759): lastname :null
08-01 06:04:29.330: V/LV(1759): headline :null
08-01 06:04:29.330: V/LV(1759): pictureUrl :null
4

3 に答える 3

2

あなたの はどこですか startActivity(i);。onClick() の中に入れます

于 2014-08-01T05:05:43.090 に答える
1

onItemClick(...)startActivity(intent)にありません

Intent i = new Intent(getApplicationContext(), SingleListItem.class);
i.putExtra("id", id);
i.putExtra("firstname", firstname);
i.putExtra("lastname", lastname);
i.putExtra("headline", headline);
i.putExtra("pictureUrl", pictureUrl);
i.putExtra("url", url);
startActivity(i);

SingleListItemで宣言することを忘れないでくださいmanifest

編集:1位置に応じてこれらの値を取得する必要があるためstring array's HashMap、デバッグを試みonItemClick(...)ます。

于 2014-08-01T05:12:55.473 に答える
-1

AsyncTask の実行後ではなく、oncreate() メソッドでリストビューのクリック リスナー イベントを呼び出す必要があると思いますので、試してみてください。

于 2014-08-01T05:08:17.573 に答える