2013-06-28 54 views
0

我正在開發一個具有選項卡功能的android應用程序。從customlistadapter調用activtygroup

一切工作正常,但我無法從listadapter調用activtygroup。

這裏是我的名單適配器代碼:

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); 
    }*/ 
} 

現在我要打電話AddRemoveFriends activtygroup。

這裏是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); 
    } 

} 

請建議我如何從listadapter調用activtygroup。

+0

你不能從任何東西中調用ActivityGroup,因爲它不是「可調用的」,不管它是什麼意思 – pskink

+0

就像簡單的活動調用Intent intent = new Intent(this,AnotherActivty.class); startActivty(inetent); 我們不能從列表適配器調用活動組.. ..? –

+0

當你從適配器午餐AddRemoveFriendScreen時,你有logcat錯誤日誌嗎? – Zhar

回答

0

讓它像這樣你可以通過這樣做來訪問所有東西。

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

希望這會幫助你。

0

根據android developper reference,你可以使用Fragments。

ActivityGroup已被棄用。改用新的Fragment和FragmentManager API;這些也可以通過Android兼容性套件在較早的平臺上使用。

你可以試試這個技巧。

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

如果它不起作用,請在您的AddRemoveFriendScreen類中使用適配器的內部類。