0

I have done the JSON parsing from URl in doInBackground and display the result as in Listview. The thing is if i will click on the list item then it will display the details about the list item. I have done the LinkedIn Integration. From that i got a list of connection displayed in list. So after clicking on each connection item it will display the details about the connection like firstname, lastname, headline, pictureurl etc... So how do i do this.?

Code:

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

            }   }
    });
}

In SingleListItem.java class i wrote like this:

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

}
}

After compiling i am getting the null value for the text.

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
user3886658
  • 53
  • 1
  • 7
  • From my understanding I think you want to display list inside list is this understanding correct – Anuja Aug 01 '14 at 04:59
  • Try to pass your ArrayList HashMap in Intent and also list item clicked position so in details activity you get all list item data now base on list item clicked show particular details also you can show all list item details implementing ViewPager in detail screen. – Haresh Chhelana Aug 01 '14 at 05:04
  • 1
    share your logcat. Also share the line of code whose line number is displayed in the logcat trace, where you get the NPE – Rajen Raiyarela Aug 01 '14 at 05:50
  • I have added. Check it out @Rajen – user3886658 Aug 01 '14 at 06:08
  • you forgot to add class names. so which class you are getting NPE in GetContacts or in SingleListItem. From code i think you are getting error in intent putextra. just put a check for null before adding to putextra for each variables you add. – Rajen Raiyarela Aug 01 '14 at 06:13

3 Answers3

2

Where is your startActivity(i);. Put it inside onClick()

ORIGINAL
  • 179
  • 1
  • 12
1

In onItemClick(...) startActivity(intent) is missing

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

Don't forget to declare SingleListItem in manifest.

EDIT :1 Because you have to fetch those values from string array's HashMap according to position try to debug onItemClick(...).

Kaushik
  • 6,150
  • 5
  • 39
  • 54
-1

I think you have to call on click listener event of listview in oncreate() method instead of on post execute of AsyncTask so try it.

Rashmi
  • 23
  • 6