以下是我当前用于 listView 的代码:
private class Asyncprojectlist extends AsyncTask<Void, Void, Void>{
JSONArray JSar;
JSONObject JSob;
String project_title, project_sector;
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(Allprojects.this);
progressDialog.setCancelable(false);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
UserFunctions user = new UserFunctions();
JSob = user.allprojects();
try {
JSar = JSob.getJSONArray("data");
} catch (JSONException e1) {
e1.printStackTrace();
}
for(int i=0; i<JSar.length(); i++){
try {
JSONObject newobj = JSar.getJSONObject(i);
project_title = newobj.getString("title");
project_sector = newobj.getString("sector");
HashMap<String, String> single = new HashMap<String, String>();
single.put("title", project_title);
single.put("sector", project_sector);
all_list.add(single);
} catch (JSONException e) {
e.printStackTrace();
}
}
// Log.i("json object", JSar.toString());
return null;
}
@Override
protected void onPostExecute(Void result) {
ListAdapter adapter = new SimpleAdapter(getApplicationContext(), all_list, R.layout.projectlist_frame,
new String[]{"title","sector"}, new int[]{R.id.projecttitle, R.id.projectsector});
setListAdapter(adapter);
progressDialog.dismiss();
list = getListView();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked,
int position, long id) {
Intent toindividual = new Intent(Allprojects.this, Individual_project.class);
try {
toindividual.putExtra("nid", JSar.getJSONObject(position).getString("id"));
startActivity(toindividual);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
这里调用了一个 api 来获取数据,并且它是all_list
一个ArrayList<HashMap<>>
用于SimpleAdapter
分配值的位置,例如布局上的projectTitle
和。这里的一切都很完美。但是我很难实现listView布局中的监听器。我相信需要一个自定义适配器。在我尝试构建自定义适配器时,我想不出一个工作代码。请帮助实现自定义适配器,以便 listView 可以实现复选框。projectStatus
listView
checkBox
CheckChangeListener