2016-12-31 57 views
0

我已經在線使用一些代碼來解析JSON到android中的listview。現在,我想通過點擊listview上的任何項目來將單擊行中的數據發送到另一個活動。試過其他的解決方案,但沒有奏效。如何將json listview上的選定項目發送給其他活動?

我正在處理的是將報價及其作者發送給其他活動。

public class MainActivity extends AppCompatActivity { 

private String TAG = MainActivity.class.getSimpleName(); 
public ListView lv; 

ArrayList<HashMap<String, String>> contactList; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    contactList = new ArrayList<>(); 
    lv = (ListView) findViewById(R.id.list); 

    new GetContacts().execute(); 


} 

private class GetContacts extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     Toast.makeText(MainActivity.this,"Json Data is downloading",Toast.LENGTH_LONG).show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     HttpHandler sh = new HttpHandler(); 
     // Making a request to url and getting response 
     String url="https://gist.githubusercontent.com/chefk5/b2a6b17037e989b902834a70cc9b4ce7/raw/667f7fe8aa74cea7e54dfb69a0d2015f7b934f0d/quotes.json"; 
     String jsonStr = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + jsonStr); 
     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       JSONArray contacts = jsonObj.getJSONArray("quotes"); 

       // looping through All Contacts 
       for (int i = 0; i < contacts.length(); i++) { 
        JSONObject c = contacts.getJSONObject(i); 
        String quote = c.getString("quote"); 
        String name = c.getString("name"); 

        // tmp hash map for single contact 
        HashMap<String, String> contact = new HashMap<>(); 

        // adding each child node to HashMap key => value 
        contact.put("quote", quote); 
        contact.put("name", name); 

        // adding contact to contact list 
        contactList.add(contact); 
       } 
      } catch (final JSONException e) { 
       Log.e(TAG, "Json parsing error: " + e.getMessage()); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Json parsing error: " + e.getMessage(), 
           Toast.LENGTH_LONG).show(); 
        } 
       }); 

      } 

     } else { 
      Log.e(TAG, "Couldn't get json from server."); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Couldn't get json from server. Check LogCat for possible errors!", 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, 
       R.layout.list_item, new String[]{ "quote","name"}, 
       new int[]{R.id.email,R.id.mobile}); 
     lv.setAdapter(adapter); 


    } 


} 

回答

0

在OtherActivity爲此在onPostExecute

ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, 
       R.layout.list_item, new String[]{ "quote","name"}, 
       new int[]{R.id.email,R.id.mobile}); 
     lv.setAdapter(adapter); 
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      @Override 
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
       String quete = contactList.get(i).getQuete(); 
       String name = contactList.get(i).getName(); 
       Intent intent = new Intent(MainActivity.this, OtherActivity.class); 
       intent.putExtra("QUETE", quete); 
       intent.putExtra("NAME", name); 
       startActivity(intent); 
      } 
     }); 

現在,把下面的代碼行中的OnCreate()梅索德

String name = getIntent().getExtraString("NAME"); 
String quete = getIntent().getExtraString("QUETE"); 

多數民衆贊成:)

+0

從那裏我可以getQuete()和getName()? – Chefk5

+0

它的工作表示感謝! – Chefk5

相關問題