ListView item selection issue. Deselect others when one selected - android

I have a custom listview: http://i.stack.imgur.com/oBCCG.png
Up on clicking an item of 'ListView' the 'ImageView' with that "mark" becomes visible.
But the problem is that I need only ONE item to be marked. How can I deselect all others items up on click?
CustomList adapter:
private class TimeZoneItemAdapter extends ArrayAdapter<String> {
private Activity myContext;
private ArrayList<String> datas;
public TimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = myContext.getLayoutInflater();
convertView = inflater.inflate(R.layout.listview_item, null);
viewHolder = new ViewHolder();
viewHolder.postNameView = (TextView) convertView.findViewById(R.id.listview_item);
viewHolder.postImageView = (ImageView) convertView.findViewById(R.id.image_mark);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.postNameView.setText(datas.get(position));
viewHolder.postNameView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//How to deselect all other items here?
if (viewHolder.postImageView.getVisibility() != View.VISIBLE) {
viewHolder.postImageView.setVisibility(View.VISIBLE);
} else {
viewHolder.postImageView.setVisibility(View.INVISIBLE);
}
}
});
return convertView;
}
class ViewHolder {
TextView postNameView;
ImageView postImageView;
}
}
setting the adapter:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_time_zone, container, false);
// Set the adapter
mListView = (ListView) view.findViewById(R.id.listView_timezones);
imageCheck = (ImageView) view.findViewById(R.id.image_mark);
mListView.setAdapter(new TimeZoneItemAdapter(
getActivity(),
R.layout.listview_item,
dataTimezones.getZonesList()
));
return view;
}
Thanks for any help!

You might want to keep track of the currently selected list view item position in a dedicated integer variable. Then you would only need to call notifyDataSetChanged() on the adapter each time an item is selected and set the ImageView as visible in getView() if yourSelectedPosition == position.
Also check out the setOnItemClickListener() method on ListView.
EDIT:
Here's some code.
First in the Activity attach a listener to the ListView:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
timeZoneItemAdapter.selectedPosition = position;
timeZoneItemAdapter.notifyDataSetChanged();
}
});
and the adapter should look something like this:
private class TimeZoneItemAdapter extends ArrayAdapter<String> {
private Activity myContext;
private ArrayList<String> datas;
public int selectedPosition = -1;
public TimeZoneItemAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
super(context, textViewResourceId, objects);
myContext = (Activity) context;
datas = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = myContext.getLayoutInflater().inflate(R.layout.listview_item, null);
TextView postNameView = (TextView) convertView.findViewById(R.id.listview_item);
ImageView imageMark = (ImageView) convertView.findViewById(R.id.image_mark);
postNameView.setText(datas.get(position));
imageMark.setVisibility(selectedPosition == position ? View.VISIBLE : View.GONE);
return convertView;
}
}
This solution gets rid of the ViewHolder, but would it work for you?

Related

Custom ListView is not working properly when clicked

So I have a simple Custom ListView. When a button in the ListView is clicked, a Toast is supposed to be created (as a test, will later switch to an activity depending on the pressed category).
The problem is, that nothing happens when any of the buttons are clicked.
Not sure how to fix it at the moment.
Category Table
public class CategoryTable extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.category_table);
populateTable();
}
private void populateTable() {
final ListView mListView = findViewById(R.id.listView);
Category One = new Category(" One");
Category Two = new Category(" Two");
Category Three = new Category(" Three");
Category Four = new Category(" Four");
final ArrayList<Category> categoryList = new ArrayList<>();
categoryList.add(One);
categoryList.add(Two);
categoryList.add(Three);
categoryList.add(Four);
CategoryListAdapter adapter = new CategoryListAdapter(this, R.layout.adapter_view_layout, categoryList);
mListView.setAdapter(adapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast toast = Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
Custom Adapter
public class CategoryListAdapter extends ArrayAdapter<Category> {
private Context mContext;
int mResource;
public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
Category category = new Category(name);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
Button catName = convertView.findViewById(R.id.btnNextTbl);
catName.setText(name);
return convertView;
}
}
I've also added
android:descendantFocusability="blocksDescendants"
to the custom adapter xml file (didn't seem to change anything though).
Will include more code if needed.
Any help would be greatly appreciated.
its because you have the listener for when you click the actual row, not the button inside the row. if you want it on button click you will need to do something like this
public class CategoryListAdapter extends ArrayAdapter<Category> {
private Context mContext;
int mResource;
public CategoryListAdapter(Context context, int resource, ArrayList<Category> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
Category category = new Category(name);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
Button catName = convertView.findViewById(R.id.btnNextTbl);
catName.setText(name);
catName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//make call back to activity to do whatever you want
}
});
return convertView;
}
}

ListView modify other view from specific view

I have a ListView that contains 5 rows with 5 button:
When I click on first button, I replace it text with "B".
Then, I click on second button, I replace it text with "B", but how can I replace the previous clicked button (the first) with "A"?
This is what really happen:
This is my code for the button click:
private int resource;
private LayoutInflater inflater;
ViewHolder holder;
private Context ctx;
public RingtoneAdapter(Context context, int resourceId, List<Ringtone> objects) {
super(context, resourceId, objects);
this.ctx = context;
resource = resourceId;
inflater = LayoutInflater.from(context);
}
public View getView(final int position, View v, ViewGroup parent) {
// Recuperiamo l'oggetti che dobbiamo inserire a questa posizione
final Ringtone ringtone = getItem(position);
if (v == null) {
v = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
holder.btnPlay = (Button) v.findViewById(R.id.btnPlay);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button btnadd = (Button) view;
btnadd.setText("B");
notifyDataSetChanged();
}
});
return v;
}
private static class ViewHolder {
Button btnPlay;
TextView txtTitolo;
TextView txtCategoria;
}
If I'm reading this correctly, you only want a single position to be 'selected' at a time. If that's the case, you could keep a reference to the selected position, and use the Adapter's functionality instead of having multiple buttons know about each other. e.g.
private int selectedPosition = -1;
#Override
public View getView(final int position, View convertView, ViewGroup parent){
// ... Your ViewHolder / Ringtone initialization
if(position == selectedPosition)
holder.btnPlay.setText("B");
else holder.btnPlay.setText("A");
holder.btnPlay.setOnClickListener(new View.OnClickListener(){
if(holder.btnPlay.getText().toString().equals("A"))
selectedPosition = position;
else selectedPosition = -1;
notifyDataSetChanged();
});
return convertView;
}
The key here is allowing notifyDataSetChanged() to internally call your getView method, which then really only needs to know how to render the data in its current state, instead of dealing with the complex logic required to map between multiple Views.
you need to reset the items which were not clicked,
btn.setText("B"); // if the item was clicked
btn.setText("A"); // for all other items
what you can do is, cycle through all elements setting the text for button like this btn.setText("A"), once this is done call btn.setText("B")
you should be using OnItemClickListener on the listview, it is easy that way, as you are setting the value in view holder, it does not give you access to the other items in the listview, but by using OnItemClickListener you can cover all the elements in the listview once you update things as you like call notifyDataSetChanged(), As this can be done there is another way to look at the problem
ListView listView = (ListView) findViewById(R.id.ListViewTestSevenActivity_listView);
ArrayList<Item> items = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Item item = new Item("A");
items.add(item);
}
MyAdapter adapter = new MyAdapter(getApplicationContext(), R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
This will take care of initialization and rest of the code as you want it to work. code is also available on github
private class MyAdapter extends ArrayAdapter {
private Context a_context;
private ArrayList<Item> items;
private LayoutInflater a_layoutInflater;
public MyAdapter(Context context, int resource, ArrayList<Item> items) {
super(context, resource, items);
this.a_context = context;
this.items = items;
a_layoutInflater = LayoutInflater.from(this.a_context);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
row = a_layoutInflater.inflate(R.layout.single_item_listview_seven, parent, false);
holder = new ViewHolder();
holder.button = (Button) row.findViewById(R.id.ListViewTestSevenActivity_text_button);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final Item item = items.get(position);
System.out.println("" + item.getText());
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (Item current_item : items) {
current_item.setText("A");
}
item.setText("B");
notifyDataSetChanged();
}
});
holder.button.setText("" + item.getText());
return row;
}
private class ViewHolder {
Button button;
}
}
private class Item {
String text;
public Item(String text) {
this.text = text;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}

ListView adapter and onClickListener

How to set onClickListener for button for each element in ListView?
When I do something like this:
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.element, parent, false);
}
mButton = (Button) convertView.findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mButton.setText("Clicked");
}
});
return convertView;
}
And when I press the button I get this errors:
android.content.res.Resources$NotFoundException: String resource ID #0x4c
at android.content.res.Resources.getText(Resources.java:286)
at android.widget.TextView.setText(TextView.java:4148)
at the.vizimir.onezone.adapters.PostListViewAdapter$1.onClick(ListViewAdapter.java:119)
at android.view.View.performClick(View.java:4785)
at android.view.View$PerformClick.run(View.java:19858)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
UPDATE:
I don't get errors but after clicking Button text is chaning not in this element where I press, but in other.
public class PostListViewAdapter extends ArrayAdapter<Post> {
private List<Post> mPostList;
private Context mContext;
Button mPlusImageButton;
TextView mTextTextView;
public PostListViewAdapter(List<Post> postList, Context context){
super(context, R.layout.post, postList);
mPostList = postList;
mContext = context;
}
public View getView(final int position, View convertView, ViewGroup parent){
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.post, parent, false);
}
mPlusImageButton = (Button) convertView.findViewById(R.id.plusImageButton);
mTextTextView = (TextView) convertView.findViewById(R.id.textTextView);
String mText = mPostList.get(position).getText();
mTextTextView.setText(mText);
int a = position;
mPlusImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPlusImageButton.setText("A");
}
});
return convertView;
}
#Override
public int getCount(){
return mPostList.size();
}
}
Maybe you should try to use:
Button thisButton = (Button) v;
instead of using it straight, if you totally sure that problem is onClick()
So, i've found a time, and made simple example, which works :
MainActivity.class:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<String> items = new ArrayList<String>();
items.add("1");
items.add("2");
items.add("3");
TestAdapter adapter = new TestAdapter(this, R.layout.list_item, R.id.title, items);
listView.setAdapter(adapter);
}
}
TestAdapter.class(i know i'm not using ViewHolder pattern and so on, i've just made a class base on your example) :
public class TestAdapter extends ArrayAdapter<String>{
private List<String> items;
private Context mContext;
public TestAdapter(Context context, int resource, int textViewResourceId,
List<String> items) {
super(context, resource, textViewResourceId, items);
this.items = items;
mContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
}
final Button mButton = (Button) convertView.findViewById(R.id.button);
TextView mTitle = (TextView) convertView.findViewById(R.id.title);
mTitle.setText(items.get(position));
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mButton.setText("clicked");
}
});
return convertView;
}
#Override
public int getCount() {
return items.size();
}
}
So i need all of your code to see what's happening
Edit : Ofcourse you get text on wrong Button, you need to create them in getView(like in my post), and not as field in your adapter class.
you do not need to use buttons.
For listviews use onListItemClick. Its a method built into ListFragment. Otherwise use listview.setOnItemClickListener(); hope that helps. They take parameters ListView l, View v, int position, long id
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mButton.setText("clicked");
notifyDataSetChanged();
}
});

Multiselection ListView not working properly

So I have managed to make my listview highlight selected items, but I have now run into a new problem, If I select an item, it highlights, and then once I scroll down, another item is highlighted, I know the issue is because my listview adapter recycles the view to optimise performance, I am just wondering, how do I stop it from doing that??
Here is my Fragment
public class SecondFragment extends Fragment {
CardDatabaseAdapter2 mCardDatabaseAdapter;
ExerciseHistoryAdapter mExerciseHistoryAdapter;
ArrayList<Set> sets = new ArrayList<>();
ArrayList<Set> selected = new ArrayList<>();
String name;
ListView lv;
int x;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.exercise_history, container, false);
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
name = extras.getString("ExerciseName");
}
mCardDatabaseAdapter = new CardDatabaseAdapter2(getActivity());
sets = mCardDatabaseAdapter.getAllSetsWithName(name);
Log.v("array", sets.toString());
lv = (ListView) rootView.findViewById(R.id.history_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(!selected.contains(sets.get(position))) {
lv.setItemChecked(position, true);
view.setBackgroundColor(Color.rgb(255, 152, 0));
Toast.makeText(getActivity(), "Item Selected", Toast.LENGTH_SHORT).show();
selected.add(sets.get(position));
} else {
lv.setItemChecked(position, false);
view.setBackgroundColor(Color.rgb(221, 221, 221));
selected.remove(sets.get(position));
Toast.makeText(getActivity(), "Item Unselected", Toast.LENGTH_SHORT).show();
}
}
});
Button add = (Button) rootView.findViewById(R.id.add_button);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
mExerciseHistoryAdapter= new ExerciseHistoryAdapter(getActivity(), R.layout.history_listview_item, sets);
lv.setAdapter(mExerciseHistoryAdapter);
return rootView;
}
}
And here is my Adapter
public class ExerciseHistoryAdapter extends ArrayAdapter {
Context mContext;
int mLayoutResourceId;
ArrayList<Set> mData;
PlaceHolder holder;
public ExerciseHistoryAdapter(Context context, int resource, ArrayList<Set> data) {
super(context, resource, data);
this.mContext = context;
this.mLayoutResourceId = resource;
this.mData = data;
}
#Override
public Object getItem(int position) {
return super.getItem(position);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = null;
//inflate the layout for a single row
if(row == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);
holder = new PlaceHolder();
holder.weight = (TextView) row.findViewById(R.id.rowWeight);
holder.reps = (TextView) row.findViewById(R.id.rowReps);
row.setTag(holder);
} else {
//otherwise use an existing one
holder = (PlaceHolder) row.getTag();
Log.v("Position", "i " + position);
}
//getting the data from the data array
Set place = mData.get(position);
//setting the view to reflect the data we need to display
holder.weight.setText(String.valueOf(place.getWeight()));
holder.reps.setText(String.valueOf(place.getReps()));
holder.reps.setTextColor(Color.GRAY);
holder.weight.setTextColor(Color.GRAY);
//returning the row
return row;
}
private static class PlaceHolder {
TextView weight;
TextView reps;
}
}
In your getView() method you need to check selection state and update UI accordingly.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = null;
//inflate the layout for a single row
if (row == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
row = inflater.inflate(mLayoutResourceId, parent, false);
holder = new PlaceHolder();
holder.weight = (TextView) row.findViewById(R.id.rowWeight);
holder.reps = (TextView) row.findViewById(R.id.rowReps);
row.setTag(holder);
} else {
//otherwise use an existing one
holder = (PlaceHolder) row.getTag();
Log.v("Position", "i " + position);
}
//getting the data from the data array
Set place = mData.get(position);
//setting the view to reflect the data we need to display
holder.weight.setText(String.valueOf(place.getWeight()));
holder.reps.setText(String.valueOf(place.getReps()));
holder.reps.setTextColor(Color.GRAY);
holder.weight.setTextColor(Color.GRAY);
// Update color based on selected state
row.setBackgroundColor(
lv.isItemChecked(position)
? Color.rgb(255, 152, 0) // selected color
: Color.rgb(221, 221, 221) // normal color
);
//returning the row
return row;
}
To get the lv reference:
ListView lv;
public ExerciseHistoryAdapter(Context context, int resource, ArrayList<Set> data, ListView lv) {
super(context, resource, data);
this.mContext = context;
this.mLayoutResourceId = resource;
this.mData = data;
this.lv = lv;
}
And in your fragment:
mExerciseHistoryAdapter= new ExerciseHistoryAdapter(getActivity(), R.layout.history_listview_item, sets, lv);

How to save state of CheckBox while scrolling in ListView?

I have used ListView with custom adapter, so my ListView includes CheckBox, but problem is that when I scroll down, checked CheckBoxes uncheck, how can I do it with this code?
UPDATE
My custom adapter:
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;
private final String DefaultApps = "def_apps";
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");
LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.apps, parent, false);
TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
text_apps.setText(prefNameDefaultApps.split("\n")[position]);
check.setChecked(checkedItems.contains((Integer) position));
return rowView;
}
}
And my Activity:
public class AppsActivity extends ListActivity {
private final String DefaultApps = "def_apps";
private List<Integer> checkedItems = new ArrayList<Integer>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppsArrayAdapter adapter = new AppsArrayAdapter(this, prefNameDefaultApps.split("\n"), checkedItems);
setListAdapter(adapter);
}
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
if(checkedItems.contains((Integer) position)) checkedItems.remove((Integer) position);
else checkedItems.add((Integer) position);
}
// initialise the list
private List<Integer> selection = new ArrayList<Integer>();
for (String str: prefNameDefaultApps.split("\n")) {
selection.add(0);
}
// setting the list
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
if(CheckBox is checked) {//(check if the checkbox is checked )
selection.set(position, 1);
}
else{
selection.set(position, 0);
}
}
// show in the listView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
....
....
check.setChecked(selection.get(position) == 0 ? false : true);
}
This should get you going :)
I'm not entirely sure whether there is a standard solution in Android, but this should do the trick:
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;//Here we save the position of the selected items.
private final String DefaultApps = "def_apps";
//This will be invoked when one of the CheckBoxes gets clicked. It will update the checkedItems list.
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer position = (Integer) v.getTag();
if(checkedItems.contains(position)) checkedItems.remove(position);
else checkedItems.add(position);
}
};
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
//You shouldn't inflate the View everytime getView() is called,
/*LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.apps, parent, false);*/
//rather let the superclass handle it as it's more efficient.
View rowView = super.getView(position, convertView, parent);
TextView text_apps = (TextView)rowView.findViewById(R.id.text_apps);
CheckBox check = (CheckBox)rowView.findViewById(R.id.check_apps);
text_apps.setText(prefNameDefaultApps.split("\n")[position]);
check.setChecked(checkedItems.contains((Integer) position));//XXX
check.setTag((Integer) position);//XXX
check.setOnClickListener(onClickListener);//XXX
return rowView;
}
}
In your ListActivity, add a field private List<Integer> checkedItems = new ArrayList<Integer>;. You have to pass this List in the Adapter constructor!
If you want to check whether the nth item in your ListView was selected, just look whether n is saved in checkedItems, i.e.
if(checkedItems.contains(requestedItemPosition)) {
//Item was selected.
} else {
//Item was not selected.
}
I really recommend you not to inflate the views by yourself.
Try this: (your custom adapter)
public class AppsArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private List<Integer> checkedItems;
private final String DefaultApps = "def_apps";
LayoutInflater inflater;
public AppsArrayAdapter(Context context, String[] values, List<Integer> checkedItems) {//XXX
super(context, R.layout.apps, values);
this.context = context;
this.checkedItems = checkedItems;//XXX
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences myPrefsApps =context.getSharedPreferences("myPrefsApps", Context.MODE_PRIVATE);
String prefNameDefaultApps = myPrefsApps.getString(DefaultApps, "");
String prefNameDefaultAppsVer = myPrefsApps.getString(DefaultAppsVer, "");
View rowView = convertView;
ViewHolder viewH = new ViewHolder();
if ( rowView == null){
rowView = inflater.inflate(R.layout.apps, parent, false);
viewH.text_apps = (TextView)rowView.findViewById(R.id.text_apps);
viewH.check = (CheckBox)rowView.findViewById(R.id.check_apps);
rowView.setTag(viewH);
}else{
viewH = (ViewHolder) rowView.getTag();
}
viewH.text_apps.setText(prefNameDefaultApps.split("\n")[position]);
viewH.check.setChecked(checkedItems.contains((Integer) position));
return rowView;
}
static class ViewHolder {
public TextView text_apps;
public CheckBox check;
}
}
Hope this helps :)
you should use
MyAdapter newad=(MyAdapter)adapter.getAdapter();
newad.notifyDataSetChanged();
this code in onitem click method
and MyAdapter is class of Adapter and make new object like this one
and in adapter class you should have to write a line
LayoutInflater in;
in=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
in adapter constructor simple

Categories

Resources