0

我正在编写一个媒体播放器应用程序。单击播放列表按钮时,ExpandList Activity 启动,根据单击的子项,它应该启动我的主要活动并返回子位置。但它并没有做任何事情。没有错误或任何东西。似乎它甚至没有进入 onChildClick 方法,我不知道为什么。我看过很多例子,他们似乎都和我一样。我希望有人能指出我做错了什么。下面是我的代码。谢谢

import java.util.ArrayList;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.Toast;

public class ExpandListActivity extends ExpandableListActivity implements
OnChildClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ExpandableListView expandbleLis = getExpandableListView();
    expandbleLis.setDividerHeight(2);
    expandbleLis.setGroupIndicator(null);
    expandbleLis.setClickable(true);
    expandbleLis.setOnChildClickListener(this);

    setGroupData();
    setChildGroupData();

    NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem);
    mNewAdapter
    .setInflater(
            (LayoutInflater)       getSystemService(Context.LAYOUT_INFLATER_SERVICE),
            this);
    expandbleLis.setAdapter(mNewAdapter);

}

public void setGroupData() {
    groupItem.add("TechNology");
    groupItem.add("Mobile");
    groupItem.add("Manufacturer");
    groupItem.add("Extras");
}

ArrayList<String> groupItem = new ArrayList<String>();
ArrayList<Object> childItem = new ArrayList<Object>();

public void setChildGroupData() {
    /**
     * Add Data For TecthNology
     */
    ArrayList<String> child = new ArrayList<String>();
    child.add("Java");
    child.add("Drupal");
    child.add(".Net Framework");
    child.add("PHP");
    childItem.add(child);

    /**
     * Add Data For Mobile
     */
    child = new ArrayList<String>();
    child.add("Android");
    child.add("Window Mobile");
    child.add("iPHone");
    child.add("Blackberry");
    childItem.add(child);
    /**
     * Add Data For Manufacture
     */
    child = new ArrayList<String>();
    child.add("HTC");
    child.add("Apple");
    child.add("Samsung");
    child.add("Nokia");
    childItem.add(child);
    /**
     * Add Data For Extras
     */
    child = new ArrayList<String>();
    child.add("Contact Us");
    child.add("About Us");
    child.add("Location");
    child.add("Root Cause");
    childItem.add(child);
}


@Override
public boolean onChildClick(ExpandableListView parent, View v,
        int groupPosition, int childPosition, long id) {

    Toast.makeText(ExpandListActivity.this, "Clicked On Child",
            Toast.LENGTH_SHORT).show();

    // Starting new intent
    Intent in = new Intent(getApplicationContext(),
            AndroidBuildingMusicPlayerActivity.class);

    // Sending childPosition to PlayerActivity
    in.putExtra("childPosition", childPosition);

    Log.println(0, getClass().getName(), "onChildClick inside");

    setResult(100, in);

    startActivity(in);

    // Closing PlayListView
    finish();


    return true;
}
}
4

3 回答 3

1

解决了我的问题

我只是在适配器类的 onClick 方法中添加了它,它就可以工作了。谢谢你们。

Intent intent= new Intent(activity, AndroidBuildingMusicPlayerActivity.class);   
intent.putExtra("childPosition",childPosition); 
activity.setResult(100, intent);  
activity.finish();
于 2012-12-28T02:17:09.487 回答
0

如果您使用 调用此活动startActivityForResult(),则返回调用活动的正确方法如下:

Intent returnIntent = new Intent();
returnIntent.putExtra("childPosition", childPosition);
setResult(RESULT_OK,returnIntent);        
finish();

所以你需要删除这些行:

Intent in = new Intent(getApplicationContext(),
            AndroidBuildingMusicPlayerActivity.class);
startActivity(in);

希望能帮助到你。

于 2012-12-26T05:13:24.493 回答
0

这是适配器类。我尝试在 onClick 中添加一个意图,但它不允许我“startActivity();” 因为我猜我是在适配器类而不是活动类中执行此操作。我不确定。

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

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

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

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.playlist_item, null);
    }
    text = (TextView) convertView.findViewById(R.id.songTitle);
    text.setText(tempChild.get(childPosition));
    convertView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity, tempChild.get(childPosition),
                    Toast.LENGTH_SHORT).show();




        }
    });
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return ((ArrayList<String>) Childtem.get(groupPosition)).size();
}

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

@Override
public int getGroupCount() {
    return groupItem.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 View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = minflater.inflate(R.layout.grouprow, null);
    }
    ((CheckedTextView) convertView).setText(groupItem.get(groupPosition));
    ((CheckedTextView) convertView).setChecked(isExpanded);
    return convertView;
}

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

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

}
于 2012-12-27T01:55:00.393 回答