JSON send string value


Last Updated: 2012-03-21 20:08:31
  1. shien

    shien New Member

    Hi i am trying to make view that shows only your name but after you click it it shows email and gender. This data was put in JSON file. But now it shows everything and after clicking it also shows everything. Any ideas how to change it?


    Code (Text):
    1. ArrayList<HashMap<String,String>> contactList = new ArrayList<HashMap<String,String>>();
    2.        
    3.         JSONParser jParser = new JSONParser();
    4.         JSONObject json = jParser.getJSONFromUrl(url);
    5.         try{
    6.             contacts = json.getJSONArray(TAG_CONTACTS);
    7.             for(int i = 0; i < contacts.length(); i++)
    8.             {
    9.                 JSONObject c = contacts.getJSONObject(i);
    10.                
    11.                 String id = c.getString(TAG_ID);
    12.                 String vardpav = c.getString(TAG_NAME);
    13.                 String email = c.getString(TAG_EMAIL);
    14.                 String lytis = c.getString(TAG_GENDER);
    15.                 String nuoroda = c.getString(TAG_LINK);
    16.                
    17.                 HashMap<String,String> map = new HashMap<String, String>();
    18.                
    19.                 map.put(TAG_ID, id);
    20.                 map.put(TAG_NAME, vardpav);
    21.                 map.put(TAG_EMAIL, email);
    22.                 map.put(TAG_GENDER, lytis);
    23.                 map.put(TAG_LINK, nuoroda);
    24.                 contactList.add(map);
    25.             }
    26.         } catch (JSONException e) {
    27.             e.printStackTrace();
    28.         }
    29.         ListAdapter adapter = new SimpleAdapter(this, contactList,R.layout.list_item,
    30.                 new String[] {TAG_NAME, TAG_EMAIL, TAG_GENDER, TAG_LINK}, new int[] { R.id.name, R.id.email, R.id.gender, R.id.link});
    31.        
    32.         setListAdapter(adapter);
    33.        
    34.         ListView lv = getListView();
    35.         lv.setOnItemClickListener(new OnItemClickListener(){
    36.             public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    37.                 String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
    38.                 String email = ((TextView)view.findViewById(R.id.email)).getText().toString();
    39.                 String lytis = ((TextView)view.findViewById(R.id.gender)).getText().toString();
    40.                 String nuoroda = ((TextView)view.findViewById(R.id.link)).getText().toString();
    41.                
    42.                 Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
    43.                 in.putExtra(TAG_NAME, name);
    44.                 in.putExtra(TAG_EMAIL, email);
    45.                 in.putExtra(TAG_GENDER, lytis);
    46.                 in.putExtra(TAG_LINK, nuoroda);
    47.                 startActivity(in);
    Code (Text):
    1. TextView txtName = (TextView) findViewById(R.id.txtName);
    2.         TextView txtEmail = (TextView) findViewById(R.id.txtEmail);
    3.         TextView txtGender = (TextView) findViewById(R.id.txtGender);
    4.         TextView txtFile = (TextView) findViewById(R.id.txtFile);
    5.        
    6.         Intent i = getIntent();
    7.         String name = i.getStringExtra("vardpav");
    8.         String email = i.getStringExtra("email");
    9.         String gender = i.getStringExtra("lytis");
    10.         String link = i.getStringExtra("nuoroda");
    11.         txtName.setText(name);
    12.         txtEmail.setText(email);
    13.         txtGender.setText(gender);
    14.         try {
    15.             // Create a URL for the desired page
    16.             URL url = new URL(link);
    17.  
    18.             // Read all the text returned by the server
    19.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    20.             String str;
    21.             while ((str = in.readLine()) != null) {
    22.                 txtFile.setText(str);
    23.                 // str is one line of text; readLine() strips the newline character(s)
    24.             }
    25.             in.close();
    26.         } catch (MalformedURLException e) {
    27.  
    28.             System.out.println("CHACHACHA");
    29.         } catch (IOException e) {
    30.         }
    31.        
    32.         Button btnImage = (Button) findViewById(R.id.imageButton);
    33.         btnImage.setOnClickListener(new View.OnClickListener() {
    34.             public void onClick(View arg0){
    35.                 Intent i = new Intent(SingleMenuItemActivity.this,ImageActivity.class);
    36.                 startActivity(i);

    Advertisement

Share This Page