我在使用自定义适配器在列表视图上选择多个项目时遇到问题。
这是我的ListActivity类,我使用JSON从MySQL数据库中获取数据:
public class ViewCitiesActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Search EditText
EditText inputSearch;
// view elements
Button execute_btn;
ListView lv;
// List adapter
ListAdapter adapter;
// Exercise type
public Integer type;
// string for selected items
String my_sel_items;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// Hashmap for received data
ArrayList<HashMap<String, String>> cityList;
// url to get all categories list
private static String url_cities = "http://www.xxxxxxxxx.hr/sale/viewcities.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_cities = "cities";
private static final String TAG_ID = "city_id";
private static final String TAG_NAME = "city";
// categories JSONArray
JSONArray cities = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcities);
// Hashmap for ListView
cityList = new ArrayList<HashMap<String, String>>();
Intent i = getIntent();
type = i.getIntExtra("type", 0);
// Loading products in Background Thread
new LoadAll().execute(type.toString());
// Get listview
lv = getListView();
execute_btn = (Button) findViewById(R.id.btn_newcity);
lv.setLongClickable(true);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
execute_btn.setText(R.string.btn_deletecity);
// setting new layout with checkedTextView for adapter
adapter = new SimpleAdapter(
ViewCitiesActivity.this, cityList,
R.layout.view_list_item2, new String[] { TAG_ID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
return true;
}
});
execute_btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
my_sel_items = new String("Test");
SparseBooleanArray a = lv.getCheckedItemPositions();
for (int i = 0; i < a.size(); i++) {
if (a.valueAt(i)) {
my_sel_items = my_sel_items + ","
+ lv.getItemAtPosition(i).toString();
}
}
Log.d("values", my_sel_items);
}
});
}
/**
* Background Async Task to Load all cities by making HTTP Request
* */
class LoadAll extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ViewCitiesActivity.this);
pDialog.setMessage("Loading cities. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("extype", args[0]));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_cities, "POST", params);
// Check your log cat for JSON reponse
Log.d("All cities: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of categories
cities = json.getJSONArray(TAG_cities);
// looping through All categories
for (int i = 0; i < cities.length(); i++) {
JSONObject c = cities.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
// adding HashList to ArrayList
cityList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all categories
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
ViewCitiesActivity.this, cityList,
R.layout.view_list_item, new String[] { TAG_ID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
// standard JSONParser class
public class JSONParser {
}
以下是inital adapter build的view_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="1"
android:orientation="vertical" >
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#000000"
android:textSize="23sp"
android:textStyle="bold" />
这是第二个view_list_item2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="1"
android:orientation="vertical" >
<TextView
android:id="@+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<GridLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="1"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
<TextView
android:id="@+id/name"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:textColor="#000000"
android:textSize="23sp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkBox"
android:layout_column="0"
android:layout_gravity="right|top"
android:layout_row="0"
android:layout_marginRight="10dp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple" />
</GridLayout>
</LinearLayout>
现在,一切正常,但是当我点击按钮时,我无法从列表中获取已检查项目的列表。而我实际需要的是来自已检查项目的city_ID列表,以便我可以启动一个新线程从数据库中删除它们。
有谁知道怎么做?我已经搜索了StackOverflow问题和其他教程,但我似乎无法使其工作。
答案 0 :(得分:0)
在您的帖子中,您需要列表中的已检查项目列表。 如果这是你的问题,我可以提供帮助。 首先,您应该使用 CustomArrayAdapter 。不是 SimpleAdapter 。 现在,您必须创建布尔值的ArrayList 以保存列表项的检查状态。
ArrayList<boolean> checkstate = new ArrayList<boolean>();
当复选框发生变化时,在 CustomArrayAdatper 中,您可以使用 onCheckedChangeListener 将状态保存在布尔列表(检查状态)中。 现在您有了已检查项目的列表。