点击监听器上的错误按钮

时间:2012-10-10 06:43:48

标签: java android

我使用删除按钮从数据库中检索学生的信息(学生编号和ID和姓名),因此如果用户点击删除,它将显示列表中每个按钮的学生ID的烤面包消息。

但我运行代码并单击删除按钮没有任何反应,日志cat没有错误在控制台中没有错误。我错过了什么,为什么会这样?此外,我尝试在log cat中显示此消息,但没有任何反应。

这是我的代码:

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.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {

                 selected_student =studentList.get(pos); //member of your activity.
                 delete =(Button)findViewById(R.id.DeleteStudent);
                 cl=selected_student.get(TAG_StudentID);
                Log.d("id: ",cl);
                 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 

        } 


    }
}

和xml for manage

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_all_1" >

    <!-- HEADER -->

    <include
        android:id="@+id/top_header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="90dp"
        layout="@layout/layout_header" />

    <!-- FOOTER -->


    <!-- MAIN PART -->

    <RelativeLayout
        android:id="@+id/sub_content_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/top_header"
        android:orientation="vertical" >
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
        <ListView
            android:id="@android:id/list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_marginLeft="50dp"
              android:gravity="left"
            android:layout_marginTop="10dp" >
        </ListView>
         </ScrollView>
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/bottom_menu"
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="38dp"
        android:orientation="vertical" >

        <include
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            layout="@layout/layout_footer" />
    </LinearLayout>

</RelativeLayout>

列出student xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <!-- Student id (CourseID) - will be HIDDEN - used to pass to other activity -->
    <TextView
        android:id="@+id/StudentID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone"
         />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      />

    <!-- Name Label -->
    <TextView
        android:id="@+id/FullName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:textSize="8dip"
        android:textStyle="bold" />


        <TextView
            android:id="@+id/StudentNo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="6dip"
            android:paddingTop="6dip"
            android:textSize="8dip"
            android:textStyle="bold" />


        <Button
            android:id="@+id/DeleteStudent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minHeight="10dip"
            android:minWidth="50dip"
            android:text="@string/deleteStudent"
            android:textSize="8dp"
            android:width="30dp" 

            >

      </Button>



              <Button
                  android:id="@+id/Alert"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:minHeight="10dip"
                  android:minWidth="50dip"
                  android:text="@string/Alert"
                  android:textSize="8dp"
                  android:width="30dp" >

      </Button>
      <RadioGroup 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


       <RadioButton
           android:id="@+id/radio_pirates"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:onClick="onRadioButtonClicked"
           android:text="@string/attendance"
           android:textSize="8dp"

            />

       </RadioGroup>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

请尝试替换代码中的以下行:

delete =(Button)findViewById(R.id.DeleteStudent);

delete =(Button)view.findViewById(R.id.DeleteStudent);

答案 1 :(得分:0)

@sara你不能在onItemClick事件中给出onclick事件的实现你应该把这段代码:

 delete.setOnClickListener(new View.OnClickListener()

                {

                    public void onClick(View v) {

                        Log.d("id: ",cl);
                        Toast.makeText(getBaseContext(),cl,Toast.LENGTH_LONG).show();
                    }
             });
        }

在你的适配器的getView方法然后它将工作。你在这里做的是你正在应用监听器删除按钮项目点击所以它不会工作因为它始终应用监听器和你的监听器的方法永远不会被调用!