Android 如何通过XML设置TextView
当我运行应用程序时,列表上显示错误。setAdapterNewAdapter; 我怎样才能解决它? XML代码如下所示:Android 如何通过XML设置TextView,android,textview,Android,Textview,当我运行应用程序时,列表上显示错误。setAdapterNewAdapter; 我怎样才能解决它? XML代码如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertica
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/NewsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray"
android:gravity="center_horizontal"
android:textSize="@dimen/topicsize" />
</LinearLayout>
public class Fragment_NewsContent extends Fragment {
ArrayAdapter<String> newsAdapter;
ArrayList<String> newsTitle = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_newscontent, container, false);
TextView list = (TextView) root.findViewById(R.id.NewsTitle);
newsAdapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, newsTitle);
list.setAdapter(newsAdapter);
showNewsTask task=new showNewsTask();
task.execute();
return root;
}
class showNewsTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("http://garyhui86.er-webs.com/newxContent.xml");
HttpURLConnection urlConn =
(HttpURLConnection)url.openConnection();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
DocumentBuilder builder = DocumentBuilderFactory
.newInstance().newDocumentBuilder();
Document document = builder.parse(urlConn.getInputStream());
NodeList nodeList = document.getElementsByTagName("Info");
for (int i = 0; i < nodeList.getLength(); i++) {
NamedNodeMap attributes=nodeList.item(i).getAttributes();
String category=attributes.getNamedItem("NewsTitle").getNodeValue();
Log.i("ttt", category);
newsTitle.add(category);
}
}
urlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
newsAdapter.notifyDataSetChanged();
}
}
}
爪哇语如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/NewsTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray"
android:gravity="center_horizontal"
android:textSize="@dimen/topicsize" />
</LinearLayout>
public class Fragment_NewsContent extends Fragment {
ArrayAdapter<String> newsAdapter;
ArrayList<String> newsTitle = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_newscontent, container, false);
TextView list = (TextView) root.findViewById(R.id.NewsTitle);
newsAdapter=new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, newsTitle);
list.setAdapter(newsAdapter);
showNewsTask task=new showNewsTask();
task.execute();
return root;
}
class showNewsTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("http://garyhui86.er-webs.com/newxContent.xml");
HttpURLConnection urlConn =
(HttpURLConnection)url.openConnection();
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
DocumentBuilder builder = DocumentBuilderFactory
.newInstance().newDocumentBuilder();
Document document = builder.parse(urlConn.getInputStream());
NodeList nodeList = document.getElementsByTagName("Info");
for (int i = 0; i < nodeList.getLength(); i++) {
NamedNodeMap attributes=nodeList.item(i).getAttributes();
String category=attributes.getNamedItem("NewsTitle").getNodeValue();
Log.i("ttt", category);
newsTitle.add(category);
}
}
urlConn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
newsAdapter.notifyDataSetChanged();
}
}
}
不能将TextView强制转换为ListView,因为它们是完全不同的动物。
请参阅此示例。如果我将TextView更改为ListView,则会出现如下错误04-27 19:34:15.278:E/AndroidRuntime13131:java.lang.ClassCastException:android.widget.TextView无法转换为android.widget.ListView