json, listadapters and multiple on click activity for listviews
I am using api calls to pull out a json entry and fill in my list view.
The problem I am having is implementing different listeners inside of my
list view. The list view looks like this:
The listView is to implement an onItemClick listener and a checkBox
listener. Till now I was able to populate the listView and generate the
onItemClick listener, but I failed to implement the checkBox listener.
From my tries I am only able to invoke this checkBox listener after the
listView item is clicked and only for one of the checkBoxes(out of
several).
The code looks like this for now:
public class Allprojects extends SherlockListActivity{
ArrayList<HashMap<String, String>> all_list;
ProgressDialog progressDialog;
ActionMode mMode;
ListView list;
private List<Random> randomList = new ArrayList<Random>();
ArrayAdapter<Random> adapterNew;
private SparseBooleanArray mCheckStates;
Layout layoutList;
@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.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();
}
}
});
}
});
}
}
}
No comments:
Post a Comment