我正在开发一个应用程序,我需要能够将一个位置列表加载到ListView中,然后让用户选择一个位置,然后确定用户体验的其余部分。
目前我有一个从JSON请求中提取的位置列表,我可以轻松地将信息显示到ListView中 - 我遇到的问题是将相同的列表转换为某种形式的列表,用户只能选择一个地点。如果它是单选按钮列表或微调器,我个人没有偏好。我过去一周在网上的教程中一直在尝试各种方法,但是我永远无法使用我当前的检索和存储列表的方法,所以我终于有机会结束并寻找一些指导。我是java / android编程的新手,所以我知道我缺乏经验可能是最值得责备的。所以我想如果有人能够至少指出我正确的方向,那么我就不会在需要大量重新工作的解决方案上转动我的车轮,那么我会非常感激!
下面是我当前的活动,它检索JSON并将其打成标准ListView - 我将它存储到数据库中,以便在应用程序的行下方使用。
public class SQLiteJSONParsing extends ListActivity {
private ProgressDialog pDialog;
//////////////////////// JSON URL //////////////////////////
private static String url = "http://my/link/to/json";
//////////////////////// JSON Node Names //////////////////////////
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_CON_POSITION = "con_position";
private static final String TAG_ADDRESS = "address";
private static final String TAG_SUBURB = "suburb";
private static final String TAG_STATE = "state";
private static final String TAG_POSTCODE = "postcode";
private static final String TAG_TELEPHONE = "telephone";
private static final String TAG_EMAIL_TO = "email_to";
//////////////////////// JSON Array //////////////////////////
JSONArray contacts = null;
private DatabaseHelper databaseHelper;
//////////////////////// HashMap ListView //////////////////////////
ArrayList<HashMap<String, String>> locationsList;
///////////////////////// Start onCreate method ////////////////////////////
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_launch_options);
databaseHelper = new DatabaseHelper(SQLiteJSONParsing.this);
locationsList = new ArrayList<HashMap<String, String>>();
//////////////////////// Skip Button //////////////////////////
Button cancel = (Button)findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
Intent locationList = new Intent(SQLiteJSONParsing.this, MainActivity.class);
startActivity(locationList);
finish();
}
});
//////////////////////// Start ASYNC //////////////////////////
new GetLocations().execute();
}
//////////////////////// ASYNC HTTP Call //////////////////////////
private class GetLocations extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(SQLiteJSONParsing.this);
pDialog.setMessage("Loading Locations...");
pDialog.setCancelable(true);
pDialog.show();
}
//////////////////////// Start Background Service Handler & Load the DB //////////////////////////
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
LocationsFeedServiceHandler sh = new LocationsFeedServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, LocationsFeedServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
//////////////////////// Get the JSON Node Array //////////////////////////
contacts = jsonObj.getJSONArray(TAG_CONTACTS);
//////////////////////// Loop Through the Results //////////////////////////
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String con_position = c.getString(TAG_CON_POSITION);
String address = c.getString(TAG_ADDRESS);
String suburb = c.getString(TAG_SUBURB);
String state = c.getString(TAG_STATE);
String postcode = c.getString(TAG_POSTCODE);
String telephone = c.getString(TAG_TELEPHONE);
String email_to = c.getString(TAG_EMAIL_TO);
//////////////////////// Save Records to DB //////////////////////////
databaseHelper.saveTableRecord(id, name, con_position, address, suburb, state, postcode, telephone, email_to);
//////////////////////// Single Items HashMap //////////////////////////
HashMap<String, String> items = new HashMap<String, String>();
//////////////////////// Add Items to the HashMap //////////////////////////
items.put(TAG_ID, id);
items.put(TAG_NAME, name);
items.put(TAG_CON_POSITION, con_position);
items.put(TAG_ADDRESS, address);
items.put(TAG_SUBURB, suburb);
items.put(TAG_STATE, state);
items.put(TAG_POSTCODE, postcode);
items.put(TAG_TELEPHONE, telephone);
items.put(TAG_EMAIL_TO, email_to);
//////////////////////// Add Items to the LocationsList //////////////////////////
locationsList.add(items);
}
//////////////////////// Capture Exceptions //////////////////////////
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("LocationsFeedServiceHandler", "Couldn't get any data from the url");
}
return null;
}
//////////////////////// Close Progress Dialog //////////////////////////
protected void onPostExecute(Void location_result) {
super.onPostExecute(location_result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
//////////////////////// Update the Parsed JSON into the ListAdapter //////////////////////////
ListAdapter locations_adapter = new SimpleAdapter(
SQLiteJSONParsing.this, locationsList,
R.layout.first_launch_locations_detail,
new String[] { TAG_NAME },
new int[] { R.id.name });
setListAdapter(locations_adapter);
}
}
答案 0 :(得分:0)
我使用了以下示例代码,我需要从列表视图中选择单个项目。
http://amitandroid.blogspot.in/2013/03/android-custon-single-choice-lsitview.html