我正在使用Android Studio编写Android应用程序,该应用程序使用XMLPullParser从文件中读取xml文件。之后我想在我的ListView中有一个替代行颜色,我看到this topic并将@Override getView方法添加到我的适配器,但它对我不起作用,我的问题是什么?
MainActivity.java
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
List<App> apps = null;
try {
XMLPullParserHandler parser = new XMLPullParserHandler(getApplicationContext());
InputStream is=getAssets().open("apps.xml");
apps = parser.parse(is);
ArrayAdapter<App> adapter =new ArrayAdapter<App>
(this,android.R.layout.simple_list_item_1, apps);
listView.setAdapter(adapter);
} catch (IOException e) {e.printStackTrace();}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
App.java
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
/**
* Created by ali on 07/29/2016.
*/
public class App extends ArrayAdapter{
private int id,location;
private String title,activity,address;
public App(Context context, int resource) {
super(context, resource);
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getActivity() {
return activity;
}
public void setActivity(String activity) {
this.activity = activity;
}
public int getLocation(){
return location;
}
public void setLocation(int location)
{
this.location=location;
}
public String getAddress(){
return address;
}
public void setAddress(String address)
{
this.address=address;
}
@Override
public String toString() {
return id + "\n " + title + "\n" +activtiy+"\n"+location+"\n"+address;
}
@Override
public View getView(int position,View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
if (position % 2 == 1) {
view.setBackgroundColor(Color.BLUE);
} else {
view.setBackgroundColor(Color.CYAN);
}
return view;
}
}
XMLPullParserHandler.java
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
public class XMLPullParserHandler{
Context context;
public XMLPullParserHandler(Context context) {
this.context = context;
}
private List<App> apps= new ArrayList<App>();
private App app;
private String text;
public List<App> getApps() {
return apps;
}
public List<App> parse(InputStream is) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, null);
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
String tagname = parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (tagname.equalsIgnoreCase("app")) {
app = new App(context,android.R.layout.simple_list_item_1);
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("app")) {
apps.add(app);
}else if (tagname.equalsIgnoreCase("id")) {
app.setId(Integer.parseInt(text));
} else if (tagname.equalsIgnoreCase("title")) {
app.setTitle(text);
} else if (tagname.equalsIgnoreCase("activity")) {
app.setActivity(text);
} else if (tagname.equalsIgnoreCase("location")){
app.setLocation(Integer.parseInt(text));
} else if (tagname.equalsIgnoreCase("address")){
app.setAddress(text);
}
break;
default:
break;
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
return apps;
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#d8d8d8"
android:dividerHeight="1dp">
</ListView>
答案 0 :(得分:2)
在这里您需要创建一个CustomAdapter:
public class CustomAdapter extends ArrayAdapter<App> {
public CustomAdapter (Context context) {
super(context);
}
public View getView(int position, View view, ViewGroup root) {
CustomHolder holder;
if(view == null) {
view = LayoutInflater.from(this.getContext()).inflate(R.layout.simple_list_item_1, root, false);
holder = new CustomHolder();
view.setBackgroundColor(position % 2 == ? Color 1 : Color 2);
view.setTag(holder);
} else {
holder = (CustomHolder)view.getTag();
}
holder.setApp((App)this.getItem(position));
return view;
}
public static class CustomHolder{
public void setApp(App app){//DO YOUR THING}
public CustomHolder () {
}
}
}
然后你的活动应该是这样的:
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.Activity;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
List<App> apps = null;
try {
XMLPullParserHandler parser = new XMLPullParserHandler(getApplicationContext());
InputStream is=getAssets().open("apps.xml");
apps = parser.parse(is);
CustomAdapter<App> adapter = new CustomAdapter (this);
listView.setAdapter(adapter);
} catch (IOException e) {e.printStackTrace();}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}