I am populating a ListView with a Base Adapter in such a way that all except the last item will be checkboxes and the last item will be a TextView with a button.
Here are the XML files.
Final Item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_newitem"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="3"
android:text="@string/new_account_text"
/>
<Button
android:id="@+id/b_newitem"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="2"
android:text="@string/add_button_text"
android:onClick="showNewAccountDialog"
/>
</LinearLayout>
Checkboxes:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
>
<CheckBox
android:focusable="true"
android:id="@+id/account_item_cb"
android:layout_height="wrap_content"
android:layout_width="match_parent"
></CheckBox>
</LinearLayout>
Here is the Class file for the base adapter:
import java.util.ArrayList;
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.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class AccountListAdapter extends BaseAdapter{
private static final int TYPE_ACCOUNT = 0;
private static final int TYPE_NEW_ACCOUNT = 1;
private static final int TYPE_MAX_COUNT = 2;
private LayoutInflater mInflator;
private ArrayList<String> mStrings;
private ArrayList<String> mSelectedStrings;
public AccountListAdapter(Context context, ArrayList<String> array)
{
mInflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStrings = array;
mSelectedStrings = new ArrayList<String>();
}
public void addNewAccount(final String accountName)
{
mStrings.add(mStrings.size()-2, accountName);
notifyDataSetChanged();
}
@Override
public int getCount()
{
return mStrings.size();
}
@Override
public String getItem(int position)
{
return mStrings.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public int getViewTypeCount()
{
return TYPE_MAX_COUNT;
}
@Override
public int getItemViewType(int position)
{
return position == mStrings.size()-1 ? TYPE_NEW_ACCOUNT : TYPE_ACCOUNT;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
int type = getItemViewType(position);
System.out.println(position + ": " + type);
switch (type) {
case TYPE_ACCOUNT:
convertView = mInflator.inflate(R.layout.account_item, null);
CheckBox tv = (CheckBox) convertView.findViewById(R.id.account_item_cb);
tv.setText(getItem(position));
tv.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked)
{
mSelectedStrings.add(buttonView.getText().toString());
}else {
mSelectedStrings.remove(buttonView.getText().toString());
}
}
});
break;
case TYPE_NEW_ACCOUNT:
convertView = mInflator.inflate(R.layout.list_new_item_add_button, null);
break;
default:
break;
}
return convertView;
}
public ArrayList<String> getSelectedStrings()
{
return mSelectedStrings;
}
}
There is an Activity calls which Populates this base adapter will an Array list of String. I am trying to show a dialog box to the user when the Add button is clicked. But I am not able to show it. I tried:
Adding android:onClick=method in the XML file and writing corresponding method in the main activity file, but Eclipse cannot find the function. I think it is looking for the function in the base adapter class. But the problem is I can't write code to show a AlertBox in the Base Adapter class because getSupportFragmentManager cannot be accessed from there.
Adding onClickListener to Button using findViewById, but Eclipse gives me NullPointerException here. I think this is because the button is placed in the ListView and not the Activity directly.
Can someone help me here?
Thanks!