我创建了用于解析xml文件的类,并在listview中显示了它,我的类构造函数在参数中获得了Inputstream,我想将它和inputstream传递给它。
当我想从外部存储中获取文件并将其传递给我的类构造函数时,我不知道该怎么做。我用很多韦,但我做得不正确。
public class MainActivity extends AppCompatActivity {
Button xml;
Button json;
ListView dataShow;
List<book> bookList;
ArrayAdapter<book> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xml = findViewById(R.id.btnXmlShow);
json = findViewById(R.id.btnJsonShow);
dataShow = findViewById(R.id.parsedDataListView);
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/mine", "products_xml");
final FileInputStream fis = null;
try {
fis = new FileInputStream(file);
/*Toast.makeText(this,Environment.getExternalStorageDirectory()
.getAbsolutePath().toString(),Toast.LENGTH_SHORT).show();*/
} catch (FileNotFoundException e) {
e.printStackTrace();
}
xml.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//getResources().openRawResource(R.raw.products_xml)
bookList = new xmlpullparser(fis).parsedXml();
adapter = new ArrayAdapter<book>
(MainActivity.this,android.R.layout.simple_list_item_1,bookList);
dataShow.setAdapter(adapter);
}
});
}
}
/////////////////////////////
public class xmlpullparser {
private InputStream input;
private List<book> bookList;
private book currentBook;
private String currentTag;
public xmlpullparser(InputStream input){ this.input = input; }
public List<book> parsedXml(){
bookList = new ArrayList<>();
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(input,null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
handleStartTag(parser.getName());
}else if (eventType == XmlPullParser.TEXT){
handleText(parser.getText());
}else if (eventType == XmlPullParser.END_TAG){
currentTag = null;
}
eventType = parser.next();
}
input.close();
} catch (XmlPullParserException | IOException e) {
e.printStackTrace();
}
return bookList;
}
private void handleText(String text) {
if(currentBook == null || currentTag == null) return;
switch (currentTag){
case "id":
currentBook.setId(Integer.valueOf(text));
break;
case "author":
currentBook.setAuthor(text);
break;
case "title":
currentBook.setTitle(text);
break;
case "genre":
currentBook.setGenre(text);
break;
case "price":
currentBook.setPrice(Double.valueOf(text));
break;
case "publishDate":
currentBook.setPublishDate(text);
break;
case "description":
currentBook.setDescription(text);
break;
default: break;
}
}
private void handleStartTag(String tagName) {
if (tagName.equals("book")){
currentBook = new book();
bookList.add(currentBook);
}else{
currentTag = tagName;
}
}
}
//////////////////////////////////////
package com.example.sobhan.parserproject;
public class book {
private int id;
private String author;
private String title;
private String genre;
private double price;
private String publishDate;
private String description;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPublishDate() {
return publishDate;
}
public void setPublishDate(String publishDate) {
this.publishDate = publishDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Title: " + title + "\nAuthor: " + author + "\n" + "Price: " + price ;
}
}
//////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/buttonLayer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/btnXmlShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Xml" />
<Button
android:id="@+id/btnJsonShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Json" />
</LinearLayout>
<ListView
android:id="@+id/parsedDataListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/buttonLayer"
android:padding="5dp"
android:layout_weight="1" />
</RelativeLayout>
我如何将fis对象传递给类。 谢谢。