Java 将活动类更改为片段
我对Android的新开发非常感兴趣。我有一个扩展活动的类,我想改成一个片段。我知道作为一个片段,它将使用onCreateView(LayoutInflater、ViewGroup、Bundle),但我不知道如何将我的工作活动更改为片段 编辑:我已经开始工作了。现在的问题是RSS提要中的列表没有显示。我得到了我的加载指示器,一旦完成它就会停止,但是列表没有显示 编辑:实际上列表正在显示,但是文本与背景颜色相同,所以我看不到它 以下是我现有的活动:Java 将活动类更改为片段,java,android,android-activity,android-fragments,Java,Android,Android Activity,Android Fragments,我对Android的新开发非常感兴趣。我有一个扩展活动的类,我想改成一个片段。我知道作为一个片段,它将使用onCreateView(LayoutInflater、ViewGroup、Bundle),但我不知道如何将我的工作活动更改为片段 编辑:我已经开始工作了。现在的问题是RSS提要中的列表没有显示。我得到了我的加载指示器,一旦完成它就会停止,但是列表没有显示 编辑:实际上列表正在显示,但是文本与背景颜色相同,所以我看不到它 以下是我现有的活动: import java.io.IOExcepti
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ActionAlerts extends Activity {
ListView listFeed;
ProgressBar prgLoading;
TextView txtAlert;
ActionAlertsAdapter la;
static String[] title;
static String[] pubDate;
static String[] link;
String URLFeed;
URL Feed;
DocumentBuilder db;
Document doc;
NodeList nodeList;
Bundle rssBundle = new Bundle();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actionalerts);
Bundle b = getIntent().getExtras();
URLFeed = b.getString("url");
la = new ActionAlertsAdapter(this);
listFeed = (ListView) findViewById(R.id.listFeed);
prgLoading = (ProgressBar) findViewById(R.id.prgLoading);
txtAlert = (TextView) findViewById(R.id.txtAlert);
// ColorDrawable whiteColor = new ColorDrawable(Color.WHITE);
ColorDrawable blackColor = new ColorDrawable(Color.BLACK);
listFeed.setDivider(blackColor);
listFeed.setDividerHeight(3);
new getDataTask().execute();
listFeed.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
Intent web = new Intent(ActionAlerts.this, WebBrowser.class);
rssBundle.putString("myURL", link[position]);
web.putExtras(rssBundle);
startActivity(web);
}
});
}
/** this class is used to handle thread */
public class getDataTask extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
getDataFromFeed();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
//dialog.dismiss();
prgLoading.setVisibility(8);
if(title != null){
listFeed.setVisibility(0);
listFeed.setAdapter(la);
}else{
txtAlert.setVisibility(0);
}
}
}
/*
* This code is used to get data from feed and store them
* to array attributes
*/
public void getDataFromFeed(){
try {
Feed = new URL(URLFeed);
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
doc = db.parse(new InputSource(Feed.openStream()));
doc.getDocumentElement().normalize();
nodeList = doc.getElementsByTagName("item");
title = new String[nodeList.getLength()];
pubDate = new String[nodeList.getLength()];
link = new String[nodeList.getLength()];
for(int i=0;i<nodeList.getLength();i++){
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList titleList = fstElmnt.getElementsByTagName("title");
Element titleElement = (Element) titleList.item(0);
titleList = titleElement.getChildNodes();
title[i] = ((Node) titleList.item(0)).getNodeValue();
NodeList pubDateList = fstElmnt.getElementsByTagName("pubDate");
Element pubDateElement = (Element) pubDateList.item(0);
pubDateList = pubDateElement.getChildNodes();
pubDate[i] = ((Node) pubDateList.item(0)).getNodeValue();
// chops off the " +0000" on date
pubDate[i] = pubDate[i].substring(0, pubDate[i].length()-14);
NodeList linkList = fstElmnt.getElementsByTagName("link");
Element linkElement = (Element) linkList.item(0);
linkList = linkElement.getChildNodes();
link[i] = ((Node) linkList.item(0)).getNodeValue();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onConfigurationChanged(final Configuration newConfig)
{
// Ignore orientation change to keep activity from restarting
super.onConfigurationChanged(newConfig);
}
}
下面是我试图从上面的活动类生成的片段类:
package kyfb.android.kyfb.com.kyfb;
import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
public class ActionAlertsFragment extends Fragment {
ListView listFeed;
ProgressBar prgLoading;
TextView txtAlert;
ActionAlertsAdapter la;
static String[] title;
static String[] pubDate;
static String[] link;
String URLFeed;
URL Feed;
DocumentBuilder db;
Document doc;
NodeList nodeList;
Bundle rssBundle = new Bundle();
public ActionAlertsFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// View rootView = inflater.inflate(R.layout.fragment_actionalerts, container, false);
View rootView = inflater.inflate(R.layout.fragment_actionalerts, null);
URLFeed = "http://kyfbnewsroom.com/category/notifications/feed";
Context ctx = rootView.getContext();
la = new ActionAlertsAdapter(ctx);
listFeed = (ListView) rootView.findViewById(R.id.listFeed);
prgLoading = (ProgressBar) rootView.findViewById(R.id.prgLoading);
txtAlert = (TextView) rootView.findViewById(R.id.txtAlert);
// ColorDrawable whiteColor = new ColorDrawable(Color.WHITE);
ColorDrawable blackColor = new ColorDrawable(Color.BLACK);
listFeed.setDivider(blackColor);
listFeed.setDividerHeight(3);
new getDataTask().execute();
listFeed.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
// Intent web = new Intent(ActionAlertsFragment.this, WebBrowser.class);
// rssBundle.putString("myURL", link[position]);
// web.putExtras(rssBundle);
// startActivity(web);
}
});
return rootView;
}
/** this class is used to handle thread */
public class getDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
getDataFromFeed();
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
//dialog.dismiss();
prgLoading.setVisibility(8);
if(title != null){
listFeed.setVisibility(0);
listFeed.setAdapter(la);
}else{
txtAlert.setVisibility(0);
}
}
}
/*
* This code is used to get data from feed and store them
* to array attributes
*/
public void getDataFromFeed(){
try {
Feed = new URL(URLFeed);
DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
doc = db.parse(new InputSource(Feed.openStream()));
doc.getDocumentElement().normalize();
nodeList = doc.getElementsByTagName("item");
title = new String[nodeList.getLength()];
pubDate = new String[nodeList.getLength()];
link = new String[nodeList.getLength()];
for(int i=0;i<nodeList.getLength();i++){
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList titleList = fstElmnt.getElementsByTagName("title");
Element titleElement = (Element) titleList.item(0);
titleList = titleElement.getChildNodes();
title[i] = ((Node) titleList.item(0)).getNodeValue();
NodeList pubDateList = fstElmnt.getElementsByTagName("pubDate");
Element pubDateElement = (Element) pubDateList.item(0);
pubDateList = pubDateElement.getChildNodes();
pubDate[i] = ((Node) pubDateList.item(0)).getNodeValue();
// chops off the " +0000" on date
pubDate[i] = pubDate[i].substring(0, pubDate[i].length()-14);
NodeList linkList = fstElmnt.getElementsByTagName("link");
Element linkElement = (Element) linkList.item(0);
linkList = linkElement.getChildNodes();
link[i] = ((Node) linkList.item(0)).getNodeValue();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onConfigurationChanged(final Configuration newConfig)
{
// Ignore orientation change to keep activity from restarting
super.onConfigurationChanged(newConfig);
}
}
包kyfb.android.kyfb.com.kyfb;
导入android.app.Fragment;
导入android.app.ListFragment;
导入android.content.Context;
导入android.content.Intent;
导入android.content.res.Configuration;
导入android.graphics.Color;
导入android.graphics.drawable.ColorDrawable;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ListView;
导入android.widget.ProgressBar;
导入android.widget.TextView;
导入org.w3c.dom.Document;
导入org.w3c.dom.Element;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.xml.sax.InputSource;
导入org.xml.sax.SAXException;
导入java.io.IOException;
导入java.net.MalformedURLException;
导入java.net.URL;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
公共类ActionAlertsFragment扩展了片段{
ListView listFeed;
进行性阅读;
TextView txtAlert;
行动警报:洛杉矶;
静态字符串[]标题;
静态字符串[]pubDate;
静态字符串[]链接;
字符串URLFeed;
URL提要;
文档生成器数据库;
文件文件;
NodeList NodeList;
Bundle rssBundle=新Bundle();
public ActionAlertsFragment(){}
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
super.onCreateView(充气机、容器、保存状态);
//视图根视图=充气机。充气(R.layout.fragment\u actionalerts,container,false);
视图根视图=充气机。充气(R.layout.fragment\u actionalerts,null);
URLFeed=”http://kyfbnewsroom.com/category/notifications/feed";
Context ctx=rootView.getContext();
la=新动作警报捕捉器(ctx);
listFeed=(ListView)rootView.findviewbyd(R.id.listFeed);
prgLoading=(ProgressBar)rootView.findviewbyd(R.id.prgLoading);
txtAlert=(TextView)rootView.findViewById(R.id.txtAlert);
//可着色白色=新的可着色(Color.WHITE);
ColorDrawable blackColor=新的ColorDrawable(Color.BLACK);
setDivider(黑色);
listFeed.setDividerHeight(3);
新建getDataTask().execute();
setOnItemClickListener(新的OnItemClickListener(){
公共单击(适配器视图arg0,视图arg1,内部位置,
长arg3){
//TODO自动生成的方法存根
//Intent web=新的Intent(ActionAlertsFragment.this、WebBrowser.class);
//putsString(“myURL”,link[position]);
//web.putExtras(rssBundle);
//星触觉(网络);
}
});
返回rootView;
}
/**此类用于处理线程*/
公共类getDataTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//TODO自动生成的方法存根
getDataFromFeed();
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
//TODO自动生成的方法存根
//dialog.dismise();
设置可见性(8);
如果(标题!=null){
listFeed.setVisibility(0);
setAdapter(la);
}否则{
txtAlert.setVisibility(0);
}
}
}
/*
*此代码用于从提要获取数据并存储它们
*数组属性
*/
public void getDataFromFeed(){
试一试{
Feed=新URL(URLFeed);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
db=dbf.newDocumentBuilder();
doc=db.parse(新的InputSource(Feed.openStream());
doc.getDocumentElement().normalize();
nodeList=doc.getElementsByTagName(“项目”);
title=新字符串[nodeList.getLength()];
pubDate=新字符串[nodeList.getLength()];
link=新字符串[nodeList.getLength()];
对于(int i=0;i请尝试以下方法:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_submenus_list, null);
....
return view;
}
并将每个findViewById(…)
更改为视图。findViewById(…)
首先更改此项
View rootView = inflater.inflate(R.layout.fragment_actionalerts, null);
到
并使用Fragment Context
作为getActivity()
您需要一个容器(视图组)来附加片段。您可以在onCreateView中膨胀视图并返回相同的视图。使用相同的视图对象初始化视图。对于上下文,请使用getActivity()
。这似乎不起作用。我知道我做错了什么,但我不确定是什么原因,因为我没有太多的Android经验。我已经发布了我正在处理的片段的代码。
View rootView = inflater.inflate(R.layout.fragment_actionalerts, null);
View rootView = inflater.inflate(R.layout.fragment_actionalerts, container, false);