从可扩展列表视图适配器中启动新活动

时间:2013-05-25 23:09:20

标签: android expandablelistadapter

我已将我在网上找到的自定义展开式列表视图集成到我正在处理的项目中。我遇到的问题是从可扩展列表视图适配器中调用一个新的Activity。下面是适配器代码的一部分。当调用context.startActivity()时,我得到一个空异常错误。

关于如何调用新活动的任何想法

package com.example.apple;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;

@SuppressWarnings("unchecked")
public class NewAdapter extends BaseExpandableListAdapter {

 public ArrayList<String> groupItem, tempChild;
 public ArrayList<String> childDateTime;
 public ArrayList<Object> Childtem = new ArrayList<Object>();
 public LayoutInflater minflater;
 public Activity activity;
 private Context context;

 public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem, ArrayList<String> childDT) {
  groupItem = grList;
  this.Childtem = childItem;

  childDateTime = childDT;
 }

 public NewAdapter(Context context) {
     this.context = context;     
}

 public void setInflater(LayoutInflater mInflater, Activity act) {
  this.minflater = mInflater;
  activity = act;
 }

 @Override
 public Object getChild(int groupPosition, int childPosition) {
  return null;
 }

 @Override
 public long getChildId(int groupPosition, int childPosition) {
  return 0;
 }

 @Override
 public View getChildView(int groupPosition, final int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent) {
  tempChild = (ArrayList<String>) Childtem.get(groupPosition);
  TextView text = null;
  if (convertView == null) {
   convertView = minflater.inflate(R.layout.activity_main_child, null);
  }
  text = (TextView) convertView.findViewById(R.id.textView1);
  text.setText(tempChild.get(childPosition));

  TextView dateTime = null;
  dateTime = (TextView) convertView.findViewById(R.id.textDateTime);
  dateTime.setText(childDateTime.get(groupPosition));

  convertView.setOnClickListener(new OnClickListener() {

@Override
   public void onClick(View v) {

       Intent intent= new Intent(context, ViewSomeStuff.class);

       context.startActivity(intent);
   }
  });
  return convertView;
 }

1 个答案:

答案 0 :(得分:2)

public NewAdapter(Context context, ArrayList<String> grList, ArrayList<Object> childItem, ArrayList<String> childDT) {
  groupItem = grList;
  this.Childtem = childItem;

  childDateTime = childDT;
  this.context = context; 
 }