我创建了以下适配器:
package org.virtualbet.gui.util;
import java.util.List;
import org.virtualbet.R;
import org.virtualbet.model.Tip;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class TipListAdapter extends ArrayAdapter<Tip> {
private final Context context;
private List<Tip> values;
public TipListAdapter(Context context, int textViewResourceId, List<Tip> objects) {
super(context, textViewResourceId, objects);
//super(context, R.layout.rowlayout, values);
this.context = context;
this.values = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row_tip, null);
}
Tip tip = values.get(position);
if (tip!=null) {
((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipNumber());
((TextView)v.findViewById(R.id.textviewtiprepresentation)).setText((CharSequence)tip.getTipRepresentation());
((TextView)v.findViewById(R.id.textviewtipresult)).setText((CharSequence)tip.getDetails());
((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipOptions());
return v;
}
return null;
} catch (Throwable t) {
Log.e("A", t.toString());
return null;
}
}
}
ListView xml如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="TextView"
android:id="@+id/textviewtipid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</TextView>
<TextView android:text="TextView"
android:id="@+id/textviewtiprepresentation"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text="TextView"
android:id="@+id/textviewtipresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView android:text="TextView"
android:id="@+id/textviewtipplayed"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
getView方法被调用...所有textviews都会改变它们的值...并且在最后一次更改后(无论是哪一个)程序进入catch部分???
为什么呢?有没有人有这样的问题?我读过某个项目(日食)问题....
哦,是的,我正在改变模特的地方是:
package org.virtualbet.gui;
import java.util.ArrayList;
import java.util.List;
import org.virtualbet.R;
import org.virtualbet.bll.TipBLL;
import org.virtualbet.gui.util.TipListAdapter;
import org.virtualbet.model.Ticket;
import org.virtualbet.model.Tip;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class FollowTicket extends Activity {
private static final String TAG = "FollowTicket";
private TipBLL bll;
private TipListAdapter tipListAdapter;
private ListView lv;
private List<Tip> tips;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.follow_ticket);
bll = new TipBLL();
tips = new ArrayList<Tip>();
tipListAdapter = new TipListAdapter(this.getApplicationContext(), R.layout.row_tip, tips);
lv = ((ListView)findViewById(R.id.listViewTips));
lv.setAdapter(tipListAdapter);
((Button)findViewById(R.id.buttonSearchForTicket)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
searchTicket(((EditText)findViewById(R.id.editTextSerialNumber)).getText().toString());
}
});
}
private void searchTicket (String serialNumber) {
Log.i(TAG, "Searching ticket " + serialNumber);
Ticket ticket = bll.getTicket(serialNumber);
if (ticket != null){
Log.i(TAG, "Found it!");
tips.clear();
for (Tip tip : ticket.getTips()) {
Log.i(TAG, "Adding tip " + tip.getTipNumber());
tips.add(tip);
}
tipListAdapter.notifyDataSetChanged();
} else {
Log.i(TAG, "Didn't find it!");
}
}
}
答案 0 :(得分:1)
固定它!真是个愚蠢的错误。在我的布局中,我拿着列表填充我有
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" <-------------ERROR!!!!!!
android:background="#880000"
android:orientation="horizontal" >
.............一些元素
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
哪里错误!!!指向它应该是wrap_content,因为这个布局将ListView推出范围,它不知道有ListView(在运行时,编译编译器不会抱怨)......真的很蠢!!! / p> 无论如何,谢谢你的帮助......
答案 1 :(得分:0)
您可能希望在代码中再次查看此部分:
if (tip!=null) {
((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipNumber());
((TextView)v.findViewById(R.id.textviewtiprepresentation)).setText((CharSequence)tip.getTipRepresentation());
((TextView)v.findViewById(R.id.textviewtipresult)).setText((CharSequence)tip.getDetails());
((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipOptions());
return v;
}
我认为你正在尝试两次R.id.textviewtipid。
if (tip!=null) {
((TextView)v.findViewById(R.id.textviewtipid)).setText((CharSequence)tip.getTipNumber());
((TextView)v.findViewById(R.id.textviewtiprepresentation)).setText((CharSequence)tip.getTipRepresentation());
((TextView)v.findViewById(R.id.textviewtipresult)).setText((CharSequence)tip.getDetails());
((TextView)v.findViewById(R.id.textviewipplayed)).setText((CharSequence)tip.getTipOptions());
return v;
}
这可能是原因