안드로이드 개발 질문/답변
(글 수 45,052)
안녕하세요.
ListActivity에서 커스텀 레이아웃을 사용하려 시도해 보고 있습니다.
일단 기본 안드로이드 레이아웃으로는 잘 돌아가는 것을 확인했고, 이 상태에서 각 리스트 아이템마다 보여줄 정보가 더 있어서 커스텀 레이아웃을 사용했습니다.
데이터베이스에서 Cursor로 데이터를 가져오고 이것을 ArrayList<MyClass> 에 추가하였습니다.
db = (new Database(this)).getWritableDatabase();
cs = db.query(Database.DB_ACCOUNTS, columns, null, null, null, null, null);
/*if(cs.getCount() ==0) {
Intent i = new Intent(this, AddAccount.class);
startActivityForResult(i,ADDACCOUNT);
}*/
/* make Account List from Cursor */
mAccountList = new ArrayList<Account>(cs.getCount());
refreshAccount(cs,mAccountList);
mAdapter = new AccountAdapter(this,R.layout.list_item_account,mAccountList);
setListAdapter(this.mAdapter);
refreshAccount 함수는 cs에서 데이터를 받아서 mAccountList로 넣어주는 함수입니다.
그리고 AccountAdapter는 이 ListActivity에서 private class로 추가했습니다.
private class AccountAdapter extends ArrayAdapter<Account> {
private ArrayList<Account> accountList = null;
private Context context;
public AccountAdapter(Context context, int textViewResourceId, ArrayList<Account> list) {
//super(context, R.layout.list_item_account, list);
super(context, textViewResourceId, list);
this.context = context;
this.accountList = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.list_item_account,null);
}
if (accountList == null)
return row;
Account entry = accountList.get(position);
if (entry == null ) {
Log.d("Podkill", "Account entry is null!");
}
else {
TextView userId = (TextView)row.findViewById(R.id.account_list_head);
TextView apiKey = (TextView)row.findViewById(R.id.account_list_body);
TextView isFull = (TextView)row.findViewById(R.id.account_list_info);
if(userId != null)
userId.setText(entry.userId);
if(apiKey != null)
apiKey.setText(entry.apiKey);
if(isFull != null)
isFull.setText((entry.isFull==1)?"Full API" : "Limited API");
}
return row;
}
}
Account 클래스는 기본적으로 userId, apiKey, isFull 변수만 가지고 있습니다.(생성자 포함)
그리고 list_item_account는 아래와 같이 만들었습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView android:id="@+id/account_list_head"
style="list_head"
/>
<TextView android:id="@+id/account_list_info"
style="list_info"
/>
<TextView android:id="@+id/account_list_body"
style="list_body"
/>
</LinearLayout>
제딴에는 예제랑 다 찾아보고 했는데, 위와같이 선언하였음에도 불구하고 ListActivity 의 onCreate 함수가 호출되고 위의 코드가 수행 된 후, onCreate를 빠져나가자마자 바로 에러를 내뱉고 앱이 죽어버립니다.
이것을 해결할 방법이 있을까요? 예제랑 동일하게 작성했다고 생각했는데.. 난감하네요. ㅜ.ㅜ



