我想读取一个xml文件,并在ListView中显示项目,,,现在我可以做到这一点,之后我想在ListView中替换行颜色,我在this topic找到解决方案, ,但我的getView方法有问题,,,我的问题是什么?
public class MainActivity extends Activity {
ArrayAdapter<App> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText EDSearch= (EditText) findViewById(R.id.EDSearch);
ListView listView = (ListView) findViewById(R.id.listView1);
List<App> apps = null;
try {
XMLPullParserHandler parser = new XMLPullParserHandler();
InputStream is=getAssets().open("main.xml");
apps = parser.parse(is);
adapter =new ArrayAdapter<App>
(this,android.R.layout.simple_list_item_1, apps);
listView.setAdapter(adapter);
}
catch (IOException e) {e.printStackTrace();}
//Text Changed for search in ListView
listView.setTextFilterEnabled(true);
EDSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
MainActivity.this.adapter.getFilter().filter(s);
}
});
}
// fill the listview by xml file
public class App {
private int id,location;
private String title,activtiy,address;
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 getActivtiy() {
return activtiy;
}
public void setActivtiy(String activtiy) {
this.activtiy = activtiy;
}
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;
}
//这是我的问题,,,无法解决getView
@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;
}
}
public class XMLPullParserHandler {
private List<App> root= new ArrayList<App>();
private App app;
private String text;
public List<App> getApps() {
return root;
}
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("row")) {
app = new App();
}
break;
case XmlPullParser.TEXT:
text = parser.getText();
break;
case XmlPullParser.END_TAG:
if (tagname.equalsIgnoreCase("row")) {
root.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.setActivtiy(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 root;
}
}
这是我的布局文件
<?xml version="1.0" encoding="utf-8"?><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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asaravani.sqlserver.MainActivity">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="500px"
android:divider="#d8d8d8"
android:dividerHeight="1dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_below="@+id/EDSearch">
</ListView>
<EditText
android:id="@+id/EDSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:drawableRight="@android:drawable/ic_menu_search"
android:ems="10"
android:hint="کلمه مورد نظر را وارد کنید" >
<requestFocus />
</EditText>
答案 0 :(得分:0)
您需要编写CustomAdapter才能执行此操作。 getView
方法属于适配器而不属于Activity。您正在尝试在活动中编写getView
方法,这是导致错误的原因。
This是您创建自定义适配器的方式。
答案 1 :(得分:0)
您需要为此实现自定义适配器。我给你一个例子,我已经为每个列表项更改了背景颜色,它工作正常。请记住对每个列表项组件使用viewHolder。
public class EarnRedeemAdapter extends BaseAdapter {
private Context context;
private List<GroupData<Merchant>> sortMerchants = new ArrayList<>();
private Transformation transformation;
private boolean earnFlag = true;
public EarnRedeemAdapter(Context context, List<GroupData<Merchant>> list) {
this.context = context;
this.sortMerchants = list;
UtuAppRunningStorage.wrapperMerchantList.clear();
}
public EarnRedeemAdapter(Context context, List<GroupData<Merchant>> list, boolean earnFlag) {
this.context = context;
this.sortMerchants = list;
this.earnFlag = earnFlag;
UtuAppRunningStorage.wrapperMerchantList.clear();
}
@Override
public int getCount() {
return sortMerchants.size();
}
@Override
public Object getItem(int position) {
return sortMerchants.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_earn_redeem, parent, false);
viewHolder.iv_item_earn_redeem_merchant_icon = (ImageView) convertView.findViewById(R.id.iv_item_earn_redeem_merchant_icon);
viewHolder.tv_item_earn_redeem_merchant_points = (TextView) convertView.findViewById(R.id.tv_item_earn_redeem_merchant_points);
viewHolder.tv_item_earn_redeem_merchant_name = (TextView) convertView.findViewById(R.id.tv_item_earn_redeem_merchant_name);
viewHolder.tv_item_earn_redeem_merchant_category = (TextView) convertView.findViewById(R.id.tv_item_earn_redeem_merchant_category);
viewHolder.tv_item_earn_redeem_merchant_distance = (TextView) convertView.findViewById(R.id.tv_item_earn_redeem_merchant_distance);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (position % 2 == 1) {
convertView.setBackgroundColor(Color.BLUE);
} else {
convertView.setBackgroundColor(Color.CYAN);
}
if (merchant != null) {
if (!TextUtils.isEmpty(merchant.getImage())) {
Picasso.with(context)
.load(merchant.getImage())
.placeholder(R.drawable.icon_background_white)
//.transform(new CircleTransform())
.transform(new RoundedTransformation(4, 1))
.into(viewHolder.iv_item_earn_redeem_merchant_icon);
}
//System.out.println("---> merchant.getRewardsdesc(): " + merchant.getRewardsdesc());
if (TextUtils.isEmpty(merchant.getRewardslistingdesc())) {
viewHolder.tv_item_earn_redeem_merchant_points.setVisibility(View.GONE);
viewHolder.tv_item_earn_redeem_merchant_points.setBackgroundColor(Color.TRANSPARENT);
} else {
viewHolder.tv_item_earn_redeem_merchant_points.setVisibility(View.VISIBLE);
viewHolder.tv_item_earn_redeem_merchant_points.setText(merchant.getRewardslistingdesc());
}
viewHolder.tv_item_earn_redeem_merchant_name.setText(merchant.getMerchantname());
viewHolder.tv_item_earn_redeem_merchant_category.setText(merchant.getCategorydesc());
return convertView;
}
}
class ViewHolder {
private ImageView iv_item_earn_redeem_merchant_icon;
private TextView tv_item_earn_redeem_merchant_points;
private TextView tv_item_earn_redeem_merchant_name;
private TextView tv_item_earn_redeem_merchant_category;
private TextView tv_item_earn_redeem_merchant_distance;
}
}