我正在开发一个Android应用,它以Json
的形式从网站获取数据,它有100个ListView
数据。我想获取每个OnClickListener
的每个自动收报机值。我的代码如下:
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/
答案 0 :(得分:0)
抱歉,但也许我不明白你的问题。 但是为了获得onItemClickListener的值,您只需在项目位置使用联系人列表。适配器的相同列表。喜欢这个
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String id1 = contactList.get(position).getIdMethod();
Intent intent = new Intent(MainActivity.this,ViewCoinActivity.class);
intent.putExtra("id",id1);
startActivity(intent);
}
});