我正在使用 api 调用来提取一个 json 条目并填写我的列表视图。我遇到的问题是在我的列表视图中实现不同的侦听器。列表视图如下所示:
listView 是实现一个 onItemClick 监听器和一个 checkBox 监听器。到目前为止,我能够填充 listView 并生成 onItemClick 侦听器,但我未能实现 checkBox 侦听器。从我的尝试中,我只能在单击 listView 项目后调用此复选框侦听器,并且只能针对其中一个复选框(其中一个)。
代码现在看起来像这样:
public class Allprojects extends SherlockListActivity{
ArrayList<HashMap<String, String>> all_list;
ProgressDialog progressDialog;
ActionMode mMode;
ListView list;
Layout layoutList;
CheckBox projectCheckBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.projectlist);
ActionBar bar = getSupportActionBar();
bar.setDisplayShowTitleEnabled(false);
bar.setDisplayHomeAsUpEnabled(false);
bar.setNavigationMode(com.actionbarsherlock.app.ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.actionbarnavlist, android.R.layout.simple_dropdown_item_1line);
OnNavigationListener mNavigationlistner = new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
switch (itemPosition) {
case 0:
// Toast.makeText(getApplicationContext(), "You are already viewing all projects", Toast.LENGTH_LONG).show();
break;
case 1:
Intent intent = new Intent(Allprojects.this, Myproject.class);
startActivity(intent);
finish();
break;
default:
break;
}
return false;
}
};
bar.setListNavigationCallbacks(mSpinnerAdapter, mNavigationlistner);
bar.setSelectedNavigationItem(0);
all_list = new ArrayList<HashMap<String, String>>();
Asyncprojectlist connection = new Asyncprojectlist();
connection.execute();
}
private ActionMode.Callback mActionCallBack = new ActionMode.Callback() {
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.contextual_menu_add_project, menu);
return true;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case R.id.cancelActionMenu:
break;
case R.id.addToProjectsMenu:
break;
default:
break;
}
return false;
}
};
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getSupportActionBar().setSubtitle("press to start selection");
}
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();
}
projectCheckBox = (CheckBox)findViewById(R.id.checkBox);
projectCheckBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(projectCheckBox.isChecked()){
Log.i("CHECKED", "CHECKED");
startActionMode(mActionCallBack);
}else{
Log.i("UNCHECKED","UNCHECKED");
startActionMode(mActionCallBack).finish();
}
}
});
}
});
}
}
}