将上下文传递给ListView

时间:2015-09-22 14:43:51

标签: java android listview

我有一个名为Contacts的类,它接受来自另一个活动的数据并创建一个自定义ListView。我的困境在于我的CustomAdapter类。我尝试将我的Activity上下文传递给CustomAdapter的构造函数。我试过,使用 this,Contacts.this ,但没有一个正在运行。我的所有小部件都没有使用适当的文本进行更新。所以我的自定义列表视图将加载,但那里什么也没有。相反,我传递了getApplicationContext()。这有效,但我必须在我的自定义适配器类中设置Flags,我不确定它是否是最好的编程习惯。首先,这是我的联系人活动文件

public class Contacts extends AppCompatActivity implements View.OnClickListener {
private Button addContactBtn;
private ListView contactList;
private CustomAdapter customAdapter;
private ArrayList<String> names = new ArrayList<>();
private ArrayList<String> phoneNums = new ArrayList<>();
private ArrayList<String> emails = new ArrayList<>();
static final int REQUEST_CODE = 1;



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

    addContactBtn = (Button)findViewById(R.id.contactAddButton);
    contactList = (ListView)findViewById(R.id.contactList);

    addContactBtn.setOnClickListener(this);

    customAdapter = new CustomAdapter(getApplicationContext(),names,phoneNums,emails);
    contactList.setAdapter(customAdapter);
}


@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.contactAddButton:{
            Intent i = new Intent(Contacts.this, AddContact.class);
            startActivityForResult(i,REQUEST_CODE);

        }

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == REQUEST_CODE){
        if(resultCode == RESULT_OK){
            String name = data.getStringExtra("name");
            String phone = data.getStringExtra("phone");
            String email = data.getStringExtra("email");
            phoneNums.add(phone);
            names.add(name);
            emails.add(email);
        }
    }
}

}

接下来,这是我的CustomAdapter类。我在这里传递了我的背景,并且我已经解决了这个问题。我将onClick设置为某些小部件。我希望他们在点击时启动另一个意图,特别是打开android的拨号屏幕。一切都很好,但当我在CustomList视图中单击小部件时,应用程序崩溃了。我可以通过设置标志来解决这个问题,但我宁愿解决上下文问题。为什么我传递的上下文会导致此问题?我该如何解决?

public CustomAdapter(Context c,ArrayList<String>n, ArrayList<String>nums, ArrayList<String>e){

    context = c;
    phoneNumbers = nums;
    names = n;
    emails = e;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

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

@Override
public Object getItem(int position) {
    return position;
}

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

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

    if (view == null){
        view = inflater.inflate(R.layout.contacts_custom_row,null);
        TextView name = (TextView)view.findViewById(R.id.customRowContactName);
        TextView phone = (TextView)view.findViewById(R.id.customRowContactNumber);
        TextView email = (TextView)view.findViewById(R.id.customRowContactEmail);

        name.setText(names.get(position));
        phone.setText(phoneNumbers.get(position));
        email.setText(emails.get(position));

        phone.setOnClickListener(this);
    }
    return view;
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.customRowContactNumber:{
            Intent i = new Intent(Intent.ACTION_DIAL);
            context.startActivity(i);//THIS IS THE ISSUE I THINK
            break;
        }
    }

}

这是Log Cat错误:

09-22 10:38:25.679    2794-2794/com.markfeldman.mydoggydays E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.markfeldman.mydoggydays, PID: 2794
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:1238)
        at android.app.ContextImpl.startActivity(ContextImpl.java:1225)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:323)
        at com.markfeldman.mydoggydays.CustomAdapter.onClick(CustomAdapter.java:73)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:1)

您正在使用Context调用其他活动,因此它认为您尝试在活动外部进行调用,因此您需要使用标记FLAG_ACTIVITY_NEW_TASK。 相反,您可以尝试传递Activity。如下所示:

public CustomAdapter(Contacts activity,ArrayList<String>n, ArrayList<String>nums, ArrayList<String>e){

    mActivity = activity;
    phoneNumbers = nums;
    names = n;
    emails = e;
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.customRowContactNumber:{
            Intent i = new Intent(Intent.ACTION_DIAL);
            mActivity.startActivity(i);//THIS IS THE ISSUE I THINK
            break;
        }
    }

}

然后你就可以打电话:

customAdapter = new CustomAdapter(Contacts.this,names,phoneNums,emails);
contactList.setAdapter(customAdapter);