我正在尝试为Android应用程序设计ArrayList的自定义数组适配器。
我需要的是要显示的项目列表,每个项目的位置包含一个复选框和一个描述性文本。我想我需要实现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函数中的最后一行“有助于”应用程序不会崩溃,但随后列表仍为空...我该如何解决此问题?
答案 0 :(得分:0)
请发布您的logcat输出以了解您到底发生了什么事故,虽然我现在可以指出的错误是,
public Object getItem(int position) {
return position;
}
这里你需要把
public Object getItem(int position) {
return data.get(position);
}
答案 1 :(得分:0)
发布您的日志cat错误可能是b&#39;因为您没有指定任何布局宽度和高度属性 android:layout_width =&#34; wrap_content&#34;机器人:layout_height =&#34; WRAP_CONTENT&#34; 强>
为list_item指定一些宽度和高度:
<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"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</CheckBox>
<TextView
android:id="@+id/taskDescriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/taskDoneCheckBox" />
答案 2 :(得分:0)
我已经检查了这个并且它正在运行:
<强> litsitem.xml 强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<CheckBox
android:id="@+id/taskDoneCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="" />
<TextView
android:id="@+id/taskDescriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBox1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
<强> activity_main.xml中强>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/addTaskButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/addTaskButton" >
</ListView>
</RelativeLayout>
<强> HomeActivity.java 强>
import java.util.ArrayList;
import android.os.*;
import android.widget.*;
import java.util.HashMap;
import android.app.Activity;
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_main);
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);
}
}
<强> MyListAdapter 强>
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
public class MyListAdapter extends BaseAdapter {
private Context activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public MyListAdapter(Context 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.listitem, 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;
}
}