click buttons error
- by sara
I will retrieve student information (id -number- name) from a database (MySQL) as a list view,
each student have 2 buttons (delete - alert ) and radio buttons
Every thing is ok, but how can I make an onClickListener, for example for the delete button
because I try lots of examples, I heard that I can use (custom list or get view or direct onClickListener as in my code (but it is not working ) or Simple Cursor Adapter) I do not know what to use, I looked around for examples that can help me, but in my case but I did not find any so I hope this be reference for anyone have the same problem.
this is my code which I use direct onClick with Simple Adapter
public class ManageSection extends ListActivity {
//ProgresogressDialog pDialog;
private ProgressDialog pDialog;
// Creating JSON Parser object
// Creating JSON Parser object
JSONParser jParser = new JSONParser(); //class
boolean x =true;
Button delete;
ArrayList<HashMap<String, String>> studentList;
//url to get all products list
private static String url_all_student = "http://10.0.2.2/SmsPhp/view_student_info.php";
String cl;
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_student = "student";
private static final String TAG_StudentID = "StudentID";
private static final String TAG_StudentNo = "StudentNo";
private static final String TAG_FullName = "FullName";
private static final String TAG_Avatar="Avatar";
HashMap<String, String> selected_student;
// course JSONArray
JSONArray student = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.manage_section);
studentList = new ArrayList<HashMap<String, String>>();
ListView list1 = getListView();
list1.setAdapter(getListAdapter());
list1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
selected_student =(HashMap<String, String>) studentList.get(pos); //member of your activity.
delete =(Button)view.findViewById(R.id.DeleteStudent);
cl=selected_student.get(TAG_StudentID);
Toast.makeText(getBaseContext(),cl,Toast.LENGTH_LONG).show();
delete.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Log.d("id: ",cl);
Toast.makeText(getBaseContext(),cl,Toast.LENGTH_LONG).show();
}
});
}
});
new LoadAllstudent().execute();
}
/**
* Background Async Task to Load all student by making HTTP Request
* */
class LoadAllstudent extends AsyncTask<String, String, String>
{
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ManageSection.this);
pDialog.setMessage("Loading student. Please wait...");
pDialog.setIndeterminate(false);
}
/**
* getting All student from u r l
* */
@Override
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_student, "GET", params);
// Check your log cat for JSON response
Log.d("All student : ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1)
{
// student found
// Getting Array of course
student = json.getJSONArray(TAG_student);
// looping through All courses
for (int i = 0; i < student.length(); i++)//course JSONArray
{
JSONObject c = student.getJSONObject(i); // read first
// Storing each json item in variable
String StudentID = c.getString(TAG_StudentID);
String StudentNo = c.getString(TAG_StudentNo);
String FullName = c.getString(TAG_FullName);
// String Avatar = c.getString(TAG_Avatar);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_StudentID, StudentID);
map.put(TAG_StudentNo, StudentNo);
map.put(TAG_FullName, FullName);
// adding HashList to ArrayList
studentList.add(map);
}
} else {
x=false;
}
} 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 products
pDialog.dismiss();
if (x==false)
Toast.makeText(getBaseContext(),"no student" ,Toast.LENGTH_LONG).show();
ListAdapter adapter = new SimpleAdapter(
ManageSection.this, studentList,
R.layout.list_student, new String[] { TAG_StudentID,
TAG_StudentNo,TAG_FullName},
new int[] { R.id.StudentID, R.id.StudentNo,R.id.FullName});
setListAdapter(adapter);
// Updating parsed JSON data into ListView
}
}
}
So what do you think, why doesn't the delete button work? There is no error in my log cat. What is the alternative way ?.. what should I do ?