目前我是android的初学者,现在我正在尝试将我的解析xml数据显示到ListView中。但我不知道如何将数据显示到ListView中。谁能协助我解决这个问题?这是我的完整源代码。
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//new ReadRSS(this,"http://www.thestar.com.my/rss/editors-choice/main/").execute();
//new ReadRSS(this,"http://www.sciencemag.org/rss/news_current.xml").execute();
//new ReadRSS(this,"https://www.androidpit.com/feed/main.xml").execute();
new ReadRSS(this,"http://malaysiakini.com/en/news.rss").execute();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
ReadRSS.java
public class ReadRSS extends AsyncTask<Void, Void, Void> {
//Initialize progress dialog
Context context;
String address;
//String[] parts = address.split(";");
ProgressDialog progressDialog;
XmlPullParserFactory xmlPullParserFactory;
volatile boolean parsingComplete = true;
ArrayList<FeedItem> feedItems = new ArrayList<>();
public ReadRSS(Context context, String retrieveAddress) {
//Create a new progress dialog
this.address = retrieveAddress;
this.context = context;
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading....");
}
@Override
protected void onPreExecute() {
//Display progress dialog
progressDialog.show();
super.onPreExecute();
}
// This is run in a background thread
@Override
protected Void doInBackground(Void... voids) {
//ProcessXml(Getdata());
fetchXML();
return null;
}
// This is called from background thread but runs in UI
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(Void aVoid) {
//Dismiss progress dialog
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text=null;
try {
event = myParser.getEventType();
FeedItem item = new FeedItem();
while (event != XmlPullParser.END_DOCUMENT) {
String tagName = myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
if(tagName.equalsIgnoreCase("item")){
int eventChild = myParser.next();
while(eventChild != XmlPullParser.END_DOCUMENT){
String tagNameChild = myParser.getName();
switch (eventChild){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if(tagNameChild.equalsIgnoreCase("title")){
item.setTitle(text);
//Log.d("Title",text);
}
else if(tagNameChild.equalsIgnoreCase("link")){
item.setLink(text);
//Log.d("Link",text);
}
else if(tagNameChild.equalsIgnoreCase("description")){
String plain = Html.fromHtml(text).toString();
//stripHtml(text);
item.setDescription(plain);
// Log.d("Description",plain);
}
else if(tagNameChild.equalsIgnoreCase("pubDate")){
item.setPubDate(text);
//Log.d("pudDate",text);
}
else if(tagNameChild.equalsIgnoreCase("media:thumbnail") || tagNameChild.equalsIgnoreCase("media:content") || tagName.equalsIgnoreCase("enclosure")){
//Output test
if(myParser.getAttributeValue(null,"url") != null) {
item.setThumbnailUrl(text);
//Log.d("ItemThumbnailUrl",myParser.getAttributeValue(null,"url"));
}
}
else if(tagNameChild.equalsIgnoreCase("item")){
feedItems.add(item);
//Log.d("ArrayList ",item.getTitle());
}
break;
}
eventChild = myParser.next();
}
}
break;
case XmlPullParser.TEXT:
break;
case XmlPullParser.END_TAG:
break;
}
event = myParser.next();
}
parsingComplete = false;
}
catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 );
conn.setConnectTimeout(15000 );
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
xmlPullParserFactory = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlPullParserFactory.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
}
catch (Exception e) {
}
}
});
thread.start();
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/title_text"
android:layout_weight="0.09" />
<TextView
android:layout_width="80dp"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/date_text"
android:layout_gravity="right"
android:layout_weight="0.05" />
</LinearLayout>
答案 0 :(得分:1)
然后创建一个自定义列表适配器并将arraylist传递给它。 (教程:http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92)
将解析后的数据存储在asynctask的末尾。