2012-10-09 31 views
4

我想在这个例子中的自定义List adapter使用,这样我可以得到的getView() 的利益,然后拿到ID的按钮列表视图变化列表视图

这样我就可以从数据库检索(name_id)学生作为带有delete按钮的列表。

我想要实现delete,这样当用户点击delete将采取id学生,并从数据库中删除。

如何将我的适配器更改为自定义的List适配器?

我的代码如下:

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; 

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"; 
// 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"; 
// course JSONArray 
JSONArray student = null; 
private TextView mDateDisplay; 
private int mYear; 
private int mMonth; 
private int mDay; 

    @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>>(); 



    // on seleting single course 
     // launching Edit course Screen 
     // on seleting single course 
      // launching Edit course Screen 




     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); 

         // 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 

     } 


    } 
} 

注意,如果不改变适配器一个简单的方法,我可以为每个按键的id。 因为听说我必须使用自定义列表适配器...

回答

2

使用这里setListAdapter使用自定义的适配器为您的ListActivity:http://developer.android.com/reference/android/app/ListActivity.html#setListAdapter(android.widget.ListAdapter

然后您将能够使用他们的ID等来标记您的意见

没有自定义适配器(更简单但不是首选)的另一种方法是将所有条目手动映射到列表上的位置。 (因为你可以提供一组元素,所以你可以使用该位置来获取对象)。这就是说,如果你只是想连接按钮,你可以让onClick记录元素的位置,然后当他们按下按钮时,你可以通过访问数组来正确地作出反应。

编辑:

在你的onCreate做:

getListView()setOnItemClickListener(新OnItemClickListener(){

public void onItemClick(AdapterView<?> parent, View view, final int pos, long id){ 
    selected_student=studentList.get(pos); //member of your activity. 
}); 

然后在你的按钮onClickListener你只是做删除(selected_student) ;

+1

你可以解释更多关于位置我怎么能做到这一点 – sara

+1

添加示例代码。 – Edison

+0

感谢我做getListView()setOnItemClickListener(新OnItemClickListener(){ 公共无效onItemClick(适配器视图父,浏览视图,最终诠释POS,长ID) { selected_student = studentList.get(POS); //构件您的活动 }); }但它说错误的第一行?为什么? – sara

1

您将需要一个咔嗒咔嗒声为您的名单!

这里是一个例子;

list.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, final int arg2, long arg3) { 
       //get your information through array.get(arg0), find and delete in database, delete from your arraylist here, then call adapter.notifyDataSetChanged() 


       }); 

这就是你需要的一切!

+1

所以在这些方法中,首先我会做按钮d =(按钮)查找...然后d.setonclick ....然后在set中点击我怎样才能从textview中得到id?然后我使删除表单数据库正确 – sara

+1

也在哪里我会把这个代码 – sara