Android适配器的getView()没有调用活动

时间:2015-10-30 06:02:08

标签: java android adapter getview

我正在尝试从适配器类调用一个活动。我的activity类没有构造函数,只有onCreate()。如何从getView()拨打电话?我搜索了解决方案,但它没有用。

我的适配器getView()

public View getView(int position, View convertView, ViewGroup parent) {
        //SharedPostView holder;
        PostActivity holder;



        if (convertView == null) {
            Log.d("network frag list"," convertView null ");
            LayoutInflater inflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.activity_post, null);
            holder = new PostActivity();
            convertView.setTag(holder);
        } else {
            Log.d("network frag list"," convertView.getTag() ");
            holder = (PostActivity)convertView.getTag();
        }
        holder.init(position);
        Log.d("network frag list", " getView");


        return convertView;
}

3 个答案:

答案 0 :(得分:1)

如果我理解正确,您正尝试从列表视图中启动其他活动。为此你需要一个意图。

Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);

Read more

答案 1 :(得分:1)

试试这个,

这是来自适配器类的 getView

@Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        // TODO Auto-generated method stub
        View myView = convertView;
        final ViewHolder holder;
        try {

            if (myView == null) {
                LayoutInflater inflater = LayoutInflater.from(context);
                myView = inflater.inflate(R.layout.row_affiliates_list, null);
                holder = new ViewHolder();

                holder.txtAffiliateHeader = (TextView) myView.findViewById(R.id.txtAffiliateHeader);

                holder.rlMain = (RelativeLayout) myView.findViewById(R.id.rlMain);
                holder.viewLine = (TextView) myView.findViewById(R.id.viewLine);

                myView.setTag(holder);
            } else {
                holder = (ViewHolder) myView.getTag();
            }
            holder.txtAffiliateHeader.setText(""+ AffiliatesList.get(position).getStrAffiliateTitle());

            holder.rlMain.setTag(position);
            holder.rlMain.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int arg2 = Integer.parseInt(v.getTag().toString());

                    Intent intent = new Intent(context, SecondActivity.class);
                    context.startActivity(intent);
                }
            });

            return myView;
        } catch (Exception e) {
            return myView;
        }
    }

这是您可以通过单击按钮或完整相对布局从适配器类调用活动。

答案 2 :(得分:0)

您需要了解方法startActivity()的可访问性。点击ctrl+click中的Android Studio后,您可以看到此方法的详细信息。以下是详细信息,活动的类层次结构如下 -

Context (android.content)
   ContextWrapper (android.content)
     ContextThemeWrapper (android.view)
       Activity (android.app)

Context类有一个抽象方法。

 public abstract void startActivity(Intent intent);

这在课程ContextWrapperActivity中被覆盖。 因此,您可以从任何派生类或派生类的引用中调用方法startActivity(Intent intent)

所以你已经引用了Context你可以将方法称为 -

context.startActivity(intent)