从ArrayAdapter

时间:2017-03-29 06:54:21

标签: android android-arrayadapter start-activity

我有一个扩展ArrayAdapter的自定义适配器。单击EMAIL时我想转到MapActivity。如果我删除了意图部分,则会显示项目的正确位置。但是,如果我添加startActivity函数,应用程序崩溃(没有吐司)。我删除了不相关的代码,例如获取距离和setters.getters。任何帮助或指针将不胜感激!

ArrayAdapter代码

import android.content.Context;
import android.content.Intent;
import android.location.Location;
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;

import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.firebase.auth.FirebaseAuth;

import java.util.ArrayList;
import java.util.List;

public class TrackAdapter extends ArrayAdapter{

    private Context c;
    //ArrayLists to save items

    public TrackAdapter(Context context, int resource)
    {
        super(context, resource);
        this.c = context;
    }

    public void add(/*items to be added*/)
    {
       //add to ArrayLists and super.add()
    }

    static class RowHolder
    {
        TextView EMAIL;
        TextView DISTANCE;
    }

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

    @Override
    public Object getItem(int position)
    {
        return this.email.get(position);
    }

    /*other setters and getters*/

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row;
        row = convertView;
        RowHolder holder;

        final int currentPosition = position;

        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.track_item, parent, false);

            holder = new RowHolder();

            holder.EMAIL = (TextView) row.findViewById(R.id.track_item_text);
            holder.DISTANCE = (TextView) row.findViewById(R.id.track_item_distance);

            holder.EMAIL.setOnClickListener(new View.OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    Toast.makeText(c, "item at position " + currentPosition, Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(c, MapActivity.class);
                    c.startActivity(intent);
                }
            });

            row.setTag(holder);
        }
        else
        {
            holder = (RowHolder) row.getTag();
        }


        String EM = (String) getItem(position);
        holder.EMAIL.setText(EM);

        //calculate and display distance

        return row;

    }
}

我也尝试用c.getApplicationContext()v.getContext()替换c,这也不起作用。

编辑 - 我在模拟器上运行应用程序,然后转到MapActivity。但是,当在通过USB连接的Android设备上运行时,应用程序崩溃

EDIT2 - 这是崩溃报告

--------- beginning of crash
03-29 03:23:42.036 7768-7768/com.example.*******
E/AndroidRuntime: FATAL EXCEPTION: main

                                                                                android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
                                                                                    at android.app.ContextImpl.startActivity(ContextImpl.java:672)
                                                                                at android.app.ContextImpl.startActivity(ContextImpl.java:659)
                                                                                at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
                                                                                at com.example.ankit.findyourfellow.TrackAdapter$1.onClick(TrackAdapter.java:127)
                                                                                at android.view.View.performClick(View.java:5204)
                                                                                at android.view.View$PerformClick.run(View.java:21153)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

5 个答案:

答案 0 :(得分:2)

在点击监听器内部执行以下操作:

holder.EMAIL.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                Toast.makeText(c, "item at position " + currentPosition, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(c, MapActivity.class);
                //if you want to send data to called activity uncomment next line
                // intent.putExtra("extra", "value"); 

                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                c.startActivity(intent);
            }
        });

它应该工作:)

答案 1 :(得分:1)

更新您的代码:

openssl x509 -in cert.txt -outform der -out cert.der

在setText标签之后或返回之前调用setOnClickListener。

public Context c;

// Contractor   
public TrackAdapter(Context mcontext, int resource)
{
    super(mcontext, R.layout.track_item, resource);
    this.c= mcontext;
    this.resource = resource;
}

答案 2 :(得分:1)

为您的意图添加标记Intent.FLAG_ACTIVITY_NEW_TASK

在启动intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)之前使用MapActivity

试试这个:

 Intent intent = new Intent(c, MapActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 c.startActivity(intent);

答案 3 :(得分:1)

即使它有效,我也不确定intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);是否需要解决方案! 你在哪里实例化ArrayAdapter?你能尝试将背景投射到活动中吗? Intent intent = new Intent((Activity)c, MapActivity.class);
我也想知道为什么new Intent(v.getContext(), MapActivity.class);不起作用?

<强>更新
我发现了什么问题......你需要从applicationContext以外的上下文开始活动!

  

从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志

您的Context c似乎实际上是应用上下文...
所以这是放入onClick

的工作代码
Intent intent = new Intent(c, MapActivity.class); 
v.getContext().startActivity(intent);

答案 4 :(得分:0)

将以下代码添加到适配器类

ClickListener mClickListener;
public void setClickListener(ClickListener clickListener) {
        this.mClickListener = clickListener;
    }

    public interface ClickListener {
        void onItemClick();
    }

现在在Activity类中添加这个你设置适配器的地方:

mAdapter.setClickListener(new TrackAdapter.ClickListener() {

            @Override
            public void onItemClick() {
                 Intent intent = new Intent(YourActivity.this, MapActivity.class);
                 startActivity(intent);
            }
}

比以下适配器类调用的onClick方法:

if (mClickListener != null ) {
                             mClickListener.onItemClick();
                        }