I have a custom listview that has CheckedTextView. When i click the the items it doesn't toggle the checked state of the object and reflect in the ui.
dialog_categories.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dip"
android:layout_marginStart="8dip"
android:background="#color/primary_white"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/title_template"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dip"
android:layout_marginStart="16dip"
android:gravity="center_vertical|start"
android:orientation="horizontal"
android:paddingTop="5dp" >
<TextView
android:id="#+id/textView1"
style="?android:attr/textAppearanceLarge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="10dp"
android:text="#string/dialog_category_title"
android:textColor="#color/primary_color"
android:textSize="22sp" />
<TextView
android:id="#+id/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dialog_category_checkbox"
android:textColor="#color/primary_color" />
<CheckBox
android:id="#+id/checkBoxAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="6dp" />
</LinearLayout>
<View
android:id="#+id/titleDivider"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#color/black" />
<LinearLayout
android:id="#+id/contentPanel"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:minHeight="64dp"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/buttonPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button_category_ok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/dialog_category_btn_ok"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
dialog_list_item_category.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckedTextView
android:id="#+id/categories_checkbox"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:onClick="toggle"
android:text="sss" />
</RelativeLayout>
CategoriesDialogFragment.java
public class CategoriesDialogFragment extends SherlockDialogFragment {
CheckBox checkAll;
ListView categoriesListView;
CategoriesListAdapter adapter;
static Category category;
String[] categories = new String[] { "Hill Station", "Beach", "Historic",
"Wild Life", "Waterfall", "River", "Archeology" };
String[] categories_state = new String[] { "1", "0", "1", "1", "1", "1",
"0" };
public static CategoriesDialogFragment newInstance() {
CategoriesDialogFragment frag = new CategoriesDialogFragment();
Bundle args = new Bundle();
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(MainActivity.context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog_categories);
categoriesListView = (ListView) dialog
.findViewById(R.id.listViewDialog);
List<Category> theCategories = new ArrayList<Category>();
for (int i = 0; i < categories.length; i++) {
Category pl = new Category(categories[i], false);
theCategories.add(pl);
}
categoriesListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
adapter = new CategoriesListAdapter(MainActivity.context,
R.layout.dialog_list_item_category, theCategories);
categoriesListView.setAdapter(adapter);
checkAll = (CheckBox) dialog.findViewById(R.id.checkBoxAll);
checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Toast.makeText(MainActivity.context, "Check",
Toast.LENGTH_SHORT).show();
if (isChecked) {
for (int i = 0; i < adapter.getCount(); i++) {
category = adapter.getItem(i);
category.setChecked(true);
}
adapter.notifyDataSetChanged();
} else {
for (int i = 0; i < adapter.getCount(); i++) {
category = adapter.getItem(i);
category.setChecked(false);
}
adapter.notifyDataSetChanged();
}
}
});
return dialog;
}
private static class CategoriesListAdapter extends ArrayAdapter<Category> {
public Context mContext;
List<Category> mCategories;
public CategoriesListAdapter(Context context, int resource,
List<Category> categories) {
super(context, resource, categories);
// TODO Auto-generated constructor stub
this.mCategories = categories;
this.mContext = context;
}
public int getCount() {
return mCategories.size();
}
#Override
public Category getItem(int position) {
// TODO Auto-generated method stub
return mCategories.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater viewInflater;
viewInflater = LayoutInflater.from(getContext());
convertView = viewInflater.inflate(
R.layout.dialog_list_item_category, null);
holder = new ViewHolder();
holder.categoryName = (CheckedTextView) convertView
.findViewById(R.id.categories_checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.categoryName.setText(mCategories.get(position)
.getCategoryName());
return convertView;
}
static class ViewHolder {
CheckedTextView categoryName;
}
}
}
Could you just use android.R.layout.simple_list_item_multiple_choice instead of dialog_list_item_category.xml?
Update: #Luksprog's comment is the solution: Remove the RelativeLayout and it works just like with android.R.layout.simple_list_item_multiple_choice.
Are You using the proper xml?
There's
dialog_list_item_categories.xml
but in your code You use everywhere
R.layout.dialog_list_item_category
Related
I'm trying to create an AlertDialog that has many TextViews (so that scrolling in necessary) and an EditText at the end for users to enter a value that is not included in the list. When AlertDialog is first presented, things look good. However, when I scroll back to the top after scrolling to the bottom problems occur.
Here is the bottom of the list initially presented. All good.
But here is what it looks like after I scroll to the top. Not so good.
This is the Adapter class that I am using:
public class MultiSelectOtherDialog extends DialogFragment {
public static final String TAG = "MultiSelectOtherDialog";
private ArrayList<Bean> mList = new ArrayList<>();
public static MultiSelectOtherDialog newInstance(String aTitle, String[] aElems) {
MultiSelectOtherDialog frag = new MultiSelectOtherDialog();
Bundle args = new Bundle();
args.putString("title", aTitle);
args.putStringArray("elems", aElems);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// length of this list determines how many items to present - need an extra for the EditText
// at the end for "Other"
int mListLen = (getArguments().getStringArray("elems").length) + 1;
//int mListLen = (getArguments().getStringArray("elems").length);
for (int i = 0; i < mListLen; i++) {
mList.add(new Bean());
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
View view = getActivity().getLayoutInflater().inflate(R.layout.scb_listview2, null);
ListView listViewItems = (ListView)view.findViewById(R.id.lvScb);
listViewItems.setAdapter(new MultiSelectOtherAdapter());
listViewItems.setOnItemClickListener(new OnItemClickListenerListViewItem());
builder.setTitle(getArguments().getString("title")).setView(view);
AlertDialog diagFragDialog = builder.create();
return diagFragDialog;
}
public class MultiSelectOtherAdapter extends BaseAdapter {
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
// needed for the "Other" EditText at the end
if (position < mList.size() - 1) {
convertView = View.inflate(getActivity(), R.layout.scb_item, null);
holder.tv = (TextView) convertView.findViewById(R.id.tv);
Log.d(TAG, "1)");
}
else {
convertView = View.inflate(getActivity(), R.layout.scb_item_other, null);
holder.tv = (TextView) convertView.findViewById(R.id.et);
Log.d(TAG, "2)");
}
holder.cb = (SmoothCheckBox) convertView.findViewById(R.id.scb);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
Log.d(TAG, "3)");
}
final Bean bean = mList.get(position);
holder.cb.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) {
bean.isChecked = isChecked;
}
});
// needed for the "Other" EditText at the end
if (position < getArguments().getStringArray("elems").length) {
String text = getArguments().getStringArray("elems")[position];
holder.tv.setText(text);
Log.d(TAG, "4)");
}
else {
convertView = View.inflate(getActivity(), R.layout.scb_item_other, null);
holder.tv = (TextView) convertView.findViewById(R.id.et);
holder.cb = (SmoothCheckBox) convertView.findViewById(R.id.scb);
convertView.setTag(holder);
Log.d(TAG, "5)");
}
holder.cb.setChecked(bean.isChecked);
Log.d(TAG, "6)");
return convertView;
}
class ViewHolder {
SmoothCheckBox cb;
TextView tv;
}
}
public class OnItemClickListenerListViewItem implements AdapterView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bean bean = (Bean) parent.getAdapter().getItem(position);
bean.isChecked = !bean.isChecked;
SmoothCheckBox checkBox = (SmoothCheckBox) view.findViewById(R.id.scb);
checkBox.setChecked(bean.isChecked, true);
}
}
class Bean implements Serializable {
boolean isChecked;
}
}
And the layouts:
AlertDialog layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/lvScb"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:cacheColorHint="#0000"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:layout_above="#+id/viewLineHoriz" />
<View
android:id="#+id/viewLineHoriz"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="0dp"
android:layout_above="#+id/bottonRowScb"
android:background="?android:attr/dividerVertical" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:id="#+id/bottonRowScb"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:measureWithLargestChild="true"
android:paddingTop="0dip"
android:orientation="horizontal" >
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/buttonOKScb"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/ok_string" />
<View
android:id="#+id/viewLineVert"
android:layout_height="fill_parent"
android:layout_width="1dp"
android:layout_marginBottom="0dp"
android:background="?android:attr/dividerVertical" />
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/buttonCancelScb"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/cancel_string" />
</LinearLayout>
</RelativeLayout>
TextView item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal">
<TextView
android:id="#+id/tv"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="15sp" />
<cn.refactor.library.SmoothCheckBox
android:id="#+id/scb"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|right"
android:layout_weight="1"
android:layout_margin="5dp"/>
</LinearLayout>
And EditText item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal">
<EditText
android:id="#+id/et"
android:hint="#string/other_notes_string"
android:layout_weight="100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="15sp"
android:textStyle="bold|italic"/>
<cn.refactor.library.SmoothCheckBox
android:id="#+id/scb"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical|right"
android:layout_weight="1"
android:layout_margin="5dp"/>
</LinearLayout>
In your adapter change this:
#Override
public long getItemId(int position) {
return position;
}
And, also in your getView() method, put this code:
converView = null;
before :
if(convertView == null){
// your codes
}
Here, in a listview I have added a custom row in which Checkbox and EditText are there and with a add button I just add multiple views to my listview. Adding is working perfectly but when it comes to removing part the checked items are not removing and apart from that suppose I selected two items, then two items from last deleted. I don't know whats going on with my code please help me.
Here is my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
AdapterCustom customAdapter;
ArrayList<String> stringArrayList;
ListView listView;
ArrayList<Integer> listOfItemsToDelete;
AdapterCustom.ViewHolder item;
int POS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfItemsToDelete = new ArrayList<Integer>();
stringArrayList = new ArrayList<String>();
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
listView = (ListView) findViewById(R.id.list_item);
customAdapter = new AdapterCustom(MainActivity.this, R.layout.main, stringArrayList);
}
public void addItems(View v) {
stringArrayList.add("");
listView.setAdapter(customAdapter);
customAdapter.notifyDataSetChanged();
}
public void removeItems(View v) {
if (listOfItemsToDelete.isEmpty()) {
Toast.makeText(getBaseContext(), "No items selected.",
Toast.LENGTH_SHORT).show();
} else {
Log.i("Delete Pos", POS + "");
if (!listOfItemsToDelete.equals("")) {
for (int j = 0; j < listOfItemsToDelete.size(); j++) {
stringArrayList.remove(listOfItemsToDelete.get(j) - j);
customAdapter.notifyDataSetChanged();
}
}
}
}
AdapterCustom.java
public class AdapterCustom extends ArrayAdapter<String> {
Context context;
ArrayList<String> stringArrayList;
ArrayList<Boolean> positionArray;
MainActivity activity;
public AdapterCustom(Context context, int resourceId, ArrayList<String> arrayList) {
super(context, resourceId, arrayList);
this.context = context;
this.stringArrayList = arrayList;
positionArray = new ArrayList<Boolean>(stringArrayList.size());
for (int i = 0; i < stringArrayList.size(); i++) {
positionArray.add(false);
}
activity = new MainActivity();
}
#Override
public int getCount() {
return stringArrayList.size();
}
#Override
public String getItem(int position) {
return stringArrayList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
EditText ediText;
CheckBox checkBox;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
item = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.main, null);
item.ediText = (EditText) convertView.findViewById(R.id.ediText);
item.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
item.checkBox.setTag(new Integer(position));
convertView.setTag(item);
final View finalConvertView1 = convertView;
item.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b) {
int getPosition = (Integer) compoundButton.getTag();
POS = getPosition;
listOfItemsToDelete.add(POS);
Log.i("position after check", POS + "");
Log.i("position check array", listOfItemsToDelete + "");
}
}
});
} else {
item = (ViewHolder) convertView.getTag();
}
item.checkBox.setTag(position);
return convertView;
}
}
activity_main.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="10">
<Button
android:id="#+id/addBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:onClick="addItems"
android:text="Add New Item" />
<Button
android:id="#+id/goBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:onClick="goItems"
android:text="Go to Item" />
</LinearLayout>
<ListView
android:id="#+id/list_item"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:drawSelectorOnTop="false"
android:visibility="visible" />
<Button
android:id="#+id/removeBtn"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:onClick="removeItems"
android:text="Remove Item" />
</LinearLayout>
main.xml
<LinearLayout
android:id="#+id/custom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="10">
<CheckBox
android:id="#+id/checkBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:id="#+id/ediText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="9"
android:background="#android:color/white"
android:hint="Type Anything You Want" />
</LinearLayout>
You only remove the item from the array. Try removing the item also from the list view adapter with adapter.remove(itemPosition);
What does mean
- j
in this code:
for (int j = 0; j < listOfItemsToDelete.size(); j++) {
stringArrayList.remove(listOfItemsToDelete.get(j) - j);
customAdapter.notifyDataSetChanged();
}
?
I think that:
for (String itemToRemove:listOfItemsToDelete) {
stringArrayList.remove(itemToRemove);
}
customAdapter.notifyDataSetChanged();
listOfItemsToDelete.clear();
would be appropriate.
My Custom adapter is as follows:-
public class ListAdapter extends ArrayAdapter<Person> implements SectionIndexer {
private HashMap<String, Integer> alphaIndexer;
private String[] sections;
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, ArrayList<Person> items) {
super(context, resource, items);
alphaIndexer = new HashMap<String, Integer>();
for (int i = 0; i < items.size(); i++)
{
Log.d("State",items.get(i).getState().substring(0, 1).toUpperCase());
String s = items.get(i).getState().substring(0, 1).toUpperCase();
alphaIndexer.put(s, i);
}
Set<String> sectionLetters = alphaIndexer.keySet();
ArrayList<String> sectionList = new ArrayList<String>(sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
public int getPositionForSection(int section)
{
return alphaIndexer.get(sections[section]);
}
public int getSectionForPosition(int position)
{
return 1;
}
public Object[] getSections()
{
return sections;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.bystate_itemview, null);
}
Person p = getItem(position);
if (p != null) {
ImageView im1= (ImageView) v.findViewById(R.id.legislator_image);
TextView tt1 = (TextView) v.findViewById(R.id.firstname);
TextView tt2 = (TextView) v.findViewById(R.id.details);
ImageView im2 = (ImageView) v.findViewById(R.id.getDetails);
if (im1 != null){
String url = p.getImage();
Picasso.with(getContext()).load(url).fit().centerCrop().into(im1);
}
if (tt1 != null) {
String name= p.getLastname()+", "+p.getFirstname();
tt1.setText(name);
}
if (tt2 != null) {
String details="";
if(p.getDistrict()!="NA")
details= "("+p.getParty()+")"+p.getState()+" - "+"District "+p.getDistrict();
else
details= "("+p.getParty()+")"+p.getState()+" - "+"District 0";
tt2.setText(details);
}
if (im2 != null) {
Drawable myDrawable = getResources().getDrawable(R.drawable.right);
im2.setImageDrawable(myDrawable);
}
}
return v;
}
}
I have implemented this custom adapter following a tutorial. I have implemented the section indexer. But the issue is that the section indexer does not show up in the app. I am trying to index stuff based on the first letter of each state of the person. I want the alphabetical list to show up in the list view so that when I tap on a particular letter it must show persons belonging to that state.
My xml files:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/state_listView"
android:layout_weight="1"
android:layout_marginTop="35dp"
android:fastScrollEnabled="true"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_marginTop="40dp"
android:background="#drawable/gradient">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1">
<ImageView
android:layout_width="55dp"
android:layout_height="49dp"
android:layout_margin="5dp"
android:id="#+id/legislator_image"
android:layout_weight="0.19"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="80dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:text="Medium Text"
android:id="#+id/firstname"
android:layout_marginTop="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/details"
android:layout_marginTop="3dp" />
</LinearLayout>
<ImageView
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="3dp"
android:layout_alignParentRight="true"
android:id="#+id/getDetails"/>
</RelativeLayout>
</LinearLayout>
Please let me know if I am missing something. This is my first custom adapter so I may be doing something wrong.
Just use below structure ,Mayi know why youimplement sectionIndex
public class CustomeAdapter extends BaseAdapter {
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
return null;
}
}
I currently have ListFragment with a ArrayAdapter class
The codes are below.
1)I want to implement a checkbox from listview main xml to check all the checkboxes that are present in custom list row xml which is inflated using Array adapter.
2)how to refresh view in this code by clearing all checkboxes when a button is clicked.
listview_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:background="#fff2fff2" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
android:choiceMode="multipleChoice" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
/>
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/btnin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In" />
<Button
android:id="#+id/btndelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/checkAll"
android:checked="false" />
</LinearLayout>
</RelativeLayout>
custom_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/paackage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter class
public class FileAdapter extends ArrayAdapter<String>{
private List<String> filelist = null;
private Context context;
CheckBox checkAll;
public FileAdapter(Context _context, int _resource,List<String> _filelist) {
super(_context,_resource,_filelist);
this.context = _context;
this.filelist = _filelist;
this.itemChecked = new boolean[_filelist.size()];
}
private class ViewHolder {
TextView Name;
TextView pName;
CheckBox ck1;
ImageView iconview;
}
#Override
public int getCount() {
return ((null != filelist) ? filelist.size() : 0);
}
#Override
public String getItem(int position) {
return ((null != filelist) ? filelist.get(position) : null);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View view = convertView;
if (null == view) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.custom_row, null);
holder = new ViewHolder();
holder.Name = (TextView) view.findViewById(R.id.name);
holder.pName = (TextView) view
.findViewById(R.id.paackage);
holder.ck1 = (CheckBox) view.findViewById(R.id.checkBox1);
holder.iconview = (ImageView) view.findViewById(R.id.app_icon);
view.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.appName.setText();
holder.packageName.setText();
holder.iconview.setImageDrawable();
holder.ck1.setChecked(false);
if (itemChecked[position])
holder.ck1.setChecked(true);
else
holder.ck1.setChecked(false);
holder.ck1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (holder.ck1.isChecked()) {
itemChecked[position] = true;
} else {
itemChecked[position] = false;
}
}
});
return view;
}
}
ListFragment class
public class FragmentA extends ListFragment implements View.OnClickListener {
public ArrayList<String> filelist;
Button in, delete;
ListView lv;
CheckBox cb;
private FileAdapter listadaptor = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
filelist = new ArrayList<String>();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View inflatedView = inflater.inflate(R.layout.fragment_app_restore, container, false);
in = (Button) inflatedView.findViewById(R.id.btnin);
delete= (Button) inflatedView.findViewById(R.id.btndelete);
lv = (ListView) inflatedView.findViewById(android.R.id.list);
empty = inflatedView.findViewById(android.R.id.empty);
//calling asynctask to load files here
delete.setOnClickListener(this);
in.setOnClickListener(this);
return inflatedView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
cb = (CheckBox) v.findViewById(R.id.checkBox1);//this is the checkbox in row only
cb.performClick();
if (cb.isChecked()) {
} else {
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btndelete:
break;
case R.id.btnin:
break;
}
}
private class LoadFiles extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
filelist = GetFiles(root_path);
if (filelist != null) {
listadaptor = new AppRestoreFileAdapter(getActivity(),
R.layout.custom_row, filelist);
} else {
// lv.setEmptyView(empty);
}
return null;
}
#Override
protected void onPostExecute(Void result) {
lv.setAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
}
}
im just trying to create a listview using a simple adapter. the setadapter is not working. this is my code. my setcontentview is before this block of code and this is in oncreate.
ArrayList<HashMap<String,String>> arraylist = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("CType", "Alarm");
map.put("RType", "Once");
map.put("hour", "1");
map.put("minute", "1");
map.put("second", "30");
arraylist.add(map);
String[] stringarray= new String[] {"CType", "RType", "hour","minute","second"};
int[] intarray = new int[] {R.id.clocktextviewalarmtimer,R.id.clocktextviewrepeatonce,
R.id.clocktextviewhours,R.id.clocktextviewminutes,R.id.clocktextviewseconds};
ListView clocklistview = (ListView)findViewById(R.id.clocklistview);
SimpleAdapter adapter = new SimpleAdapter(this,arraylist,R.layout.list_clock,stringarray,intarray);
clocklistview.setAdapter(adapter);
this is the xml that contains my listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/clocklistview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView></LinearLayout>
this is for my list_clock.xml
<?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="wrap_content"
android:weightSum="3">
<TextView
android:id="#+id/clocktextviewalarmtimer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/clocktextviewrepeatonce"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/clocktextviewhours"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/clocktextviewminutes"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/clocktextviewseconds"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:layout_weight="0.3"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
Here's an example of a custom adapter. Try this ...
public class ClockListAdapter extends BaseAdapter {
ArrayList<HashMap<String, String>> _arrayList;
ClockListAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) {
this._arrayList = arrayList;
}
#Override
public int getCount() {
return (_arrayList != null) ? _arrayList.size() : 0;
}
#Override
public Object getItem(int position) {
return (_arrayList != null) ? _arrayList.get(position) : null;
}
#Override
public long getItemId(int position) {
return (_arrayList != null) ? _arrayList.indexOf(_arrayList.get(position)) : 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
// REPLACE WITH YOUR LAYOUT FILE vvvvvvvvvv
convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder._textView.setText("set some text here");
return convertView;
}
public class ViewHolder {
// ADD YOUR VIEW(S) vvvvvvvvv
TextView _textView;
ViewHolder(View v) {
// REPLACE WITH YOUR TEXT vvvvvvvvv
_textView = (TextView) v.findViewById(R.id.textView1);
}
}
}