Q:

How to Change Listview Button name After sending a server request inside a custom adapter class?

Recently I'm working on a online based Student Teacher communication application. In this app there is a part that Student have to send teacher a request to add in his class.

Here is the Custom Adapter getView code where i'm sending the request by using listview custiom button. I want to place the asynctask code inside this adapter class.But I can't able to do that. Inside listview onclick button method can't recognizing the asynctask method. so i placed the asynctask method in another class. I want to place asyntask method in this adapter class because i want to change the button name "Request" to "Sent" after sending the request in onPostExecute method . I manually changed the button name but i want to ensure the user that request is 100% sent. So Please tell me  where i have to place the asynctask method in this adapter class so that i can change the button name. Million Thanks in Advance. 
package project.cc.student;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.example.connectifyclassroom.R;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class SubjectAdapter extends ArrayAdapter<Subject> {
    ArrayList<Subject> contactList;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;
    int studentID;
    SendRequest sendRequest;
    String requestUrl;
    ArrayList<NameValuePair> params;
    public SubjectAdapter(Context context, int resource, ArrayList<Subject> objects,int studentID) {
        super(context, resource, objects);
        vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        contactList = objects;
        this.studentID = studentID;
        Log.d("bug", "studentID" + studentID);
        params = new ArrayList<NameValuePair>();

    }
        @Override
        public View getView( final int position, View convertView, ViewGroup parent) {
            // convert view = design
            View v = convertView;
            if (v == null) {
                holder = new ViewHolder();
                v = vi.inflate(Resource, null);
                holder.tvSubject = (TextView) v.findViewById(R.id.tvSujectName);
                holder.tvTeacher = (TextView) v.findViewById(R.id.tvTeacherName);
                holder.btSendRequest =  (Button) v.findViewById(R.id.bt_send_subject_request);
                holder.btSendRequest.setOnClickListener(new Button.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // TODO Auto-generated method stub

                        Integer pos = (Integer)view.getTag();
                        Button b = (Button) view;
                        sendRequest = new SendRequest();
                        Toast.makeText(getContext(),"Please Wait...", Toast.LENGTH_SHORT).show();

                        params.add(new BasicNameValuePair("studentID", Integer.toString(studentID)));
                        params.add(new BasicNameValuePair("teacherID", Integer.toString(contactList.get(pos).getTeacherUid())));
                        params.add(new BasicNameValuePair("subjectID", Integer.toString(contactList.get(pos).getSubjectUid())));
                        sendRequest.setPair(params);
                        sendRequest.startRequest();
                        Toast.makeText(getContext(),"Request Sent", Toast.LENGTH_SHORT).show();
                        b.setText("Sent");

                    }
                });
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }

        holder.tvSubject.setText(contactList.get(position).getSubject());
        holder.tvTeacher.setText(contactList.get(position).getTeacher());
        holder.btSendRequest.setTag(position);

        return v;

    }

    static class ViewHolder {
        public TextView tvSubject;
        public TextView tvTeacher;
        public Button btSendRequest;

    }
}

A:

I would recomend you to solve this problem via a callback.
To do this, create an Interface (e.g. RequestStatusCallback) which contains the method onRequestSended.

Now let SubjectAdapter implement this Interface.
-> the onRequestSended() changes the appearance of the button.
Create a ctor in SendRequest, which expect a RequestStatusCallback instance. save it as Instance variable.
if the SendRequest has sended the request succesfully, call the onRequestSended method of the callback.

so, only if the request has been sended, the button will be updated.
now you can add further callbacks like "onRequestFailed()" and so on...

