如何在文本视图中显示数据?

时间:2014-12-09 22:01:32

标签: java android android-intent android-activity

大家好,我正在处理我的Android项目,我创建了男性和女性体育的可扩展列表视图,并在每个体育列表下创建。接下来我要做的是以textview格式显示每项运动中的数据。我曾经意图为每项运动获得我的新活动,并使用toExtra来显示我的数据与事件。我的代码出错了,不想显示任何数据。我是Android世界的新手,如果有人可以帮助我,我真的很感激!这是我的Main.java代码:

public class MainActivity<View> extends ActionBarActivity {

    ExpandableListView exv;
    List<GroupedFeed> gfList;
    GroupedFeed gfResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        exv=(ExpandableListView)findViewById(R.id.expandableListView1);
        exv.setAdapter(new MyAdapter(this));
        exv.setOnChildClickListener(new OnChildClickListener() {
        // Here I wanted to create my for loop but that did not work 

        private GroupedFeed findFeed(String locateSport){ 
                if ((!gfList.isEmpty())){
                   gfResult = null;
                    for (int i = 0; i != gfList.size();i++){
                        if (gfList.get(i).category.equalsIgnoreCase(locateSport)){
                           gfResult =  gfList.get(i);
                        }

                    }
                   return gfResult;
                 }
                return null;
               }

            public boolean onChildClick(ExpandableListView parent,
                    android.view.View v, int groupPosition, int childPosition,
                    long id) {
                switch (groupPosition)
                {
                case 0:
                        switch (childPosition)
                        {
                        case 0:
                            gfResult = findFeed("Men's Baseball");
                            Baseball(gfResult);
                              // pass gfResult
                            break;
                        case 1:
                            gfResult =findFeed("Men's Basketball");
                            MensBasketball(gfResult);
                            break;
                       }
                return false;
            }

            private void Baseball(GroupedFeed gfResult) {
                Intent myIntent = new Intent(MainActivity.this, Baseball.class);
                myIntent.putExtra("Men's Baseball", gfResult);
                startActivity(myIntent);
            }

            private void MensBasketball(GroupedFeed gfResult) {
                Intent myIntent = new Intent(MainActivity.this, MensBasketball.class);
                myIntent.putExtra("Men's Basketball", gfResult);
                startActivity(myIntent);
            }

这里我有我的活动(棒球)代码:

public class Baseball extends ActionBarActivity {



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

    private void setupBackButton(){
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
    }
}

THANKS IN ADVANCE!

MyAdapter代码:

public class MyAdapter extends BaseExpandableListAdapter {
private Context context;
Typeface typeface;

static String []parentList = {"Men's Sports","Women's Sports"};
static String [][]childList = {
    {
        "Baseball", "Basketball", "Bowling", "Cross Country", "Golf", "Soccer", "Track & Field"
    },
    {
        "Basketball", "Bowling", "Cross Country", "Golf", "Soccer", "Softball", "Track & Field", "Volleyball"
    }
};

    public MyAdapter(Context context) {
        // TODO Auto-generated constructor stub
        this.context=context;
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return parentList.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return childList[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Timeline.ttf");
        TextView tv = new TextView(context);
        tv.setText(parentList[groupPosition]);
        tv.setPadding(45, 10, 10, 10);
        tv.setTextSize(15);
        tv.setTextColor(Color.parseColor("#003767"));
        tv.setTypeface(typeface);
        tv.setBackgroundColor(Color.parseColor("#CCCCCC"));
        return tv;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        typeface=Typeface.createFromAsset(context.getAssets(),"fonts/Timeline.ttf");
        TextView tv = new TextView(context);
        tv.setText(childList[groupPosition][childPosition]);
        tv.setPadding(45, 10, 10, 10);
        tv.setTextSize(12);
        tv.setTextColor(Color.parseColor("#FFDD00"));
        tv.setTypeface(typeface);
        tv.setBackgroundColor(Color.parseColor("#003767"));
        return tv;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

0 个答案:

没有答案