Android BaseAdapter only shows correct information once - java

I'm trying to put together an adapter that will display the number of occurrences in a list. There are two ListViews in a tab host with each being in its own tab. As long as you do not click on the tab that shows the occurrences it will keep up with the data input just fine. The first time you go to view the listview, all of the groups are displayed with the correct amount of occurrences for each; however, from then on it does not want to update properly. At times it will not update at all, re-insert an existing group but still keep the old one.
For example, if I were to put in 10, 20, and 30 the first time I go to view the groups listview I would see:
10 -- 1
20 -- 1
30 -- 1
...but if I were to put in another 10 it would still display the same thing with no update.
When I put something else in like 45 I get:
10 -- 1
20 -- 1
30 -- 1
10 -- 2
I can keep adding other numbers and it will just keep adding in 10 -- 1's or sometimes even more 20's or 30's. If I use debugAll() in the log window it will show that it attempts all of the numbers that I put in, and it will return the correct number of occurences, just the display won't keep up and is displaying more of what is already there.
I have tried putting in several log statements and they show that the information in the adapter is right, just the display is wrong.
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
public class HashAdapter extends BaseAdapter {
private LinkedHashMap<String, String> mData = new LinkedHashMap<String, String>();
private final ArrayList keyArray = new ArrayList();
private final Context _context;
public HashAdapter (Context context) {
_context = context;
}
#Override
public int getCount() {
return keyArray.size();
}
#Override
public String getItem(int position) {
return mData.get(keyArray.get(position));
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View listView;
final LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) { // FIXME
listView = inflater.inflate(R.layout.list_item, null);
TextView textView = (TextView) listView.findViewById(R.id.listItem);
String tile = keyArray.get(position) + " -- " + getItem(position);
textView.setText(tile);
Log.i("getView Text Is", tile);
} else {
listView = convertView;
}
return listView;
}
public void addNewValue (String key) {
if (mData.containsKey(key)) {
int retrieve = Integer.parseInt(mData.get(key));
mData.put(key, String.valueOf(++retrieve));
} else { // HASH MAP CREATING A NEW KEY WITH VALUE OF 1
mData.put(key, "1");
keyArray.add(0, key);
}
this.notifyDataSetChanged();
Log.i("Array List ", keyArray.toString());
}
public void modifyExistingValue (String oldValue, String newValue) {
if (!oldValue.equals(newValue)) {
int newInt = Integer.parseInt(mData.get(oldValue));
if (newInt > 1) {
mData.put(oldValue, String.valueOf(--newInt));
String secondInt = mData.get(newValue);
if (secondInt != null) {
int newNewInt = Integer.parseInt(secondInt);
mData.put(newValue, String.valueOf(++newNewInt));
} else {
mData.put(newValue, "1");
}
} else {
mData.remove(oldValue);
keyArray.remove(oldValue);
mData.put(newValue, "1");
Log.i("Old Value ", oldValue + " Is Gone From HashMap");
}
keyArray.add(0, newValue);
}
this.notifyDataSetChanged();
Log.i("Array List ", keyArray.toString());
}
public void removeOccurrence (String oldValue) {
String oldOccurrences = mData.get(oldValue);
if (!oldOccurrences.equals("1")) { // IF THERE ARE ENOUGH REMAINING TO STILL EXIST
int newInt = Integer.parseInt(oldOccurrences);
mData.put(oldValue, String.valueOf(--newInt));
} else { // NOT ENOUGH LEFT...REMOVE IT
mData.remove(oldValue);
keyArray.remove(oldValue);
Log.i("Old Value", oldValue + " Is Gone From HashMap");
}
this.notifyDataSetChanged();
Log.i("Array List ", keyArray.toString());
}
public void clear () {
mData.clear();
keyArray.clear();
this.notifyDataSetChanged();
Log.i("Array List ", keyArray.toString());
}
public void debugValue (String key) {
Log.i("AdapterDebug Value", key + " has " + mData.get(key));
}
public void debugAll () {
int size = keyArray.size();
for (int i = 0; i < size; i++) {
String retrieve = (String) keyArray.get(i);
Log.i("AdapterDebug All", "Attempted " + retrieve + " and retrieved " + mData.get(retrieve));
}
}
}
EDIT:
I figured some one might find the fully working version useful so I'll put this here.
It will keep track of re occurring values. Build off of it, use it as is, I don't care.
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.LinkedHashMap;
/*
* ARRAY ADAPTER FOR KEEPING TRACK OF REOCCURRING VALUES AND DISPLAYING ON A LISTVIEW
* CREATED BY : COREY WILLIAMS
* 8/13/2014
*
* BeerWare (http://en.wikipedia.org/wiki/Beerware)
* */
class HashAdapter extends BaseAdapter {
private LinkedHashMap<String, String> mData = new LinkedHashMap<String, String>();
private ArrayList keyArray = new ArrayList();
private final Context _context;
public HashAdapter (Context context) {
_context = context;
}
#Override
public int getCount() {
return keyArray.size();
}
#Override
public String getItem(int position) {
return mData.get(keyArray.get(position));
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View listView;
final LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
listView = inflater.inflate(R.layout.list_item, null);
TextView textView = (TextView) listView.findViewById(R.id.listItem);
String tile = keyArray.get(position) + " -- " + getItem(position);
textView.setText(tile);
} else {
listView = convertView;
TextView textView = (TextView) listView.findViewById(R.id.listItem);
String tile = keyArray.get(position) + " -- " + getItem(position);
textView.setText(tile);
}
return listView;
}
public void addNewValue (String key) {
if (mData.containsKey(key)) {
int retrieve = Integer.parseInt(mData.get(key));
mData.put(key, String.valueOf(++retrieve));
} else { // HASH MAP CREATING A NEW KEY WITH VALUE OF 1
mData.put(key, "1");
keyArray.add(0, key);
}
this.notifyDataSetChanged();
}
public void modifyExistingValue (String oldValue, String newValue) {
if (!oldValue.equals(newValue)) {
int oldAmmount = Integer.parseInt(mData.get(oldValue)); // GET HOW MANY ARE LEFT OF WHAT YOU SELECTED
boolean alreadyHasNewValue = mData.containsKey(newValue);
if (oldAmmount > 1) { // IF THERE IS SOME LEFT
mData.put(oldValue, String.valueOf(--oldAmmount));
if (alreadyHasNewValue) {
String secondInt = mData.get(newValue);
int newNewInt = Integer.parseInt(secondInt);
mData.put(newValue, String.valueOf(++newNewInt));
} else {
mData.put(newValue, "1");
}
} else {
mData.remove(oldValue);
keyArray.remove(oldValue);
if (alreadyHasNewValue) {
String secondInt = mData.get(newValue);
int newNewInt = Integer.parseInt(secondInt);
mData.put(newValue, String.valueOf(++newNewInt));
} else {
mData.put(newValue, "1");
}
}
if (!keyArray.contains(newValue)) {
keyArray.add(0, newValue);
}
}
this.notifyDataSetChanged();
}
public void removeOccurrence (String oldValue) {
String oldOccurrences = mData.get(oldValue);
if (!oldOccurrences.equals("1")) { // IF THERE ARE ENOUGH REMAINING TO STILL EXIST
int newInt = Integer.parseInt(oldOccurrences);
mData.put(oldValue, String.valueOf(--newInt));
} else { // NOT ENOUGH LEFT...REMOVE IT
mData.remove(oldValue);
keyArray.remove(oldValue);
}
this.notifyDataSetChanged();
}
public void clear () {
mData.clear();
keyArray.clear();
this.notifyDataSetChanged();
}
public ArrayList getKeys () {
return keyArray;
}
public void restoreKeys (ArrayList<String> restore) {
keyArray = restore;
this.notifyDataSetChanged();
}
public LinkedHashMap<String, String> getValues () {
return mData;
}
public void restoreValues (LinkedHashMap<String, String> restore) {
mData = restore;
this.notifyDataSetChanged();
}
}

In the "getView" method you must update the content placeholders (TextView(s) in your code) even if "convertView" is not null.
It's because of view recycling system of ListView.
So the updated code could be like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View listView;
final LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
listView = inflater.inflate(R.layout.list_item, null);
} else {
listView = convertView;
}
TextView textView = (TextView) listView.findViewById(R.id.listItem);
String tile = keyArray.get(position) + " -- " + getItem(position);
textView.setText(tile);
Log.i("getView Text Is", tile);
return listView;
}

Related

Android String reference to shared preferences cannot be iterated?

So I have an ArrayList named locationsList. I also have SharedPreferences that hold a city name which was set in the previous activity which i want to add onto my ArrayList. BUT I want to add it with a ">" at the end of it. So for example
SharedPreferences prefs = getActivity().getSharedPreferences("prefs", 0);
locationsList.add(0, prefs.getString("city", "no city") + ">");
However it does not change!!!. the ">" Isn't added. I even tried adding it when i set the text of the textView.
TextView tv...;
tv.setText(locationsList.get(0) + ">");
I dont understand why it cant change. Obviously the array list is holding a reference to the preferences and that cannot change. But I even tried assigning the preferences to a string variable and then iterating it, it doesn't budge. Can anyone help me
Async in activity
class getLocationsUrl extends AsyncTask<String, String, String> {
ArrayList<String> tempList = new ArrayList<String>();
#Override
protected String doInBackground(String... arg0) {
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("city", prefs.getString("city", "durban")));
JSONParser parser = new JSONParser();
JSONObject json = parser.makeHttpRequest(IMAGE_URL + "fetchlocations.php", "POST",
params);
try {
list = json.getJSONArray("posts");
for(int i = 0; i < list.length(); i++) {
JSONObject c = list.getJSONObject(i);
String location = c.getString("location");
tempList.add(location);
Log.d("async trend", trendList.get(0));
}
Log.d("Got list", imageUrl.get(0) + " " + trendList.get(0));
} catch(JSONException je) {
Log.d("json exception", "json exception");
je.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
locationsList = tempList;
locationsFragment.updateAdapter();
feedFragment.updateHeadingAdapter();
}
}
FeedFragment set up in onCreateView
headingPager = (ViewPager) view.findViewById(R.id.headingPager);
headingList = (ArrayList<String>) NightWatch.locationsList.clone();
added = prefs.getString("city", "makaka");
headingList.add(0, added + ">");
headingAdapter = new CustomHeadingPagerAdapter(getChildFragmentManager());
headingPager.setAdapter(headingAdapter);
FeedFragments CustomHeadingAdapter
public class CustomHeadingPagerAdapter extends FragmentPagerAdapter {
public CustomHeadingPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int arg0) {
return HeadingFragment.newInstance(arg0, headingList);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return headingList.size();
}
}
FeedFragment's updateHeadingAdapter() called from the Async
public void updateHeadingAdapter() {
headingList = (ArrayList<String>) NightWatch.locationsList.clone();
headingList.add(0, (prefs.getString("city", "null") + ">"));
headingAdapter = new CustomHeadingPagerAdapter(getChildFragmentManager());
headingPager.setAdapter(headingAdapter);
}
And finally the HeadingFragment that I return in the adapter.
package info.nightowl.nightowl;
import java.util.ArrayList;
import com.example.nightowl.R;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class HeadingFragment extends Fragment{
int position;
Typeface font;
ArrayList<String> list;
SharedPreferences prefs;
static HeadingFragment newInstance(int position, ArrayList<String> list) {
final HeadingFragment f = new HeadingFragment();
Bundle args = new Bundle();
args.putInt("position", position);
args.putStringArrayList("list", list);
f.setArguments(args);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
position = getArguments().getInt("position");
list = getArguments().getStringArrayList("list");
prefs = getActivity().getSharedPreferences("prefs", 0);
font = Typeface.createFromAsset(getActivity().getAssets(),
"NotCourierSans.ttf");
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.heading_viewpage_layout, null);
TextView tv = (TextView) v.findViewById(R.id.headingText);
if(position == 0) tv.setText(">" + list.get(position) + ">");
else tv.setText(list.get(position) + ">");
tv.setTypeface(font);
tv.setTextColor(Color.YELLOW);
tv.setTextSize(30);
return v;
}
}
I got it. Always remember when iterating to a string text vi you set it to be a single line in the xml so...
android:singleLine="true"
Kind of a dumb mistake really

getView not returning view parameters

I have an AdapterView that displays a list and headers that separate different list categories. The list is populated by a SQLite database with user input. So far everything is working.
I want to remove the headers when there are no items in the header's category. The code is as follows:
public View getView(int position, View convertView, ViewGroup parent)
{
int viewType = getItemViewType(position);
if (viewType == TYPE_NORMAL)
{
// If the convertView is a textview (group), ignore it
if (convertView instanceof TextView)
{
convertView = null;
}
final int mapCursorPos = getInteralItemPosition(position);
return super.getView(mapCursorPos, convertView, parent);
}
else
{
// Check if it's a text view
TextView text;
if (convertView == null || !(convertView instanceof TextView))
{
((ListView) parent).setDivider(null);
((ListView) parent).setDividerHeight(0);
text = (TextView) inflater.inflate(R.layout.main_list_group, parent, false);
}
else
{
text = (TextView) convertView;
}
final String group = groupsIndexer.get(position).getName(resources);
//Code to hide menu title.
text.setText(group);
text.setVisibility(View.INVISIBLE);
myCursor = getCursor();
initCols(myCursor);
myCursor.moveToFirst();
while (myCursor.isAfterLast() == false)
{
String groupCursor = getGroup(myCursor).getName(resources);
if (groupCursor == group)
{
text.setHeight(12);
text.setPadding(20, 10, 0, 10);
text.setVisibility(View.VISIBLE);
}
myCursor.moveToNext();
}
return text;
}
}
So, as you can see I started by setting the text as invisible and then querying the database to check if there is any item in their specific category and if there is I set the visibility to visible.
However when returning, the method seems to ignore whatever's in the while loop. Is it not possible to format each textview object individually through getView() like this?
Thanks in advance.
Here's something that should put you in the right direction.
import java.util.ArrayList;
import java.util.Collections;
import android.content.Context;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.graphics.Paint;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class GroupedListAdapter extends BaseAdapter
{
private static final int TYPE_NORMAL = 0;
private static final int TYPE_GROUP = 1;
private static final int MAX_TYPES = 2;
private final Context mContext;
private final LayoutInflater mInflater;
private final ContactManager mContactManager;
private final ArrayList<GroupItem> mItems = new ArrayList<GroupItem>();
private final DataSetObserver mDataSetObserver = new MyDataSetObserver();
private boolean mDataValid = false;
private Cursor mCursor = null;
private int mIndexId;
private int mIndexSummary;
private int mIndexType;
private int mIndexSender;
private int mIndexReciever;
private int mIndexCompleted;
private int mIndexCompletedBy;
public GroupedListAdapter(Context context)
{
mContext = context;
mContactManager = new ContactManager(context);
mInflater = LayoutInflater.from(context);
}
public void changeCursor(Cursor newCursor)
{
if(newCursor == mCursor){
return;
}
Cursor oldCursor = mCursor;
if(oldCursor != null) {
if(mDataSetObserver != null){
oldCursor.unregisterDataSetObserver(mDataSetObserver);
}
}
mCursor = newCursor;
if(newCursor != null){
if(mDataSetObserver != null){
newCursor.registerDataSetObserver(mDataSetObserver);
}
mIndexId = newCursor.getColumnIndexOrThrow(DatabaseContract.Task.Columns._ID);
mIndexSummary = newCursor.getColumnIndexOrThrow(DatabaseContract.Task.Columns.SUMMARY);
mIndexType = newCursor.getColumnIndexOrThrow(DatabaseContract.Task.Columns.TYPE);
mIndexSender = newCursor.getColumnIndexOrThrow(DatabaseContract.Task.Columns.SENDER);
mIndexReciever = newCursor.getColumnIndexOrThrow(DatabaseContract.Task.Columns.RECIEVER);
mIndexCompleted = newCursor.getColumnIndexOrThrow(DatabaseContract.Task.Columns.COMPLETED);
mIndexCompletedBy = newCursor.getColumnIndexOrThrow(DatabaseContract.Task.Columns.COMPLETED_BY);
// calculate types and index mappings
/*
* 1. Sent (5)
* 2. Item 5
* 3. Item 6
* 4. Received (9)
* 5. Item 9
* 6. Today (1)
* 7. Item 1
* 8. Item 2
* 9. Item 3
*
*/
// allocate
int count = newCursor.getCount();
mItems.ensureCapacity(count);
if(newCursor.moveToFirst()){
int i = 0;
do{
mItems.add(new GroupItem(getGroupId(newCursor), i, TYPE_NORMAL));
i++;
}while(newCursor.moveToNext());
}
Collections.sort(mItems);
int lastGroupId = -1;
for(int i = 0; i < mItems.size(); i++){
final GroupItem item = mItems.get(i);
if(lastGroupId != item.group_id){
mItems.add(i, new GroupItem(item, TYPE_GROUP));
lastGroupId = item.group_id;
}
}
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
}
else{
mDataValid = false;
mIndexId = -1;
mIndexSummary = -1;
mIndexType = -1;
mIndexSender = -1;
mIndexReciever = -1;
mIndexCompleted = -1;
mIndexCompletedBy = -1;
mItems.clear();
// notify the observers about the lack of a data set
notifyDataSetInvalidated();
}
if(oldCursor != null) {
oldCursor.close();
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(!mDataValid) {
throw new IllegalStateException("this should only be called when the cursor is valid");
}
GroupItem item = mItems.get(position);
if(!mCursor.moveToPosition(item.position)) {
throw new IllegalStateException("couldn't move cursor to position " + position);
}
final View view;
if(convertView == null) {
if(item.type == TYPE_GROUP){
view = mInflater.inflate(R.layout.main_list_group, parent, false);
}
else if(item.type == TYPE_NORMAL){
view = mInflater.inflate(R.layout.main_list_item, parent, false);
}
else{
throw new IllegalStateException("Bad TaskItem type");
}
}
else{
view = convertView;
}
if(item.type == TYPE_GROUP){
TextView textGroup = (TextView)view.findViewById(R.id.task_group);
textGroup.setText(mContext.getString(item.group_id));
}
else if(item.type == TYPE_NORMAL){
TextView textTitle = (TextView)view.findViewById(R.id.task_title);
TextView textDescr = (TextView)view.findViewById(R.id.task_description);
String desc;
String type = mCursor.getString(mIndexType);
if(type.equals(DatabaseContract.Task.TASK_TYPE_OWNER)){
desc = mContext.getString(R.string.desc_example);
}
else if(type.equals(DatabaseContract.Task.TASK_TYPE_RECEV)){
final String sender = mCursor.getString(mIndexSender);
final String name = mContactManager.getContactName(sender);
desc = (name != null) ? name : sender;
}
else{
final String receiver = mCursor.getString(mIndexReciever);
final String name = mContactManager.getContactName(receiver);
desc = (name != null) ? name : receiver;
}
textDescr.setText(desc);
String title = mCursor.getString(mIndexSummary);
textTitle.setText(Character.toUpperCase(title.charAt(0)) + title.substring(1));
if(mCursor.getInt(mIndexCompleted) != 0){
textTitle.setPaintFlags(textTitle.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else{
textTitle.setPaintFlags(textTitle.getPaintFlags() & ~Paint.STRIKE_THRU_TEXT_FLAG);
}
}
return view;
}
#Override
public Cursor getItem(int position)
{
if(mDataValid && mCursor != null){
mCursor.moveToPosition(mItems.get(position).position);
return mCursor;
}
else{
return null;
}
}
#Override
public long getItemId(int position)
{
if(mDataValid && mCursor != null){
if(mCursor.moveToPosition(mItems.get(position).position)){
return mCursor.getLong(mIndexId);
}
else{
return 0;
}
}
else{
return 0;
}
}
#Override
public int getItemViewType(int position)
{
if(mDataValid && mCursor != null){
return mItems.get(position).type;
}
else{
return IGNORE_ITEM_VIEW_TYPE;
}
}
#Override
public int getViewTypeCount()
{
return MAX_TYPES;
}
#Override
public int getCount()
{
if(mDataValid && mCursor != null){
return mItems.size();
}
else{
return 0;
}
}
public Cursor getCursor()
{
return mCursor;
}
#Override
public boolean hasStableIds()
{
return true;
}
private int getGroupId(Cursor c)
{
if(c.getString(mIndexType).equals(DatabaseContract.Task.TASK_TYPE_SENT)){
return R.string.group_sent;
}
else if(c.getString(mIndexType).equals(DatabaseContract.Task.TASK_TYPE_RECEV)){
return R.string.group_received;
}
else{
long time = c.getLong(mIndexCompletedBy);
long delta = (time - System.currentTimeMillis());
if(delta <= DateUtils.HOUR_IN_MILLIS){
return R.string.group_asap;
}
else if(delta <= DateUtils.DAY_IN_MILLIS){
return R.string.group_today;
}
else if(delta <= (DateUtils.DAY_IN_MILLIS * 2)){
return R.string.group_tomorrow;
}
else if(delta <= DateUtils.WEEK_IN_MILLIS){
return R.string.group_this_week;
}
else if(delta <= (DateUtils.WEEK_IN_MILLIS * 2)){
return R.string.group_next_week;
}
else{
return R.string.group_someday;
}
}
}
private class MyDataSetObserver extends DataSetObserver
{
#Override
public void onChanged()
{
mDataValid = true;
notifyDataSetChanged();
}
#Override
public void onInvalidated()
{
mDataValid = false;
notifyDataSetInvalidated();
}
}
private static class GroupItem implements Comparable<GroupItem>
{
public int group_id;
public int position;
public int type;
public GroupItem(int group_id, int position, int type)
{
this.group_id = group_id;
this.position = position;
this.type = type;
}
public GroupItem(GroupItem item, int type)
{
this.group_id = item.group_id;
this.position = item.position;
this.type = type;
}
#Override
public int compareTo(GroupItem another)
{
return group_id < another.group_id ? -1 : (group_id == another.group_id ? 0 : 1);
}
}
}
Change groupCursor == group to groupCursor.equals(group).
BTW I suggest to load the data in another thread, not in the UI thread and especially not in the getView method, because it can be called LOT of times by the ListView

CursorTreeAdapter with search implementation

I'm making an application for android and I'm using CursorTreeAdapter as ExpandableListView. I want to use a search box for displaying the filtered ExpandableListView items. Like this:
Here's the code what I've written so far:
MainActivity.java:
package com.example.cursortreeadaptersearch;
import java.util.HashMap;
import android.app.SearchManager;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.util.Log;
import android.widget.ExpandableListView;
import android.widget.SearchView;
import android.widget.SearchView.OnCloseListener;
import android.widget.SearchView.OnQueryTextListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class MainActivity extends SherlockFragmentActivity {
private SearchView search;
private MyListAdapter listAdapter;
private ExpandableListView myList;
private final String DEBUG_TAG = getClass().getSimpleName().toString();
/**
* The columns we are interested in from the database
*/
static final String[] CONTACTS_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
static final String[] GROUPS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Groups.TITLE, ContactsContract.Groups._ID,
ContactsContract.Groups.SUMMARY_COUNT,
ContactsContract.Groups.ACCOUNT_NAME,
ContactsContract.Groups.ACCOUNT_TYPE,
ContactsContract.Groups.DATA_SET };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
search = (SearchView) findViewById(R.id.search);
search.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
search.setIconifiedByDefault(false);
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
listAdapter.filterList(query);
expandAll();
return false;
}
#Override
public boolean onQueryTextChange(String query) {
listAdapter.filterList(query);
expandAll();
return false;
}
});
search.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
listAdapter.filterList("");
expandAll();
return false;
}
});
// get reference to the ExpandableListView
myList = (ExpandableListView) findViewById(R.id.expandableList);
// create the adapter
listAdapter = new MyListAdapter(null, MainActivity.this);
// attach the adapter to the list
myList.setAdapter(listAdapter);
Loader<Cursor> loader = getSupportLoaderManager().getLoader(-1);
if (loader != null && !loader.isReset()) {
runOnUiThread(new Runnable() {
public void run() {
getSupportLoaderManager().restartLoader(-1, null,
mSpeakersLoaderCallback);
}
});
} else {
runOnUiThread(new Runnable() {
public void run() {
getSupportLoaderManager().initLoader(-1, null,
mSpeakersLoaderCallback).forceLoad();
;
}
});
}
}
#Override
public void onResume() {
super.onResume();
getApplicationContext().getContentResolver().registerContentObserver(
ContactsContract.Data.CONTENT_URI, true,
mSpeakerChangesObserver);
}
#Override
public void onPause() {
super.onPause();
getApplicationContext().getContentResolver().unregisterContentObserver(
mSpeakerChangesObserver);
}
// method to expand all groups
private void expandAll() {
int count = listAdapter.getGroupCount();
for (int i = 0; i < count; i++) {
myList.expandGroup(i);
}
}
public LoaderManager.LoaderCallbacks<Cursor> mSpeakersLoaderCallback = new LoaderCallbacks<Cursor>() {
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
Log.d(DEBUG_TAG, "onCreateLoader for loader_id " + id);
CursorLoader cl = null;
HashMap<Integer, Integer> groupMap = listAdapter.getGroupMap();
if (id != -1) {
int groupPos = groupMap.get(id);
if (groupPos == 0) { // E-mail group
String[] PROJECTION = new String[] {
ContactsContract.RawContacts._ID,
ContactsContract.CommonDataKinds.Email.DATA };
String sortOrder = "CASE WHEN "
+ ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%#%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME + ", "
+ ContactsContract.CommonDataKinds.Email.DATA
+ " COLLATE NOCASE";
String selection = ContactsContract.CommonDataKinds.Email.DATA
+ " NOT LIKE ''";
cl = new CursorLoader(getApplicationContext(),
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
PROJECTION, selection, null, sortOrder);
} else if (groupPos == 1) { // Name group
Uri contactsUri = ContactsContract.Data.CONTENT_URI;
String selection = "(("
+ ContactsContract.CommonDataKinds.GroupMembership.DISPLAY_NAME
+ " NOTNULL) AND ("
+ ContactsContract.CommonDataKinds.GroupMembership.HAS_PHONE_NUMBER
+ "=1) AND ("
+ ContactsContract.CommonDataKinds.GroupMembership.DISPLAY_NAME
+ " != '') AND ("
+ ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID
+ " = '1' ))"; // Row ID 1 == All contacts
String sortOrder = ContactsContract.CommonDataKinds.GroupMembership.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
cl = new CursorLoader(getApplicationContext(), contactsUri,
CONTACTS_PROJECTION, selection, null, sortOrder);
}
} else {
// group cursor
Uri groupsUri = ContactsContract.Groups.CONTENT_SUMMARY_URI;
String selection = "((" + ContactsContract.Groups.TITLE
+ " NOTNULL) AND (" + ContactsContract.Groups.TITLE
+ " == 'Coworkers' ) OR ("
+ ContactsContract.Groups.TITLE
+ " == 'My Contacts' ))"; // Select only Coworkers
// (E-mail only) and My
// Contacts (Name only)
String sortOrder = ContactsContract.Groups.TITLE
+ " COLLATE LOCALIZED ASC";
cl = new CursorLoader(getApplicationContext(), groupsUri,
GROUPS_SUMMARY_PROJECTION, selection, null, sortOrder);
}
return cl;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in.
int id = loader.getId();
// Log.d("Dump Cursor MainActivity",
// DatabaseUtils.dumpCursorToString(data));
Log.d(DEBUG_TAG, "onLoadFinished() for loader_id " + id);
if (id != -1) {
// child cursor
if (!data.isClosed()) {
Log.d(DEBUG_TAG, "data.getCount() " + data.getCount());
HashMap<Integer, Integer> groupMap = listAdapter
.getGroupMap();
try {
int groupPos = groupMap.get(id);
Log.d(DEBUG_TAG, "onLoadFinished() for groupPos "
+ groupPos);
listAdapter.setChildrenCursor(groupPos, data);
} catch (NullPointerException e) {
Log.w("DEBUG",
"Adapter expired, try again on the next query: "
+ e.getMessage());
}
}
} else {
listAdapter.setGroupCursor(data);
}
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// is about to be closed.
int id = loader.getId();
Log.d(DEBUG_TAG, "onLoaderReset() for loader_id " + id);
if (id != 1) {
// child cursor
try {
listAdapter.setChildrenCursor(id, null);
} catch (NullPointerException e) {
Log.w(DEBUG_TAG,
"Adapter expired, try again on the next query: "
+ e.getMessage());
}
} else {
listAdapter.setGroupCursor(null);
}
}
};
private ContentObserver mSpeakerChangesObserver = new ContentObserver(
new Handler()) {
#Override
public void onChange(boolean selfChange) {
if (getApplicationContext() != null) {
runOnUiThread(new Runnable() {
public void run() {
getSupportLoaderManager().restartLoader(-1, null,
mSpeakersLoaderCallback);
}
});
}
}
};
}
MyListAdapter.java:
package com.example.cursortreeadaptersearch;
import java.util.HashMap;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorTreeAdapter;
import android.widget.TextView;
public class MyListAdapter extends CursorTreeAdapter {
public HashMap<String, View> childView = new HashMap<String, View>();
/**
* The columns we are interested in from the database
*/
private final String DEBUG_TAG = getClass().getSimpleName().toString();
protected final HashMap<Integer, Integer> mGroupMap;
private MainActivity mActivity;
private LayoutInflater mInflater;
String mConstraint;
public MyListAdapter(Cursor cursor, Context context) {
super(cursor, context);
mActivity = (MainActivity) context;
mInflater = LayoutInflater.from(context);
mGroupMap = new HashMap<Integer, Integer>();
}
#Override
public View newGroupView(Context context, Cursor cursor,
boolean isExpanded, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.list_group, parent, false);
return view;
}
#Override
public void bindGroupView(View view, Context context, Cursor cursor,
boolean isExpanded) {
TextView lblListHeader = (TextView) view
.findViewById(R.id.lblListHeader);
if (lblListHeader != null) {
lblListHeader.setText(cursor.getString(cursor
.getColumnIndex(ContactsContract.Groups.TITLE)));
}
}
#Override
public View newChildView(Context context, Cursor cursor,
boolean isLastChild, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.list_item, parent, false);
return view;
}
#Override
public void bindChildView(View view, Context context, Cursor cursor,
boolean isLastChild) {
TextView txtListChild = (TextView) view.findViewById(R.id.lblListItem);
if (txtListChild != null) {
txtListChild.setText(cursor.getString(1)); // Selects E-mail or
// Display Name
}
}
protected Cursor getChildrenCursor(Cursor groupCursor) {
// Given the group, we return a cursor for all the children within that
// group
int groupPos = groupCursor.getPosition();
int groupId = groupCursor.getInt(groupCursor
.getColumnIndex(ContactsContract.Groups._ID));
Log.d(DEBUG_TAG, "getChildrenCursor() for groupPos " + groupPos);
Log.d(DEBUG_TAG, "getChildrenCursor() for groupId " + groupId);
mGroupMap.put(groupId, groupPos);
Loader loader = mActivity.getSupportLoaderManager().getLoader(groupId);
if (loader != null && !loader.isReset()) {
mActivity.getSupportLoaderManager().restartLoader(groupId, null,
mActivity.mSpeakersLoaderCallback);
} else {
mActivity.getSupportLoaderManager().initLoader(groupId, null,
mActivity.mSpeakersLoaderCallback);
}
return null;
}
// Access method
public HashMap<Integer, Integer> getGroupMap() {
return mGroupMap;
}
public void filterList(CharSequence constraint) {
// TODO Filter the data here
}
}
I have very considerably simplified and cleaned the code (so that you guys that not need to do).
As you can see, I've in total 3 cursors (1 for the groups and 2 for the children). The data is get from ContactsContract (which are the contacts of the user).
The cursor from child 1 represents all the e-mails of all contacts and the cursor from child 2 represents all the display names of the contacts. (The most of the loader functions is from here).
The only thing is now how do I implement a search? Should I do it trough Content Provider or a raw query in the database? I would like that the results of both children tables is displayed. I think because it's easy to make a fault while typing that tokenize=porter is a option in my case.
I hope that someone can point me in a good direction.
Edit:
I've tried this in MyListAdapter.java (with FilterQueryProvider as suggested by Kyle I.):
public void filterList(CharSequence constraint) {
final Cursor oldCursor = getCursor();
setFilterQueryProvider(filterQueryProvider);
getFilter().filter(constraint, new FilterListener() {
public void onFilterComplete(int count) {
// assuming your activity manages the Cursor
// (which is a recommended way)
notifyDataSetChanged();
// stopManagingCursor(oldCursor);
// final Cursor newCursor = getCursor();
// startManagingCursor(newCursor);
// // safely close the oldCursor
if (oldCursor != null && !oldCursor.isClosed()) {
oldCursor.close();
}
}
});
}
private FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
// assuming you have your custom DBHelper instance
// ready to execute the DB request
String s = '%' + constraint.toString() + '%';
return mActivity.getContentResolver().query(ContactsContract.Data.CONTENT_URI,
MainActivity.CONTACTS_PROJECTION,
ContactsContract.CommonDataKinds.GroupMembership.DISPLAY_NAME + " LIKE ?",
new String[] { s },
null);
}
};
And this in MainActivity.java:
search.setOnQueryTextListener(new OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
listAdapter.filterList(query);
expandAll();
return false;
}
#Override
public boolean onQueryTextChange(String query) {
listAdapter.filterList(query);
expandAll();
return false;
}
});
search.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
listAdapter.filterList("");
expandAll();
return false;
}
});
But then I get these errors when I try to search:
12-20 13:20:19.449: E/CursorWindow(28747): Failed to read row 0, column -1 from a CursorWindow which has 96 rows, 4 columns.
12-20 13:20:19.449: D/AndroidRuntime(28747): Shutting down VM
12-20 13:20:19.449: W/dalvikvm(28747): threadid=1: thread exiting with uncaught exception (group=0x415c62a0)
12-20 13:20:19.499: E/AndroidRuntime(28747): FATAL EXCEPTION: main
12-20 13:20:19.499: E/AndroidRuntime(28747): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
What I'm doing wrong? Or is this because I'm only return 1 query (display names) instead of 2 (display names and e-mails) in runQuery?
Edit 2:
First of all I've changed all my database implementations to ContactsContract. This is become easier to maintain so that you don't have to write your own database implementation.
What I now have tried is to save my constraint in runQuery() of FilterQueryProvider, and then in getChildrenCursor run a query against that constraint. (as suggested by JRaymond)
private String mConstraint;
protected Cursor getChildrenCursor(Cursor groupCursor) {
// Given the group, we return a cursor for all the children within that
// group
int groupPos = groupCursor.getPosition();
int groupId = groupCursor.getInt(groupCursor
.getColumnIndex(ContactsContract.Groups._ID));
Log.d(DEBUG_TAG, "getChildrenCursor() for groupPos " + groupPos);
Log.d(DEBUG_TAG, "getChildrenCursor() for groupId " + groupId);
mGroupMap.put(groupId, groupPos);
Bundle b = new Bundle();
b.putString("constraint", mConstraint);
Loader loader = mActivity.getSupportLoaderManager().getLoader(groupId);
if (loader != null && !loader.isReset()) {
if (mConstraint == null || mConstraint.isEmpty()) {
// Normal query
mActivity.getSupportLoaderManager().restartLoader(groupId,
null, mActivity.mSpeakersLoaderCallback);
} else {
// Constrained query
mActivity.getSupportLoaderManager().restartLoader(groupId, b,
mActivity.mSpeakersLoaderCallback);
}
} else {
if (mConstraint == null || mConstraint.isEmpty()) {
// Normal query
mActivity.getSupportLoaderManager().initLoader(groupId, null,
mActivity.mSpeakersLoaderCallback);
} else {
// Constrained query
mActivity.getSupportLoaderManager().initLoader(groupId, b,
mActivity.mSpeakersLoaderCallback);
}
}
return null;
}
And here is the FilterQueryProvider:
private FilterQueryProvider filterQueryProvider = new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
// Load the group cursor here and assign mConstraint
mConstraint = constraint.toString();
Uri groupsUri = ContactsContract.Groups.CONTENT_SUMMARY_URI;
String selection = "((" + ContactsContract.Groups.TITLE
+ " NOTNULL) AND (" + ContactsContract.Groups.TITLE
+ " == 'Coworkers' ) OR (" + ContactsContract.Groups.TITLE
+ " == 'My Contacts' ))"; // Select only Coworkers
// (E-mail only) and My
// Contacts (Name only)
String sortOrder = ContactsContract.Groups.TITLE
+ " COLLATE LOCALIZED ASC";
return mActivity.getContentResolver().query(groupsUri,
MainActivity.GROUPS_SUMMARY_PROJECTION, selection, null,
sortOrder);
}
};
As you can see I've load the query of the groups in order to get the getChildrenCursor working. Only what for query should I run in MainActivity that I get from the bundle?
I've looked into your issue, and unfortunately I don't have time to replicate your setup. In generic terms, however, You should be able to save your constraint, and then in 'getChildrenCursor', run a query against that constraint:
Cursor getChildrenCursor(Cursor groupCursor) {
if (mConstraint == null || mConstraint.isEmpty()) {
// Normal query
} else {
// Constrained query
}
}
I'm not certain, but I'm pretty sure that getChildrenCursor() will get called in response to a change of the parent cursor when you return the cursor in the filterQueryProvider(). You then just manage the null/filled state of the constraint.
Details:
In your filterList function, instead of doing a complicated procedure, just call runQueryOnBackgroundThread(constraint);. This will automatically offload database work to the background. Save your constraint in your filterQueryProvider:
String s = '%' + constraint.toString() + '%';
mConstraint = s;
For the query, it just depends on what you're trying to get out of the database - a quick adjustment to the code you posted runs the query like so:
String selection = ContactsContract.CommonDataKinds.Email.DATA
+ " NOT LIKE ''";
if (constraint != null) {
selection += " AND " + ContactsContract.CommonDataKinds.Email.DATA + " LIKE ?";
}
cl = new CursorLoader(getApplicationContext(),
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
PROJECTION, selection, constraint, sortOrder);
The one thing I'm not too sure about is the auto expand thing you have going, My filter works but you need to collapse and open the list again to see the change.
What you should to do is extend FilterQueryProvider. This provides a runQuery() function that returns a new cursor of filtered results (likely accomplished with a database query).
In your CursorTreeAdapter adapter implementation you will then use the setFilterQueryProvider() method to provide it an instance of your FilterQueryProvider.
Finally, when you want to perform filtering you will call mAdapter.getFilter().filter("c").
However seeing as you are not actually using the SearchView autocomplete features and instead populating your own list, your chosen solution is quite a bit more complicated than it needs to be. Why don't you instead drop the Content Provider and CursorTreeAdapter and use a more simple in-memory scheme of lists or maps to back your adapter? Populate the in-memory data as required (can your entire dataset fit in memory?).

NullPointer cause in CustomListview

I am developing an application in which i have Implement CustomListview with one icon, raitingbar,checkbox. now everything is display fine but when i have insert one button it getting me nullpointer error.i know that nullpointer error cause in situation of not initialize the control... but i have tried so far and not understand what is the proble over here....
i have take one viewHold for declare all Row item..
package com.AppFavorits;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
class ViewHolder {
public ImageView imgvFavrowiconappicon;
public TextView txvxFavrowiconappname;
public RatingBar ratingBar1;
public CheckBox chkbxFavrowsel;
public Button btnFavrowOpen;
ViewHolder(View base) {
this.ratingBar1=(RatingBar)base.findViewById(R.id.ratingBar1);
this.ratingBar1.setFocusable(false);
this.txvxFavrowiconappname=(TextView)base.findViewById(R.id.txvxFavrowiconappname);
this.imgvFavrowiconappicon=(ImageView)base.findViewById(R.id.imgvFavrowiconappicon);
this.chkbxFavrowsel = (CheckBox)base.findViewById(R.id.chkbxFavrowsel);
this.btnFavrowOpen= (Button)base.findViewWithTag(R.id.btnFavrowOpen);
this.chkbxFavrowsel.setFocusable(false);
}
}
CustomAdapter
class RatingAdapter extends ArrayAdapter<RowModel> implements OnClickListener {
private ArrayList<Model> mlist;
boolean[] checkBoxState;
RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist) {
super(Favorites.this, R.layout.rowfavorites,
R.id.txvxFavrowiconappname, list);
checkBoxState = new boolean[list.size()];
this.mlist = mlist;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = super.getView(position, convertView, parent);
holder = (ViewHolder) row.getTag();
if (convertView == null) {
holder = new ViewHolder(row);
row.setTag(holder);
} else {
row = convertView;
((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
.get(position));
}
RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
Integer myPosition = (Integer) ratingBar.getTag();
RowModel model = getModel(myPosition);
model.rating = rating;
flFavRate[position] = rating;
// stored listitem rating in array
datasource.open();
datasource.UpdateRating(String.valueOf(rating), String.valueOf(position+1));
datasource.close();
}
};
holder.ratingBar1.setOnRatingBarChangeListener(l);
holder.chkbxFavrowsel
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) holder.chkbxFavrowsel
.getTag();
element.setSelected(buttonView.isChecked());
bSelected[position] = isChecked;
element.setsizeInc(sizeincrement);
// if (holder.chkbxFavrowsel.isChecked() ==
// isChecked) {
boolean bAlreadyfav = false;
String prefixlink = "https://play.google.com/store/apps/details?id=";
ShrdDatasource.open();
ArrayList<Comment> alDSPackagenm = ShrdDatasource.getAllPackage();
ArrayList<Comment> alDSRating = ShrdDatasource.getAllRating();
ArrayList<Comment> alDSID = ShrdDatasource.getID();
Log.i(TAG, "First Package cmp"+ alDSPackagenm);
ShrdDatasource.close();
int i = 0 ;
if (alDSPackagenm != null) {
for (i = 0; i < alDSPackagenm.size(); i++) {
if(alDSPackagenm.get(i).toString().equalsIgnoreCase(prefixlink + alPackagenm.get(position).toString() + "1x2optrue") || alDSPackagenm.get(i).toString().equalsIgnoreCase(prefixlink + alPackagenm.get(position).toString() + "1x2opfalse"))
{
bAlreadyfav = true;
break;
}
}
}
GolbPosition = position;
if (bAlreadyfav == true) {
Log.i(TAG, "Share positionis "+i);
Log.i(TAG, "Current positionis "+position);
Log.i(TAG,"ShrdDatasource Rating" + alDSRating.get(i).toString());
Log.i(TAG,"Favorite Rating" + String.valueOf(flFavRate[GolbPosition]));
if(alDSRating.get(i).toString().equalsIgnoreCase(String.valueOf(flFavRate[GolbPosition])))
{
Toast.makeText(getBaseContext(), " App already in Share list ",
Toast.LENGTH_LONG).show();
}
else
{
//alid
/*GolbPosition = position;
new GETFavTask().execute();*/
//UpdateRating
ShrdDatasource.open();
ShrdDatasource.UpdateRating(String.valueOf(flFavRate[GolbPosition]), alDSID.get(i).toString());
ShrdDatasource.close();
//Update Query fire
}
} else {
//Log.i(TAG, "First Package cmp"+ alDSPackagenm.get(position).toString());
Log.i(TAG, "Second Package cmp"+ alPackagenm.get(position).toString());
GolbPosition = position;
new GETFavTask().execute();
// add item in Database when user get select
}
}
});
RowModel model = getModel(position);
ViewHolder holder = (ViewHolder) row.getTag();
holder.ratingBar1.setTag(new Integer(position));
//holder.btnFavrowOpen.setTag(mlist.get(position));
holder.ratingBar1.setRating(model.rating);
holder.imgvFavrowiconappicon.setImageDrawable(drblAlIcon[position]);
holder.txvxFavrowiconappname.setText(alAppName.get(position)
.toString());
holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
holder.chkbxFavrowsel.setTag(mlist.get(position));
holder.btnFavrowOpen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// OpenApp(alPackagenm.get(position).toString());
}
});
try
{
if (alAppRating.get(position) == null | alAppRating.get(position).toString().equalsIgnoreCase("null")) {
holder.ratingBar1.setRating(0.0f);
} else {
holder.ratingBar1.setRating(Float.parseFloat(alAppRating
.get(position).toString()));
}
}catch (Exception e) {
e.printStackTrace();
}
//holder.ratingBar1.setRating(Float.parseFloat(alAppRating.get(position).toString()));
return (row);
}
Here you can see the button holder.btnFavrowOpen with onclicklistner i got null pointer at that line very much tied what happen here.... if i am remove button code in adapter it getting work fine...but when i write button code again it getting Error of null pointer
use
base.findViewById
and not
base.findViewWithTag

Issue with checkbox checked

i am use this for setting checkbox in listview i have follow all step as per given tutorial, but there are some critical issue with output, is that when i am select first checkbox and scroll down it will change selected item and automatically appear 3rd.
so i think there are something wrong with getview. so please help me out this ....
here is my code ::
package com.AppFavorits;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import android.app.ListActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RatingBar;
public class Favorites extends ListActivity {
protected static final String TAG = "Favorites";
CommentsDataSource datasource;
ListView lstFavrowlistv;
ArrayList alAppName;
float[] rate;
boolean[] bSelected;
ArrayList<Comment> alPackagenm;
Drawable[] alIcon;
ViewHolder holder;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
protected void onResume() {
super.onResume();
datasource = new CommentsDataSource(this);
datasource.open();
alAppName = datasource.getAllComments();
alPackagenm = datasource.getAllPackage();
Log.i(TAG, "values >>>" + alAppName);
Log.i(TAG, "values >>>" + alPackagenm);
int inc = 0;
alIcon = new Drawable[200];
for (int i = 0; i < alPackagenm.size(); i++) {
Log.i(TAG, "Appname >>>" + GetAllApp.lstpinfo.get(i).pname);
for (int j = 0; j < GetAllApp.lstpinfo.size(); j++) {
if (alPackagenm
.get(i)
.toString()
.equalsIgnoreCase(
GetAllApp.lstpinfo.get(j).pname.toString())) {
alIcon[inc] = GetAllApp.lstpinfo.get(j).icon;
Log.i("TAG", "sqlPackagename"
+ alPackagenm.get(i).toString());
Log.i("TAG", "from getAllapp"
+ GetAllApp.lstpinfo.get(j).pname.toString());
inc++;
}
}
}
ArrayList<RowModel> list = new ArrayList<RowModel>();
ArrayList<Model> Mlist = new ArrayList<Model>();
rate = new float[alAppName.size()];
bSelected = new boolean[alAppName.size()];
Iterator itr = alAppName.iterator();
String strVal = null;
while (itr.hasNext()) {
strVal += itr.next().toString() + ",";
}
int lastIndex = strVal.lastIndexOf(",");
strVal = strVal.substring(0, lastIndex);
System.out.println("Output String is : " + strVal);
String strAr[] = strVal.split(",");
for (int i = 0; i < strAr.length; i++) {
System.out.println("strAr[" + i + "] " + strAr[i]);
}
for (String s : strAr) {
list.add(new RowModel(s));
}
for (String s : strAr) {
Mlist.add(new Model(s));
}
setListAdapter(new RatingAdapter(list, Mlist));
datasource.close();
}
class RowModel {
String label;
float rating = 0.0f;
RowModel(String label) {
this.label = label;
}
public String toString() {
if (rating >= 3.0) {
return (label.toUpperCase());
}
return (label);
}
}
private RowModel getModel(int position) {
return (((RatingAdapter) getListAdapter()).getItem(position));
}
class RatingAdapter extends ArrayAdapter<RowModel> {
private ArrayList<Model> mlist;
RatingAdapter(ArrayList<RowModel> list, ArrayList<Model> mlist) {
super(Favorites.this, R.layout.outbox_list_item,
R.id.txvxFavrowiconappname, list);
this.mlist = mlist;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = super.getView(position, convertView, parent);
holder = (ViewHolder) row.getTag();
if (holder == null) {
holder = new ViewHolder(row);
row.setTag(holder);
RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
public void onRatingChanged(RatingBar ratingBar,
float rating, boolean fromTouch) {
Integer myPosition = (Integer) ratingBar.getTag();
RowModel model = getModel(myPosition);
model.rating = rating;
rate[position] = rating;
}
};
holder.chkbxFavrowsel
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
Model element = (Model) holder.chkbxFavrowsel
.getTag();
element.setSelected(buttonView.isChecked());
bSelected[position] = isChecked;
}
});
holder.chkbxFavrowsel.setTag(mlist.get(position));
holder.ratingBar1.setOnRatingBarChangeListener(l);
} else {
row = convertView;
((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
.get(position));
}
RowModel model = getModel(position);
ViewHolder holder = (ViewHolder) row.getTag();
holder.ratingBar1.setTag(new Integer(position));
holder.ratingBar1.setRating(model.rating);
holder.imgvFavrowiconappicon.setImageDrawable(alIcon[position]);
holder.txvxFavrowiconappname.setText(alAppName.get(position)
.toString());
holder.chkbxFavrowsel.setChecked(mlist.get(position).isSelected());
return (row);
}
}
}
I really don't understand your code exactly what you are trying to do. I haven't seen before this you are checking holder == null where as it should be convertView == null. If you have a scrolling issue you can check my blog post
holder = (ViewHolder) row.getTag();
if (holder == null) {
holder = new ViewHolder(row);
row.setTag(holder);
...
}
else {
row = convertView;
((ViewHolder) row.getTag()).chkbxFavrowsel.setTag(mlist
.get(position));
}
Use Below Code to your MainActivity.java file.
public class ListViewActivity extends Activity {
ListView mLstView1;
Button mBtn1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mLstView1 = (ListView) findViewById(R.id.mLstView1);
String[] lv_items = { "Dipak", "Rahul", "Hiren", "Nandlal", "Keyur",
"Kapil", "Dipak", "Rahul", "Hiren", "Nandlal", "Keyur",
"Kapil" };
;
mLstView1.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, lv_items));
mLstView1.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mBtn1 = (Button) findViewById(R.id.mBtn1);
mBtn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Using a List to hold the IDs, but could use an array.
List<Integer> checkedIDs = new ArrayList<Integer>();
// Get all of the items that have been clicked - either on or
// off
final SparseBooleanArray checkedItems = mLstView1
.getCheckedItemPositions();
for (int i = 0; i < checkedItems.size(); i++) {
// And this tells us the item status at the above position
final boolean isChecked = checkedItems.valueAt(i);
if (isChecked) {
// This tells us the item position we are looking at
final int position = checkedItems.keyAt(i);
// Put the value of the id in our list
checkedIDs.add(position);
System.out.println("Position is:- " + position);
}
}
}
});
}
}

Categories