使用另一个活动中的ExpandableListView启动活动

时间:2014-05-09 10:00:03

标签: java android expandablelistview

你好我想开始一个活动,还有另一个活动的列表视图,但我不明白该怎么做。

这是可扩展适配器

public class MyExpandableAdapter extends BaseExpandableListAdapter 
{

@SuppressWarnings("unused")
private Activity activity;
private ArrayList<Object> childtems;
private LayoutInflater inflater;
private ArrayList<String> parentItems, child;

// constructor
public MyExpandableAdapter(ArrayList<String> parents, ArrayList<Object> childern)
{
    this.parentItems = parents;
    this.childtems = childern;
}

public void setInflater(LayoutInflater inflater, Activity activity) 
{
    this.inflater = inflater;
    this.activity = activity;
}

// method getChildView is called automatically for each child view.
//  Implement this method as per your requirement
@SuppressWarnings("unchecked")
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
{

    child = (ArrayList<String>) childtems.get(groupPosition);

    TextView textView = null;

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.child_view, null);
    }

     // get the textView reference and set the value
    textView = (TextView) convertView.findViewById(R.id.textViewChild);
    textView.setText(child.get(childPosition));

    // set the ClickListener to handle the click event on child item
   /* convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(activity, child.get(childPosition),
                    Toast.LENGTH_SHORT).show();
        }
    }); */
    return convertView;
}

// method getGroupView is called automatically for each parent item
// Implement this method as per your requirement
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
{

    if (convertView == null) {
        convertView = inflater.inflate(R.layout.parent_view, null);
    }

    ((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
    ((CheckedTextView) convertView).setChecked(isExpanded);

    return convertView;
}

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

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

@SuppressWarnings("unchecked")
@Override
public int getChildrenCount(int groupPosition) 
{
    return ((ArrayList<String>) childtems.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) 
{
    return null;
}

@Override
public int getGroupCount() 
{
    return parentItems.size();
}

@Override
public void onGroupCollapsed(int groupPosition) 
{
    super.onGroupCollapsed(groupPosition);
}

@Override
public void onGroupExpanded(int groupPosition)
{
    super.onGroupExpanded(groupPosition);
}

@Override
public long getGroupId(int groupPosition) 
{
    return 0;
}

@Override
public boolean hasStableIds() 
{
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
    return false;
}

}

这是我的第一个活动

public class ConversationsListActivity extends   ConversationsEssentialActivity{

private String[] drawerListViewItems;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.conversations_list);
    ActionBar actionBar = getActionBar();
    actionBar.show();
    FontHelper.applyFont(this, findViewById(R.id.phrasebookList), "fonts/Roboto-Regular.ttf"); /**  **/

    listView = (ListView) findViewById(R.id.conversationsList);
    drawerListViewItems = getResources().getStringArray(R.array.conversations_list_array);
    listView.setAdapter(new ArrayAdapter<String>(this,R.layout.conversations_list_items, drawerListViewItems));
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            switch (position){

                case 0:{

                    Intent Info = new Intent(ConversationsListActivity.this, ConversationsEssentialActivity.class);
                    startActivityForResult(Info, position);

                    setGroupParentsEssential();
                    setChildDataEssential();

                }
                break;


            }

        }
    });

}
}

这是我的第二项活动

public class ConversationsEssentialActivity extends   Activity{

  // Create ArrayList to hold parent Items and Child Items
  ArrayList<String> parentItems = new ArrayList<String>();
  ArrayList<Object> childItems = new ArrayList<Object>();
  ExpandableListView list;
  MyExpandableAdapter adapter2;
@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  ActionBar actionBar = getActionBar();
  actionBar.show();
  list = new ExpandableListView(this);

  list.setDividerHeight(2);
  list.setGroupIndicator(null);
  list.setClickable(true);

  adapter2 = new MyExpandableAdapter(parentItems, childItems);

  adapter2.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);

  // Set the Adapter to expandableList
  list.setAdapter(adapter2);

  setContentView(list);

}

   // method to add parent Items
  public void setGroupParentsEssential() 
  {

  parentItems.add(getResources().getString(R.string.essential_1));
  parentItems.add(getResources().getString(R.string.essential_2));
  parentItems.add(getResources().getString(R.string.essential_3));
  parentItems.add(getResources().getString(R.string.essential_4));
  parentItems.add(getResources().getString(R.string.essential_5));


  }

  // method to set child data of each parent
  public void setChildDataEssential() 
  {


  ArrayList<String> child = new ArrayList<String>();
  child.add(getResources().getString(R.string.essential_1t));
  childItems.add(child);


  child = new ArrayList<String>();
  child.add(getResources().getString(R.string.essential_2t));
  childItems.add(child);


  child = new ArrayList<String>();
  child.add(getResources().getString(R.string.essential_3t));
  childItems.add(child);


  child = new ArrayList<String>();
  child.add(getResources().getString(R.string.essential_4t));
  childItems.add(child);


  child = new ArrayList<String>();
  child.add(getResources().getString(R.string.essential_5t));
  childItems.add(child);


  }

}

child_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:paddingLeft="0dp"
android:orientation="vertical"
android:id="@+id/childView"  >


    <TextView
        android:id="@+id/textViewChild"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:textSize="20sp"
        android:textColor="#75a800"
        android:padding="10dp" />

</LinearLayout>

parent_view.xml

<CheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textViewGroupName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:gravity="center_vertical"
    android:textSize="20sp"
    android:padding="10dp" /> 

第一个活动,点击,应该开始第二个活动,并启动setGroupParentsEssential();和setChildDataEssential();这是在第二个活动中宣布的。但是点击它,它只是用空白屏幕打开第二个活动。

有人为我提供了如何解决此问题的示例吗?

谢谢

1 个答案:

答案 0 :(得分:0)

如果您已为活动创建了布局,请尝试使用以下方法引用此布局:

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.yourxmlfile);
   ...
}

布局本身应该定义listview