firstly a big thanks to all the experts that answer questions and provide insight into challenges. Your work is appreciated.
Now, i'm a newbie and just started using java and Android....but i'm loving it.
secondly,
do forgive my code. its my very first Android app...moving from 13yrs of vb and vba :) and much of it is modified from user questions here on stackoverflow.
Background:
I have a gridview that i want to display contact data (name and number) from the Call Log.
In order to eliminate duplicate numbers, i loop through the cursor and compare phone numbers after of course, sorting the incoming cursor data by CallLog.Calls.NUMBER + " ASC";
i have also created my own class (ContactObj) that holds the name,number and ID of a contact and i pass this class to an ArrayList. eventually i pass this ArrayList to a custom adapter which uses layout inflater to populate the grid.
The issue:
For some reason, the program runs fine but the first ten contacts are repeated over and over. ie. the total contacts on my phone log are 113 unique. however the grid displays only the first 10 over and over for the total 113.
The question:
perhaps the "old hands" at this could point me on where i'm going wrong? i'm guessing is something to do with my implementation of the custom adapter that feeds the gridview.
as i debug, noticed that the value of mChildrenCount is fixed at 11 which is the count of the cells in the gridview in design mode. for some reason whenever this number is reached the gridview starts from 0 again and that repeats the data. it seems i'm missing some setting to allow the grid to go beyond the cells shown during design. ...any ideas anyone? Thanks.
here's the code for the main activity
public class CallLogActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
final Context myContext = CallLogActivity.this;
final CustomAdapter mAdapter;
ArrayList<ContactObj> arrToPassToGrid = new ArrayList<ContactObj>();
String strNameHolder = "";
String strCurrentName;
String strNumber;
String strCallDate;
String ID;
int i = 0;
int ComparisonResult;
// first find the grid
GridView callLogGrid = (GridView) findViewById(R.id.callLogGrid);
// next get the contents to display
Long yourDateMillis = System.currentTimeMillis()- (30 * 24 * 60 * 60 * ' `1000);
Time yourDate = new Time();
yourDate.set(yourDate);
String[] YourDateMillistring = {String.valueOf(yourDateMillis)};
String formattedDate = yourDate.format("%Y-%m-%d %H:%M:%S");
Time tempDate;
Cursor Tempcursor;
Cursor cursor;
cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
new String[]{CallLog.Calls._ID,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE},
null,
null,
CallLog.Calls.NUMBER + " ASC");
startManagingCursor(cursor);
// intialize nameholder ----will be used to remove duplicate names in
strNameHolder = "";
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
// place contents in variables for easier reading later on;
strCurrentName =
cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
strNumber = cursor.getString(
cursor.getColumnIndex(CallLog.Calls.NUMBER)).trim();
strCallDate = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
ID = cursor.getString(cursor.getColumnIndex(CallLog.Calls._ID));
if (strCurrentName == null && strNumber == null) {
ComparisonResult = 0;
} else {
ComparisonResult = strNameHolder
.compareToIgnoreCase(strNumber);
}
if (ComparisonResult != 0) {
ContactObj contList = new ContactObj();
contList.setIndex(i);
contList.setContactName(strCurrentName);
contList.setContactDialledNumber(strNumber);
contList.setContact_ID(ID);
contList.setCallDate(strCallDate);
arrToPassToGrid.add(i, contList);
i++;
}
strNameHolder = cursor.getString(
cursor.getColumnIndex(CallLog.Calls.NUMBER)).trim();
};
};
try {
// Collections.sort(arrToPassToGrid)
mAdapter = new CustomAdapter(this, arrToPassToGrid);
callLogGrid.setAdapter(mAdapter);
} catch (Exception e)
{
Log.d("Kush", e.getMessage());
e.printStackTrace();
}
}
This code is my custom adapter
public class CustomAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<ContactObj> mItems;
public CustomAdapter(Context c, ArrayList<ContactObj> items)
{
mContext = c;
mItems = items;
}
public int getCount()
{
return mItems.size();
}
public Object getItem(int position)
{
return mItems.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater li = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = li.inflate(R.layout.calllog_layout, null);
Log.d("Kush",String.valueOf(getCount()));
TextView txtContactName = (TextView)v.findViewById(R.id.txtContactName);
txtContactName.setText(mItems.get(position).getContactName() );
TextView txtNumber = (TextView)v.findViewById(R.id.txtContactNumber);
txtNumber.setText(mItems.get(position).getContactDialledNumber());
TextView txtDate = (TextView)v.findViewById(R.id.txtCallDate);
txtNumber.setText(String.valueOf(position) );
}
return v;
}
public static String getDate(long milliSeconds, String dateFormat)
{
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}
}
This is the object holding the contact details
public class ContactObj {
private String ContactName;
private String ContactDialledNumber;
private String Contact_ID;
private String CallDate;
public final String getCallDate()
{
return CallDate;
}
public final void setCallDate(String callDate)
{
CallDate = callDate;
}
private int index;
// @return the contactName
public final String getContactName()
{
return ContactName;
}
// @param contactName the contactName to set
public final void setContactName(String contactName)
{
ContactName = contactName;
}
//@return the contactDialledNumber
public final String getContactDialledNumber()
{
return ContactDialledNumber;
}
//@param contactDialledNumber the contactDialledNumber to set
public final void setContactDialledNumber(String contactDialledNumber)
{
ContactDialledNumber = contactDialledNumber;
}
//@return the contact_ID
public final String getContact_ID()
{
return Contact_ID;
}
// @param contact_ID the contact_ID to set
public final void setContact_ID(String contact_ID)
{
Contact_ID = contact_ID;
}
//@return the index
public final int getIndex()
{
return index;
}
//@param index the index to set
public final void setIndex(int index)
{
this.index = index;
}
}
Finally the gridview and layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
android:id="@+id/GridItem"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:scaleType="centerCrop" />
<TextView
android:gravity="center_horizontal"
android:id="@+id/txtContactName"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/contactName"
android:textColor="#000000" />
<TextView
android:gravity="center_horizontal"
android:id="@+id/txtContactNumber"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/contactNumber"
android:textColor="#000000" />
<TextView
android:gravity="center_horizontal"
android:id="@+id/txtCallDate"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/CallDate"
android:textColor="#000000" />
</LinearLayout>
and Gridview