Android 具有单选和动态数据的自定义ListView
我正在开发一个应用程序,我需要能够将位置列表加载到ListView中,然后让用户选择一个位置,该位置将决定用户接下来的体验 目前,我有一个从JSON请求中提取的位置列表,我可以很容易地将信息显示到ListView中——但我遇到的问题是将相同的列表转换成某种形式的列表,用户可以只选择一个位置。我个人不喜欢它是单选按钮列表还是微调器。在过去的一周里,我一直在尝试网上教程中的各种方法,但我无法让它们中的任何一种使用我目前检索和存储列表的方法,所以我终于束手无策了,想寻求一些指导。我对java/android编程非常陌生,所以我知道我缺乏经验可能是最主要的原因。所以我想,如果有人至少能给我指出正确的方向,这样我就不会在一个需要大量重复工作的解决方案上旋转轮子,那么我将非常感激 下面是我当前的活动,它检索JSON并将其放入标准ListView中——我让它将信息存储到DB中,以便在应用程序的后续工作中使用Android 具有单选和动态数据的自定义ListView,android,android-layout,android-listview,Android,Android Layout,Android Listview,我正在开发一个应用程序,我需要能够将位置列表加载到ListView中,然后让用户选择一个位置,该位置将决定用户接下来的体验 目前,我有一个从JSON请求中提取的位置列表,我可以很容易地将信息显示到ListView中——但我遇到的问题是将相同的列表转换成某种形式的列表,用户可以只选择一个位置。我个人不喜欢它是单选按钮列表还是微调器。在过去的一周里,我一直在尝试网上教程中的各种方法,但我无法让它们中的任何一种使用我目前检索和存储列表的方法,所以我终于束手无策了,想寻求一些指导。我对java/andr
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);
}
}
公共类SQLiteJSONParsing扩展ListActivity{
私人对话;
////////////////////////JSON URL//////////////////////////
专用静态字符串url=”http://my/link/to/json";
////////////////////////JSON节点名称//////////////////////////
专用静态最终字符串标记_CONTACTS=“CONTACTS”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记\u CON\u POSITION=“CON\u POSITION”;
私有静态最终字符串标记_ADDRESS=“ADDRESS”;
私有静态最终字符串标记\u郊区=“郊区”;
私有静态最终字符串标记_STATE=“STATE”;
私有静态最终字符串标记_POSTCODE=“POSTCODE”;
专用静态最终字符串标记_TELEPHONE=“TELEPHONE”;
私有静态最终字符串标记\u EMAIL\u TO=“EMAIL\u TO”;
////////////////////////JSON数组//////////////////////////
JSONArray联系人=null;
专用数据库助手数据库助手;
////////////////////////HashMapListView//////////////////////////
阵列列表位置列表;
/////////////////////////启动onCreate方法////////////////////////////
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.first_launch_选项);
databaseHelper=newdatabasehelper(SQLiteJSONParsing.this);
locationsList=新的ArrayList();
////////////////////////跳过按钮//////////////////////////
按钮取消=(按钮)findViewById(R.id.cancel);
cancel.setOnClickListener(新的OnClickListener()
{public void onClick(视图v)
{
Intent locationList=newintent(SQLiteJSONParsing.this,MainActivity.class);
startActivity(位置列表);
完成();
}
});
////////////////////////异步启动//////////////////////////
新建GetLocations().execute();
}
////////////////////////异步HTTP调用//////////////////////////
私有类GetLocations扩展异步任务{
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=newprogressdialog(SQLiteJSONParsing.this);
设置消息(“加载位置…”);
pDialog.setCancelable(真);
pDialog.show();
}
////////////////////////启动后台服务处理程序并加载数据库//////////////////////////
受保护的Void doInBackground(Void…arg0){
//创建服务处理程序类实例
LocationsFeedServiceHandler sh=新的LocationsFeedServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,LocationsFeedServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
////////////////////////获取JSON节点数组//////////////////////////
contacts=jsonObj.getJSONArray(TAG_contacts);
////////////////////////循环浏览结果//////////////////////////
对于(int i=0;i