Android : Check box sending value to next activity even after deselcting it

on Thursday, December 4, 2014


I used a listview to display the check boxes in my activity. I also put a check to see atleast one check box is checked otherwise it will toast a message asking the user to please select atleast one value. Below are my two classes. Problem which i am having is that when i press the submit button without selecting a check box then i get a message to select atleast one checkbox. But when i select and deselect the check box and then submit it then it goes to the next activity with value of the check box which i dont want. It should not go to the other activity till i select one checkbox. Please help me with this problem.


ConnectAdapter.java



package com.arcadian.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.arcadian.genconian.R;

public class ConnectAdapter extends ArrayAdapter<ConnectModel> {

public ArrayList<ConnectModel> stateList;

Context cntx;
public static ConnectModel connect;
CheckBox cb;

public ConnectAdapter(Context context, int textViewResourceId,

ArrayList<ConnectModel> stateList) {
super(context, textViewResourceId, stateList);
this.cntx = context;

this.stateList = new ArrayList<ConnectModel>();
this.stateList.addAll(stateList);
}

public class ViewHolder {

CheckBox connect_CB;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;

Log.v("ConvertView", String.valueOf(position));

if (convertView == null) {

LayoutInflater vi = (LayoutInflater) cntx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

convertView = vi.inflate(R.layout.list_connect_row, null);

holder = new ViewHolder();

holder.connect_CB = (CheckBox) convertView
.findViewById(R.id.connect_CB);

convertView.setTag(holder);

holder.connect_CB
.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton v,
boolean isChecked) {
// TODO Auto-generated method stub
cb = (CheckBox) v;

if (v.isChecked()) {
connect = (ConnectModel) cb.getTag();
/*
* Toast.makeText( cntx.getApplicationContext(),
* "Checkbox: " + cb.getText() + " -> " +
* cb.isChecked(), Toast.LENGTH_LONG).show();
*/

connect.setSelected(cb.isChecked());

}

// else{
// Toast.makeText(getContext(), "Select aleast one.", Toast.LENGTH_LONG).show();
// String select = null;
// }

}
});

}

else {
holder = (ViewHolder) convertView.getTag();

}

ConnectModel state = stateList.get(position);

holder.connect_CB.setText(state.getName());
holder.connect_CB.setChecked(state.isSelected());

holder.connect_CB.setTag(state);

return convertView;
}
}


**ConnectActivity.java**

package com.arcadian.genconian;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.arcadian.adapter.ConnectAdapter;
import com.arcadian.adapter.ConnectModel;
import com.arcadian.utils.CommonActivity;
import com.arcadian.utils.Constants;
import com.arcadian.utils.JSONParser;

public class ConnectActivity extends CommonActivity implements OnClickListener {

ConnectAdapter dataAdapter = null;
ArrayList<ConnectModel> stateList;
private ProgressDialog pDialog;
String to_connect;
String response;
private StringBuffer responseText;
int success;
String email, type;
private String status;
String select;
ConnectModel _ConnectModel;

ConnectModel selstate;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);

// Generate list View from ArrayList
displayListView();
responseText = new StringBuffer();
Button submit_BT = (Button) findViewById(R.id.submit_BT);
submit_BT.setOnClickListener(this);

Intent i = getIntent();
email = i.getStringExtra("user_email");
type = i.getStringExtra("type");

}

private void displayListView() {

// Array list of countries
stateList = new ArrayList<ConnectModel>();

_ConnectModel = new ConnectModel("All", false);
stateList.add(_ConnectModel);
_ConnectModel = new ConnectModel("Stream", false);
stateList.add(_ConnectModel);
_ConnectModel = new ConnectModel("Industry", false);
stateList.add(_ConnectModel);
_ConnectModel = new ConnectModel("Field", false);
stateList.add(_ConnectModel);
_ConnectModel = new ConnectModel("Batchmates", false);
stateList.add(_ConnectModel);

// create an ArrayAdaptar from the String Array
dataAdapter = new ConnectAdapter(this, android.R.layout.simple_list_item_multiple_choice,
stateList);
ListView to_connect_LV = (ListView) findViewById(R.id.to_connect_LV);
// Assign adapter to ListView
to_connect_LV.setAdapter(dataAdapter);
}

@Override
public void onClick(View v) {
int id = v.getId();

// openRequest.setCallback(statusCallback);
// session.openForRead(openRequest);
// loginProgress.setVisibility(View.VISIBLE);

switch (id) {
case R.id.submit_BT:

ArrayList<ConnectModel> stateList = dataAdapter.stateList;
response = "";
for (int i = 0; i < stateList.size(); i++) {
ConnectModel state = stateList.get(i);
selstate = ConnectAdapter.connect;
if(selstate!=null)
if (selstate.equals(state)) {

select = "abc";

if ((stateList.size() - 1) >= i) {

responseText.append(state.getName() + ",");
String text = state.getName();
response = responseText.toString();
loge("response", "response text is" + responseText);
}

else {
responseText.append(state.getName());
}
}

}

if (select == null) {

Toast.makeText(getApplicationContext(), "Select at least one.",
Toast.LENGTH_SHORT).show();

} else {
new Connect().execute();
}
/*
* if(response.length()>1) {
* if(response.substring(response.length()-1).equals(",") ) {
* response
* =response.replace(response.substring(response.length()-1), "" );
*
* }
*
* }
*
* if (response.length() <= 1) { response =
* "batch,stream,field,industry"; }
*/

break;

default:
break;
}
}

public class Connect extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {

super.onPreExecute();
pDialog = new ProgressDialog(ConnectActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

@Override
protected String doInBackground(String... params) {

to_connect = responseText.toString();

String connect_url = Constants.REMOTE_URL + "/GenconianApi/reg2";

log("to", "connect url is:" + connect_url);

try {
JSONParser jsonParser = new JSONParser();
List<NameValuePair> Params = new ArrayList<NameValuePair>();
String res;
Intent email_Intent = getIntent();
email = email_Intent.getStringExtra("user_email");
type = email_Intent.getStringExtra("type");

loge("email in", "connectactivity is:" + email);
int len = response.length();
StringBuilder builder = new StringBuilder();

for (int i = 0; i < len - 1; i++) {

builder.append(Character.toLowerCase(response.charAt(i)));
}

Params.add(new BasicNameValuePair("email", email));
Params.add(new BasicNameValuePair("connected", builder
.toString()));
Log.e("responce", builder.toString());

JSONObject json = jsonParser.makeHttpRequest(connect_url,
"POST", Params);
loge("in reg2", json.toString());

JSONObject obj = json.getJSONObject("Status");
loge("obj is", obj.toString());

status = obj.getString("status");

loge("user", "status is:" + status);

success = Integer.parseInt(status);

loge("chk", "rslt code is:" + success);

if (success == 1) {

Intent k = new Intent(ConnectActivity.this,
FindFriends.class);
loge("chk", "inside success:" + success);
k.putExtra("user_email", email);
k.putExtra("type", type);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
startActivity(k);
loge("email", "" + email);

return json.getString(Constants.TAG_MESSAGE);
} else {
Log.e("Login Failure!",
json.getString(Constants.TAG_MESSAGE));
return json.getString(Constants.TAG_MESSAGE);
}
}

catch (JSONException e) {
e.printStackTrace();
}

return null;
}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();

}
}

@Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();

Intent i = new Intent(ConnectActivity.this, More.class);
i.putExtra("user-email", email);
i.putExtra("type", type);
startActivity(i);
}

}

0 comments:

Post a Comment