Issue creating a custom ListView in Android
I was trying to design a custom array adapter for the ArrayList for a
android application.
What I require is a list of items to be displayed, where in each item
consists of a checkbox and a descriptive text. I figured that i would need
to implement a extention of baseAdapter to do this. I am not able to get
it to work. Please point out my mistake.
The following is my main activity screen, simply consists of a button and
a listView, both of with have been packed in a 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>
The following is the layout for a single item in a listView for the above
screen.
<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>
The following is the custom adapter I have created.
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;
}
}
And lastly the following is the mainActivity class. I have marked the line
whose presence crashes the application.
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;
}
}
Removing the last line in the OnCreate function "helps" the app to not
crash, but then the list remains empty... how can i solve this issue?
No comments:
Post a Comment