Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

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;

    }
}
like image 261
Yeahia2508 Avatar asked Sep 28 '22 19:09

Yeahia2508


2 Answers

I think that you are new in android. It's not the good solution but it will help you. After the request button Set a thread for 3 second in you adapter class and make a flag called isSend() in your project. Change the flag in your sending asynctask onpost() method. than check the flag in your thread. Hope it will help you.

like image 140
Sayem Al Hasan Avatar answered Oct 05 '22 23:10

Sayem Al Hasan


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.

  1. Now let SubjectAdapter implement this Interface. -> the onRequestSended() changes the appearance of the button.

  2. Create a ctor in SendRequest, which expect a RequestStatusCallback instance. save it as Instance variable.

  3. 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...

like image 32
Paul Reznik Avatar answered Oct 05 '22 22:10

Paul Reznik