如何从非活动类开始另一个活动?

时间:2014-10-06 11:29:51

标签: android android-intent onclicklistener baseadapter

我在一个扩展onClickListener的类中有baseAdapter,我想从中开始另一个活动,但 Eclipse 不接受{{1} }。它给出了以下错误:

方法startActivity(Intent)未定义类型new View.OnClickListener(){}

我该如何解决这个问题?

startActivity(Intent)

这是我的日志:

    public class FeedListAdapter extends BaseAdapter {  
    private Context mContext;
    public FeedListAdapter(Context cxt) {
        mContext = cxt;
    }
    public Activity activity;
    private LayoutInflater inflater;
    private List<FeedItem> feedItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

    public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
        this.activity = activity;
        this.feedItems = feedItems;
    }

    @Override
    public int getCount() {
        return feedItems.size();
    }

    @Override
    public Object getItem(int location) {
        return feedItems.get(location);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (inflater == null)
            inflater = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null)
            convertView = inflater.inflate(R.layout.feed_item, null);

        if (imageLoader == null)
            imageLoader = AppController.getInstance().getImageLoader();

        TextView name = (TextView) convertView.findViewById(R.id.name);
        final TextView timestamp = (TextView) convertView
                .findViewById(R.id.timestamp);
        TextView statusMsg = (TextView) convertView
                .findViewById(R.id.txtStatusMsg);
        TextView url = (TextView) convertView.findViewById(R.id.txtUrl);
        NetworkImageView profilePic = (NetworkImageView) convertView
                .findViewById(R.id.profilePic);
        FeedImageView feedImageView = (FeedImageView) convertView
                .findViewById(R.id.feedImage1);

        FeedItem item = feedItems.get(position);

        name.setText(item.getName());

        // Converting timestamp into x ago format
        //CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
                //Long.parseLong(item.getTimeStamp()),
                //System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
        timestamp.setText(item.getTimeStamp());

        // Chcek for empty status message
        if (!TextUtils.isEmpty(item.getStatus())) {
            statusMsg.setText(item.getStatus());
            statusMsg.setVisibility(View.VISIBLE);
        } else {
            // status is empty, remove from view
            statusMsg.setVisibility(View.GONE);
        }

        // Checking for null feed url
        if (item.getUrl() != null) {
            url.setText(Html.fromHtml("<a href=\"" + item.getUrl() + "\">"
                    + item.getUrl() + "</a> "));

            // Making url clickable
            url.setMovementMethod(LinkMovementMethod.getInstance());
            url.setVisibility(View.VISIBLE);
        } else {
            // url is null, remove from the view
            url.setVisibility(View.GONE);
        }

        // user profile pic
        profilePic.setImageUrl(item.getProfilePic(), imageLoader);

        // Feed image
        if (item.getImge() != null) {
            feedImageView.setImageUrl(item.getImge(), imageLoader);
            feedImageView.setVisibility(View.VISIBLE);
            feedImageView
                    .setResponseObserver(new FeedImageView.ResponseObserver() {
                        @Override
                        public void onError() {
                        }

                        @Override
                        public void onSuccess() {
                        }
                    });
        } else {
            feedImageView.setVisibility(View.GONE);
        }

        //onclick listner for listview items ;)

        convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // Get the position
                Intent intent = new Intent(mContext, FinalShow.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("NAME", feedItems.get(+position).getName());
                mContext.startActivity(intent);

            }
        });


        return convertView;
    }

}

9 个答案:

答案 0 :(得分:1)

如果您将此适配器用于listViewGridView,则需要申请:

 setOnItemClickListener() 

listViewGridView以及

 onItemClicked()

方法,您启动所需的活动。

即。写:

        Intent intent = new Intent("info.androidhive.listviewfeed.adapter.FINALSHOW");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("NAME", feedItems.get(+position).getName());
        context.startActivity(intent);

onItemClicked()方法上的此代码。

答案 1 :(得分:0)

在开始活动之前尝试此设置此标志:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

希望它能奏效。

像这样:

convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Get the position
            Intent intent = new Intent("info.androidhive.listviewfeed.adapter.FINALSHOW");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("NAME", feedItems.get(+position).getName());
            context.startActivity(intent);
        }
    });

答案 2 :(得分:0)

您可以通过以下代码

来完成
Intent intent = new Intent(context, YourNewActivity.class);
context.startActivity(intent);

答案 3 :(得分:0)

在适配器类的构造函数中传递活动的上下文

private Activity activity;
    private LayoutInflater inflater;
    private List<FeedItem> feedItems;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();


    public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
        this.activity = activity;
        this.feedItems = feedItems;

    }

&安培;

this.activity.startActivity(intent);

答案 4 :(得分:0)

首先,如果要启动Activity,则应使用Context或其子类。 通常,我们可以通过构造函数传递Context。例如,你的班级:

public class YOURClass implements OnClickListener {
    private Context mContext;
    public YOURClass(Context cxt) {
        mContext = cxt;
        ...
    }
    ...
    @Override
    OnClick(View v) {
    ...
        mContext.startActivity(intent);
    ...
    }
}

在其他活动/服务中......

...
YOURClass your = new YOURClass(cxt);
...

答案 5 :(得分:0)

我有这个: 我的主要活动中的listAdapter = new FeedListAdapter(this, feedItems);,以及我的非活动类构造函数

public FeedListAdapter(Activity activity, List<FeedItem> feedItems) {
        this.activity = activity;

        this.feedItems = feedItems;
    }

所以这对我有用:

convertView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent intent = new Intent(activity, FinalShow.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);



            }
        });

谢谢大家:)

答案 6 :(得分:0)

创建另一个界面/回调,通知活动该视图已被点击

例如

public static interface MyCallback
{
    void onCustomElementClicked(View v);
}

并从活动

设置适配器的接口
MainActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         //Setup views and etc...
         FeedListAdapter adapter = new FeedListAdapter(...);
         adapter.setCustomCallback(new MyCallback()
         {
              void onCustomElementClicked(View arg0)
              {
                 // Get the position
                 Intent intent = new Intent(MyActivity.this, FinalShow.class);
                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 intent.putExtra("NAME", feedItems.get(+position).getName());
                 startActivity(intent);
              }
         });
         //... The rest of your code
    }
}

并在你的适配器

convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            myCallback.onCustomElementClicked(arg0);
        }
    });

答案 7 :(得分:0)

从FeedListAdapter类中删除它

convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(activity, FinalShow.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);



        }
    });

并在您的主要活动类

中添加此内容
        Your_ListView_object.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(this, FinalShow.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });

希望你能从中理解......

答案 8 :(得分:0)

您可以向班级发送语境,

 public XXX XXX(Context context , ...){
this.context = context ; 
.
.

像这样的开始活动

    context.startActivity(intent);