我希望在列表视图中获取所选项目的ID,其中数据来自服务器。一切正常,但我无法获取ID以发送到下一个活动以显示所选项目内容。我用过这一行:
String number_of_keys = (lv.getItemAtPosition(position).toString());
但它返回行中的所有项目:( 这是我的代码 感谢。
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity2 extends AppCompatActivity implements TextWatcher {
public String number_of_keys;
private String TAG = MainActivity2.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
private String id;
// URL to get contacts JSON
private static String url = "http://farazjou.com/mobile/drugstore";
ArrayList<HashMap<String, String>> drugstoreList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
drugstoreList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetDrugs().execute();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
String number_of_keys = (lv.getItemAtPosition(position).toString());
i.putExtra("number_of_keys", number_of_keys);
startActivity(i);
}
});
}
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
/**
* Async task class to get json by making HTTP call
*/
private class GetDrugs extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity2.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
public 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 {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray drugstore = jsonObj.getJSONArray("drugstore");
// looping through All Contacts
for (int i = 0; i < drugstore.length(); i++) {
JSONObject c = drugstore.getJSONObject(i);
id = c.getString("id");
String post_title = c.getString("post_title");
String public_title = c.getString("public_title");
HashMap<String, String> drug = new HashMap<>();
drug.put("id", id);
drug.put("post_title", post_title);
drug.put("public_title", public_title);
drugstoreList.add(drug);
}
} 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);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity2.this, drugstoreList,
R.layout.list_item2, new String[]{"post_title", "public_title", "id"}
, new int[]{R.id.post_title, R.id.public_title , R.id.public_id});
lv.setAdapter(adapter);
}
}
}
答案 0 :(得分:0)
您正在实施Click Handler而不是Select Handler。默认情况下,列表不会选择。
按照以下方式试一试,
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
MyClass item = (MyClass) adapter.getItem(position);
}