如何延长ListActivity类和片段类我Mainclass? [英] How to extend ListActivity class and Fragment class with my Mainclass?

查看:204
本文介绍了如何延长ListActivity类和片段类我Mainclass?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid和在急需帮助。
我已经成功地创建了images.My任务一个ListView是当我点击它的描述应该出现在片中的ListView的右侧任意列表项。毫无疑问,这可与片段的帮助来实现。而是利用碎片主类应该扩展片段类,但我的问题是,我的类已经扩展ListActivity class.what我现在应该怎么办?

我有三个Java文件在我的项目:结果
        MainActivity.java [这应该是第一个片段],结果
        CustomArrayAdapter.java和结果
        Fragment2.java [这应该是第二个片段]

下面S中的code

MainActivity.java

 包com.example.mypractice;
进口android.app.ListActivity;
进口android.os.Bundle;
公共类MainActivity扩展ListActivity {
的String []菜单= {
互联网,
电影,
诗经,
游戏,
新闻,
菜单,
法案,
呼叫服务员
 };
整数[] = imageIDs {
R.drawable.pancake,
};
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
CustomArrayAdapter适配器=新
CustomArrayAdapter(这一点,菜单,imageIDs);
setListAdapter(适配器);
}
}

CustomArrayAdapter.java

 包com.example.mypractice;
进口android.app.Activity;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.ImageView;
进口android.widget.TextView;
公共类CustomArrayAdapter扩展ArrayAdapter<串GT; {
私人最终活动范围内;
私人最终的String []菜单;
私人最终整数[] imageIds;
公共CustomArrayAdapter(活动的背景下,
的String [] presidents,整数[] imageIds){
超(背景下,R.layout.lvrowlayout2,presidents);
this.context =背景;
this.menu = presidents;
this.imageIds = imageIds;
}
@覆盖
公共查看getView(INT位置,查看视图的ViewGroup父){
Log.d(CustomArrayAdapter,将String.valueOf(位置));
LayoutInflater吹气= context.getLayoutInflater();
查看rowView = inflater.inflate(R.layout.lvrowlayout2,空,真);
TextView的txtTitle =(TextView中)rowView.findViewById(R.id.txt presidentName);
ImageView的ImageView的=(ImageView的)rowView.findViewById(R.id.icon);
txtTitle.setText(菜单中的[位置]);
imageView.setImageResource(imageIds [位置]);
返回rowView;
}
}

Fragment2.java

 包com.example.mypractice;
进口android.app.ListActivity;
 进口android.os.Bundle;
公共类Fragment2扩展ListActivity {
的String []菜单= {
黄油和蜂蜜煎饼
黄油和蜂蜜煎饼
};
整数[] = imageIDs {
R.drawable.pancake,
R.drawable.pancake,
};
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.fragment2);this.setListAdapter(新ArrayAdapter<串GT;(
这个,
R.layout.lvrowlayout,
R.id.txt presidentName,
presidents));
CustomArrayAdapter适配器=新
CustomArrayAdapter(这一点,菜单,imageIDs);
setListAdapter(适配器);
}
}


解决方案

您基本上想知道如何的建立动态UI与片段或如何设计

您ListActivity应该成为一个FragmentActivity内ListFragment加上所有逻辑的两种布局之间切换。

I am new to android and in desperate need of help. I have successfully created a ListView with images.My task is when I click on any list item its description should appear on the right hand side of the ListView in tablet. No doubt this can be accomplished with the help of fragments. But to use fragments the Main class should extend Fragment class , But my Problem is that my class is already extending ListActivity class.what should I do now?

I have three java files in my Project:
MainActivity.java [This should be the first fragment],
CustomArrayAdapter.java and
Fragment2.java [This should be the second fragment]

Here s the code

MainActivity.java

package com.example.mypractice;
import android.app.ListActivity;
import android.os.Bundle; 
public class MainActivity extends ListActivity {
String[] menu = {
"Internet",
"Movies",
"Songs",
"Games",
"News",
"Menu",
"Bill",
"Call Waiter"
 };
Integer[] imageIDs = {
R.drawable.pancake,
};
@Override
public void onCreate(Bundle savedInstanceState) {    
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomArrayAdapter adapter = new
CustomArrayAdapter(this, menu, imageIDs);
setListAdapter(adapter);
}
}

CustomArrayAdapter.java

package com.example.mypractice;
import android.app.Activity;    
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] menu;
private final Integer[] imageIds;
public CustomArrayAdapter(Activity context,
String[] presidents, Integer[] imageIds) {
super(context, R.layout.lvrowlayout2, presidents);
this.context = context;
this.menu = presidents;
this.imageIds = imageIds;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
Log.d("CustomArrayAdapter",String.valueOf(position));
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.lvrowlayout2, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtPresidentName);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
txtTitle.setText(menu[position]);
imageView.setImageResource(imageIds[position]);
return rowView;
}
}

Fragment2.java

package com.example.mypractice;
import android.app.ListActivity;
 import android.os.Bundle;
public class Fragment2 extends ListActivity {
String[] menu = {
"PanCakes with Butter and Honey",
"PanCakes with Butter and Honey",
};
Integer[] imageIDs = {
R.drawable.pancake,
R.drawable.pancake,
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment2);

this.setListAdapter(new ArrayAdapter<String>(
this,
R.layout.lvrowlayout,
R.id.txtPresidentName,
presidents));
CustomArrayAdapter adapter = new
CustomArrayAdapter(this, menu, imageIDs);
setListAdapter(adapter);
}
}

解决方案

You are basically wondering how to Build a Dynamic UI with Fragments or how to Design for Multiple Screens.

Your ListActivity should become a ListFragment inside a FragmentActivity plus all the logic to switch between the two kind of layouts.

这篇关于如何延长ListActivity类和片段类我Mainclass?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆