Question

I am working on an android app which is having functionality of tabs.

Everything is working fine but I am not able to call activtygroup from listadapter.

Here is my code for list adapter:

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tv.socialgoal.R;
import com.tv.socialgoal.imageloader.ImageLoader;

public class AllyListAdapter extends BaseAdapter{
    Activity ctx;
    ArrayList<Object> alist;
    private ImageLoader imageLoader;

    AllyBean allyBean;
    private String photoPath;

    public AllyListAdapter(Activity ctx, ArrayList<Object> alist) {
        super();
        this.ctx = ctx;
        this.alist = alist;
        imageLoader=new ImageLoader(ctx);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return alist.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2) {
        LayoutInflater linf=(LayoutInflater) ctx.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
        View v=linf.inflate(R.layout.ally_list_row, null);
        TextView tv=(TextView)v.findViewById(R.id.allyName);
        ImageView profileImage=(ImageView)v.findViewById(R.id.ally_image);
        Button inviteBtn=(Button)v.findViewById(R.id.invite_btn);

        //SHOW DATA FROM LIST
        allyBean=(AllyBean)alist.get(position);
        tv.setText(allyBean.getName());
        photoPath=allyBean.getAvatar();

        profileImage.setTag(photoPath);
        imageLoader.displayImage(photoPath, ctx, profileImage, false);

        inviteBtn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Intent intent=new Intent(ctx,AddRemoveFriendScreen.class);
                //intent.putExtra("friendId", allyBean.getUserId());
                ctx.startActivity(intent);
            }
        });
        return v;
    }

    /*public void replaceContentView(String id, Intent newIntent) {
        View view =getLocalActivityManager().startActivity(id,
                newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();
        this.setContentView(view);
    }*/
}

Now I have to call AddRemoveFriends activtygroup.

Here is the code for activtygroup:

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.tv.servercommunication.IServerResponse;
import com.tv.servercommunication.WebServiceCommunicator;
import com.tv.socialgoal.Constant;
import com.tv.socialgoal.R;
import com.tv.socialgoal.network.NetworkAvailablity;
import com.tv.task.TabViewActivity;

public class AddRemoveFriendScreen extends ActivityGroup implements OnClickListener, IServerResponse{

    Button backBtn;
    Button addRemoveFriendBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends_profile_screen);

        backBtn=(Button)findViewById(R.id.back_button);
        addRemoveFriendBtn=(Button)findViewById(R.id.add_remove_frnd_btn);

        backBtn.setOnClickListener(this);
        addRemoveFriendBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.back_button:

            break;

        case R.id.add_remove_frnd_btn:
            callAddFriend_WS();
            break;

        default:
            break;
        }

    }

    private Handler _handler = new Handler() {
        public void dispatchMessage(Message msg) {
            switch (msg.arg1) {
            case Constant.PID_ADD_REMOVE_FRIEND:
                if (parseResponse(msg.obj.toString()) == true) {
                    Intent intent = new Intent(getParent(),
                            TabViewActivity.class);
                    startActivity(intent);
                } else {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Constant.showAlertDialog(
                                    Constant.DIALOG_TITLE_ERROR,
                                    "Invalid username or password.",
                                    getParent(), false);
                        }
                    });
                }
                break;

            default:
                break;
            }
        }
    };

    // GET USER ACCESSTOCKEN AND USER ID
    private boolean parseResponse(String response) {

        String message = null;
        JSONObject post;

        boolean isUserInfoAvail = false;
        try {
            JSONObject postjsonObject = new JSONObject(response);
            JSONObject posts = postjsonObject.getJSONObject("posts");
            post = posts.getJSONObject("post");
            message = post.getString("message");

            if (message.equalsIgnoreCase("failure")) {
                isUserInfoAvail = false;
            } else {
                isUserInfoAvail = true;
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return isUserInfoAvail;
    }

    public void callAddFriend_WS() {

        if (NetworkAvailablity.checkNetworkStatus(AddRemoveFriendScreen.this)) {
            // PREPARE URL
            Constant.methodURL = "http://admin.tvdevphp.com/goalmachine/add_friend.php";

            // PREPARE REQUEST PARAMETER
            ArrayList<NameValuePair> requestParaList = new ArrayList<NameValuePair>();
            requestParaList.add(new BasicNameValuePair("self_user_id", "1"));
            requestParaList.add(new BasicNameValuePair("user_friend_id", "2"));

            // CALL WEBSERVICE
            WebServiceCommunicator.getInstance().registerForServerResponse(
                    AddRemoveFriendScreen.this);
            WebServiceCommunicator.getInstance().callGetAppWebService(
                    Constant.showDialog, getParent(),
                    Constant.methodURL, getParent(), Constant.PID_ADD_REMOVE_FRIEND,
                    false, requestParaList);
        } else {
            Constant.showAlertDialog(Constant.errorTitle,
                    Constant.MSG_CHECK_INTERNET_SETTING, getParent(),
                    false);

        }
    }

    // SERVER RESPONSE METHOD
    public void serverResponse(String response, int processid) {
        Message msg = new Message();
        msg.arg1 = processid;
        msg.obj = response;
        _handler.dispatchMessage(msg);
    }

}

Please suggest me how to call activtygroup from listadapter.

Was it helpful?

Solution

Make it like this you can access everything by doing like this.

 public class AddRemoveFriendScreen extends ActivityGroup implements OnClickListener, IServerResponse
   {

       @Override
         protected void onCreate(Bundle savedInstanceState) 
           {
            .......
            } 

        class AllyListAdapter extends BaseAdapter
        {
                //Now you can call everything from ActivityGroup
        } 
   } 

Hope this will help you.

OTHER TIPS

According to the android developper reference, you may use Fragments.

ActivityGroup is deprecated. Use the new Fragment and FragmentManager APIs instead; these are also available on older platforms through the Android compatibility package.

You can try this trick.

inviteBtn.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        if(ctx instanceof ActivityGroup){
            Intent intent = new Intent(ctx.getParent(),AddRemoveFriendScreen.class);
            //intent.putExtra("friendId", allyBean.getUserId());
            ctx.getParent().startActivity(intent);
        }
        else{
            Intent intent = new Intent(ctx,AddRemoveFriendScreen.class);
            //intent.putExtra("friendId", allyBean.getUserId());
            ctx.startActivity(intent);
        }
    }
});

If it doesn't work just use internal class for your adapter in your AddRemoveFriendScreen class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top