我正在尝试使用自定义适配器实现自己的ListView
设计。但每当我运行我的程序时,我都会收到此错误“ArrayAdapter:您必须为TextView提供资源ID”。
这是我的自定义适配器
public class LayoutAdapter extends ArrayAdapter {
List list = new ArrayList();
public LayoutAdapter (Context context, int resource) {
super(context,resource);
}
static class DataHandler {
TextView accountName;
TextView availableBalance;
TextView totalBalance;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return this.list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return this.list.get(position);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
DataHandler handler;
if(convertView == null) {
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.custom_layout,parent,false);
handler = new DataHandler();
handler.accountName = (TextView) row.findViewById(R.id.account_name);
handler.availableBalance = (TextView) row.findViewById(R.id.available_balance);
handler.totalBalance = (TextView) row.findViewById(R.id.total_balance);
row.setTag(handler);
}
else {
handler = (DataHandler)row.getTag();
}
Account account;
account = (Account)this.getItem(position);
handler.accountName.setText(account.returnName());
handler.availableBalance.setText("Available Balance: $"+account.returnAvailableBalance());
handler.totalBalance.setText("Total Balance: $"+account.returnTotalBalance());
return row;
}
}
这是我的custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:padding="8dp">
<TextView
android:text="@string/str_account_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/account_name"
android:height="80dp"
android:textSize="24sp"
android:gravity="center_vertical" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/account_name">
<TextView
android:text="@string/str_available_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/available_balance"
android:textSize="16sp"
android:layout_gravity="right"
android:paddingTop="30dp" />
<TextView
android:text="@string/str_total_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/total_balance"
android:textSize="16sp"
android:layout_gravity="right" />
</LinearLayout>
</RelativeLayout >
这是我的主要活动
LayoutAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new LayoutAdapter(getApplicationContext(),R.layout.custom_layout);
ArrayList<Account> accounts = new ArrayList<>();
accounts.add(new Account ("Checking",5.00));
accounts.add(new Account ("Cash",50.00));
Account acc1 = new Account("Checking",5.00);
Account acc2 = new Account("Cash",50.00);
adapter.add(acc1);
adapter.add(acc2);
listView.setAdapter(adapter);