내 프로그램 용 사용자 정의 배열 어댑터를 설정하는 데 문제가 있습니다. 다음은 아래의 샘플 코드의 일부는 다음과 같습니다사용자 정의 ArrayAdapter가있는 Android Listview
는 XML activity_main :
<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=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow android:id="@+id/row1" >
<TextView android:text="@string/name" >
</TextView>
<EditText android:id="@+id/name" >
</EditText>
</TableRow>
<TableRow android:id="@+id/row2" >
<TextView
android:id="@+id/textView2"
android:text="@string/address" />
<EditText android:id="@+id/address" />
</TableRow>
<TableRow android:id="@+id/row3" >
<TextView
android:id="@+id/textView3"
android:text="@string/type" />
<RadioGroup android:id="@+id/group" >
<RadioButton
android:id="@+id/takeout"
android:text="@string/takeout" />
<RadioButton
android:id="@+id/delivery"
android:text="@string/delivery" />
<RadioButton
android:id="@+id/sitdown"
android:text="@string/sitdown" />
</RadioGroup>
</TableRow>
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addr" />
</TableLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
activity_list에 대한 XML :
// adapter to populate the rows of the listview
private class restaurantAdapter extends ArrayAdapter<restaurant>{
public restaurantAdapter(){
super(MainActivity.this, android.R.layout.simple_list_item_1, restList);
}
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
restaurantHolder holder = null;
if(row == null){
LayoutInflater inflater = getLayoutInflater();
// set the row of data to the specified xml formatting
row = inflater.inflate(R.layout.actitivity_list, null);
holder = new restaurantHolder(row);
row.setTag(holder);
}else{
holder = (restaurantHolder)row.getTag();
}
holder.populateFrom(restList.get(position));
return row;
}
}
// creates a holder for the adapter
private class restaurantHolder{
private TextView rowName = null;
private TextView addrName = null;
private ImageView rowIcon = null;
private View row = null;
public restaurantHolder(View row){
this.row = row;
}
public void populateFrom(restaurant r){
getName().setText(r.getName());
getAddress().setText(r.getAddress());
if(r.getMealType().equals("Delivery")){
getIcon().setImageResource(R.drawable.ball_yellow);
}else if(r.getMealType().equals("Sit Down")){
getIcon().setImageResource(R.drawable.ball_red);
}else{
getIcon().setImageResource(R.drawable.ball_green);
}
}
private TextView getName(){
if(rowName == null){
rowName = (TextView)findViewById(R.id.listName);
}
return rowName;
}
private TextView getAddress(){
if(addrName == null){
addrName = (TextView)findViewById(R.id.listAddress);
}
return addrName;
}
private ImageView getIcon(){
if(rowIcon == null){
rowIcon = (ImageView)findViewById(R.id.icon);
}
return rowIcon;
}
}
:
<?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="fill_parent"
android:orientation="horizontal"
android:padding="4px">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="4px"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/listName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:text="TextView" />
<TextView
android:id="@+id/listAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:singleLine="true"
android:ellipsize="end"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
이 사용자 정의 ArrayAdapter와 클래스입니다
arrayAdapter에서 holder.populateFrom medthod가 호출 되 자마자 프로그램이 충돌합니다. 잠시 동안 디버깅을 시도했지만 도움이되지 않는 널 포인터 예외를 제공합니다. 어댑터 홀더 클래스가 activity_list xml 파일에 연결되어 있지 않지만이 시점에서 확신 할 수없는 것 같습니다. 어떤 제안이라도 도움이 될 것입니다.
나는 많은 코드를 알고 있지만, 나는 그저 새로운 눈이 필요하다고 생각한다. 도와 주셔서 감사합니다.
나는 당신의 코드를 보았을 때, 조금 지저분했다. 그리고 당신은 ArrayAdapter를 확장하여 사용자 정의 어댑터를 생성하고 있습니다 만, ** BaseAdapter **를 커스텀 어댑터 용으로 확장하는 것이 좋을 것입니다. 당신이 그때 질문한다면 나는 그것에 대한 약간의 튜토리얼을 제안 할 것이다. –