2012-10-11 76 views
1

我會從一個數據庫(MySQL的)成列表視圖獲取學生信息(ID -number-名), 每個學生有2個按鈕(刪除 - 警報)和單選按鈕點擊按鈕錯誤

screen shot http://im21.gulfup.com/1pWi1.png

每件事都可以,但我怎麼能做一個onClickListener,例如刪除按鈕 ,因爲我嘗試了很多的例子,我聽說我可以使用(自定義列表或獲取視圖或直接onClickListener在我的代碼(但它不工作)或簡單的光標適配器)我不知道該用什麼,我四處尋找可以幫助我的例子,但在我的情況下,但我沒有找到任何,所以我希望這可以作爲參考任何人都有同樣的問題。 這是我的代碼,我使用直接的onClick用簡單的適配器

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 

     } 
    } 
} 

那麼你覺得什麼,爲什麼沒有刪除按鈕的工作?我的日誌貓沒有錯誤。什麼是替代方式?

+0

刪除後是否收到日誌消息和Toast消息? –

+0

1.使用自定義適配器2.in getView()執行onclick監聽器 – mukesh

+0

no @ Mohit Verma – sara

回答

0

在城外定義按鈕的onclick監聽你的ListView的onclicklistener -

delete =(Button)view.findViewById(R.id.DeleteStudent); // initialize this here 

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. 
     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(); 
    } 
}); 

從你的代碼時,你ListView它會工作的項目,一旦點擊,然後只有你的按鈕將工作。所以請嘗試使用上述代碼。

+0

因此,在單擊某個項目之前,不會定義delete,因此此代碼在setOnClickListener崩潰 – njzk2

+0

@ njzk2您是否認爲我的代碼會崩潰 – Praveenkumar

+0

是的,它甚至在我點擊之前崩潰:( – sara

0

添加您的按鈕的onclicklistener你的ListView的onclicklistener之外 -

list1.setAdapter(new (youradaptername)(getApplicationContext())); 
list1.setOnItemClickListener(detaillistener); 


private OnItemClickListener detaillistener = new OnItemClickListener() 
    { 
    // to your Onclick action coding here 
    } 
    }; 

只是做同樣爲其他按鈕。 更多隻是去這個鏈接here

+0

其中我將定義按鈕?並獲取位置 – sara

+0

in onCreate函數並嘗試使用自定義適配器執行此操作並使用getView()執行onclick listener – Jpm

0

@sara你的代碼是非常正確的,但它不工作,因爲你沒有正確地通過context ..!

現在使用此代碼,將肯定工作:

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(ManageSection.this,cl,Toast.LENGTH_LONG).show(); 
        } 
      }); 
     } 

    }); 

我剛加入這行到我們的代碼:

Toast.makeText(ManageSection.this,cl,Toast.LENGTH_LONG).show(); 

現在就來試試,並告訴我,如果它是工作..