Java 如何在文本视图中显示数据?
大家好,我正在做我的Android项目,我创建了我的可扩展列表视图,其中包括男性和女性的运动项目,以及每个项目下的运动项目列表。接下来我要做的是以文本视图格式显示每项运动中的数据。我使用intent获取每项运动的新活动,并使用putExtra显示我的事件数据。我的代码有问题,不想显示任何数据。我是android世界的新手,如果有人能帮我,我会非常感激!以下是我的Main.java代码:Java 如何在文本视图中显示数据?,java,android,android-intent,android-activity,Java,Android,Android Intent,Android Activity,大家好,我正在做我的Android项目,我创建了我的可扩展列表视图,其中包括男性和女性的运动项目,以及每个项目下的运动项目列表。接下来我要做的是以文本视图格式显示每项运动中的数据。我使用intent获取每项运动的新活动,并使用putExtra显示我的事件数据。我的代码有问题,不想显示任何数据。我是android世界的新手,如果有人能帮我,我会非常感激!以下是我的Main.java代码: public class MainActivity<View> extends ActionBar
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);
}
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;
}
}
你想通过以下方式实现什么<代码>主要活动?另外,通过适配器将数据绑定到
ExpandableListView
中的子列表项和列表标题项,但您没有显示任何适配器代码。我犯了一个错误,将其放在了不必要的位置,同时更新了我的问题,并且有适配器代码。那么会发生什么?您的ExpandableListView
是否实际显示了正确的组和子组?您的代码在何处/何时失败?我的代码不希望在textview中显示数据。示例:如果我单击“男子运动”,则棒球将不会显示任何内容。我必须在文本视图中获取棒球赛事。我认为我的意图没有正常发挥作用,我在棒球活动中遗漏了一些代码。我看不到你在任何地方初始化gfList
,这表明如果(!gfList.isEmpty())会抛出NullPointerException
,行。
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;
}
}