Click here to Skip to main content
15,896,450 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to set button on click in my list
first i will retrieve form data base (MySQL)name-id-number of student
then display it as list with 2 buttons
the problem is how to but on click listener i try lots of example and
i do not know what is the problem i try to do on item click but it is not work
and no error happened
look at delete button



Java
package com.ksu.sms;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;

import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import android.widget.SimpleAdapter;

import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
 


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;
private TextView mDateDisplay;
private int mYear;
private int mMonth;
private int mDay;
ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manage_section);
        mDateDisplay = (TextView) findViewById(R.id.day);
  	  
        // add a click listener to the button
    
        // get the current date
        
        final Calendar c = Calendar.getInstance();
       mYear = c.get(Calendar.YEAR);
      mMonth = c.get(Calendar.MONTH);
        mDay = c.get(Calendar.DAY_OF_MONTH);
        mDateDisplay.setText(mDay+"-"+mMonth+"-"+mYear);

	
        studentList = new ArrayList<HashMap<String, String>>();
        //image

        //imageView = new ImageView(this);
       // imageView = (ImageView)findViewById(R.id.imageView1);
      //  Drawable image = ImageOperations(this, path, getid+"."+getnamep);
       // imageView.setImageDrawable(image);
  
        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);

            	 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 
		      
		} 

 
    }
}


it there is easy way please help me this problem derive me crazy
Posted
Comments
Mohibur Rashid 11-Oct-12 3:41am    
I am not sure, but wont you have to implements listener?
maram1410 11-Oct-12 3:51am    
can you explain more what should i do
Mohibur Rashid 11-Oct-12 4:25am    
follow the question here:
http://stackoverflow.com/questions/1821871/android-how-to-fire-onlistitemclick-in-listactivity-with-buttons-in-list
Mike DiGiovanni 15-Oct-12 6:21am    
Also look into using an AsyncTaskLoader, instead of just an AsyncTask for loading the data in the background. http://developer.android.com/reference/android/content/AsyncTaskLoader.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900