2
這裏的錯誤是代碼得到一個錯誤arrayadapter - 你必須提供一個資源ID爲一個TextView定製ArrayAdapter <new_class>讓你必須提供一個資源ID爲一個TextView
public class lay extends Activity
{
public class MyCustomAdapter extends ArrayAdapter<new_class>
{
private ArrayList<new_class> items;
public MyCustomAdapter(Context context, int textViewResourceId, ArrayList<new_class> items)
{
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
new_class o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.text1);
TextView bt = (TextView) v.findViewById(R.id.text2);
if (tt != null) {
tt.setText("Name: "+o.getName()); }
if(bt != null){
bt.setText("Status: "+ o.getLink());
}
}
return v;
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myclass try1 = new myclass();
final List<Map<String, ?>> aaa = try1.type();
//------------------------------------------------------
ArrayList<new_class> my_orders = null;
my_orders = new ArrayList<new_class>();
new_class o1 = new new_class();
o1.setName("perspectiva");
o1.setLink("link1");
new_class o2 = new new_class();
o2.setName("perspectiva22");
o2.setLink("link222");
my_orders.add(o1);
my_orders.add(o2);
ArrayAdapter<new_class> adapter = new ArrayAdapter<new_class>(this, R.layout.row, my_orders);
ListView lv = (ListView)this.findViewById(R.id.ListView01);
lv.setAdapter(adapter);
//------------------------------------------------------
}
和佈局行
<pre>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/text2"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
</LinearLayout>
</pre>
謝謝。但你能解釋我爲什麼應該使用baseadapter而不是arrayadapter?或在哪裏閱讀更多信息? – SERG 2011-04-17 15:33:20
陣列適配器本身擴展了基礎適配器,因爲基礎適配器是適配器的基礎類。 – 2011-04-29 00:11:48