我试图为 ArrayList 为 android 应用程序设计一个自定义数组适配器。
我需要的是要显示的项目列表,其中每个项目由一个复选框和一个描述性文本组成。我想我需要实现 baseAdapter 的扩展来做到这一点。我无法让它工作。请指出我的错误。
以下是我的主要活动屏幕,仅由一个按钮和一个 listView 组成,两者都已打包在一个 RelativeLayout 中。
<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:background="@drawable/background"
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=".HomeActivity" >
<Button
android:id="@+id/addTaskButton"
android:textSize="30sp"
style="@style/bottomButton"
android:text="@string/addTaskButtonText"/>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/addTaskButton"
android:id="@+id/list">
</ListView>
</RelativeLayout>
以下是上述屏幕的 listView 中单个项目的布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/singleItemContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
<CheckBox
android:id="@+id/taskDoneCheckBox"
android:layout_alignParentLeft="true">
</CheckBox>
<TextView
android:id="@+id/taskDescriptionTextView"
android:layout_toRightOf="@+id/taskDoneCheckBox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"/>
</RelativeLayout>
以下是我创建的自定义适配器。
public class MyListAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public MyListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_item, null);
CheckBox taskDoneCheckBox = (CheckBox) vi.findViewById(R.id.taskDoneCheckBox);
TextView taskDescriptionTextView = (TextView)vi.findViewById(R.id.taskDescriptionTextView);
HashMap<String, String> task = new HashMap<String, String>();
task = data.get(position);
// Setting all values in listview
if(task.get(HomeActivity.IS_TASK_DONE) == "false")
taskDoneCheckBox.setChecked(false);
else
taskDoneCheckBox.setChecked(true);
taskDescriptionTextView.setText(task.get(HomeActivity.TASK_DESCRIPTION));
return vi;
}
}
最后是 mainActivity 类。我已经标记了它的存在使应用程序崩溃的行。
public class HomeActivity extends Activity {
public static final String IS_TASK_DONE = "isTaskDone";
public static final String TASK_DESCRIPTION = "taskDescription";
MyListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//setting up font for the "add task" button.
Typeface tf = Typeface.createFromAsset(getAssets(),"Fonts/doridrobot.ttf");
Button addTaskButton = (Button) findViewById(R.id.addTaskButton);
addTaskButton.setTypeface(tf);
// storing string resources into HashMap
ArrayList<HashMap<String,String>> taskList = new ArrayList<HashMap<String,String>>();
HashMap<String,String> task = new HashMap<String,String>();
task.put(IS_TASK_DONE, "false");
task.put(TASK_DESCRIPTION, "Let this be task 1");
taskList.add(task);
HashMap<String,String> task1 = new HashMap<String,String>();
task1.put(IS_TASK_DONE, "true");
task1.put(TASK_DESCRIPTION, "Let this be task 2");
taskList.add(task1);
ListView itemList = (ListView) findViewById(R.id.list);
adapter = new MyListAdapter(this,taskList);
itemList.setAdapter(adapter); //<----- THIS IS THE ONE
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
}
删除 OnCreate 函数中的最后一行“有助于”应用程序不会崩溃,但列表仍然为空......我该如何解决这个问题?