0
我正在開發一個Android應用程序,它從網站提取數據爲Json
,它有100 ListView
數據。我想要獲取每個OnClickListener
的每個股票價值。我下面的代碼給出:如何獲取每個OnClickListener的json數據?
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
private static String url = "https://api.coinmarketcap.com/v1/ticker/?convert=INR&limit=100";
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();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String id1 = ((TextView)view.findViewById(R.id.name)).getText().toString();
Intent intent = new Intent(MainActivity.this,ViewCoinActivity.class);
intent.putExtra("id",id1);
startActivity(intent);
}
});
}
/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading....");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONArray json = new JSONArray(jsonStr);
// Getting JSON Array node
// looping through All Contacts
for (int i = 0; i < json.length(); i++) {
JSONObject c = json.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String symbol = c.getString("symbol");
String price_inr = c.getString("price_inr");
String percent_change_24h = c.getString("percent_change_24h");
String market_cap_inr = c.getString("market_cap_inr");
String rank = c.getString("rank");
// Phone node is JSON Object
/* JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office"); */
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", name);
contact.put("symbol", symbol);
contact.put("price_inr", price_inr);
contact.put("percent_change_24h", percent_change_24h);
contact.put("market_cap_inr", market_cap_inr);
contact.put("rank", rank);
// 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 Data from Server",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name","symbol",
"price_inr","percent_change_24h","market_cap_inr","rank"}, new int[]{R.id.name,
R.id.symbol,R.id.price_inr,R.id.percent_change_24h,R.id.market_cap_inr,R.id.rank});
lv.setAdapter(adapter);
}
}
}
每個的onclick我想從以下基於股票 https://api.coinmarketcap.com/v1/ticker/bitcoin/
https://api.coinmarketcap.com/v1/ticker/ethereum/
什麼是您遇到的錯誤或者是什麼問題? – Mandy8055