View Changelog

I want to use a custom List adapter in this example to take advantage of getView() and then enter the id for the buttons in a list view.

So that I can get (student_id) student from the database as a list using the delete buttons.

And I want to implement delete so that when user clicks delete he takes the student id and removes it from the database.

How can I change my adapter here to my own list adapter?

My code is as follows:

 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 url * */ @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 } } } 

Please note: if there is an easy way without changing the adapter, I can make an id for each button. Because I heard that I have to use a custom list adapter ...

+4
source share
2 answers

Use setListAdapter here to use a custom adapter for your ListActivity: http://developer.android.com/reference/android/app/ListActivity.html#setListAdapter(android.widget.ListAdapter )

Then you can mark your views with your identifiers, etc.

Another way to do this without a user adapter (simpler but not preferred) is to manually map all entries to a position in the list. (since you can provide an array of elements, you can just use the position to get the object) At the same time, if you just want to connect the buttons, you can let onClick record the position of the element, and then when they click the button, you can respond correctly by accessing the array.

EDIT:

In your onCreate do:

getListView (). setOnItemClickListener (new OnItemClickListener () {

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

And then in your onClickListener button you just delete (selected_student);

+2
source

You will need a click listener for your list!

Here is an example:

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

That is all you need!

+1
source

Source: https://habr.com/ru/post/1438691/


All Articles