首先非常感谢所有回答问题和洞察挑战的专家。你的工作表示赞赏
现在,我是一个新手,刚开始使用Java和Android ....但我很喜欢它。
其次,
请原谅我的代码。它是我的第一个Android应用程序...从13年的vb和vba移动:)其中大部分是在stackoverflow上从用户问题修改的。
背景:
我有一个gridview,我想在呼叫记录中显示联系人数据(姓名和号码)
为了消除重复的数字,我循环通过光标并比较电话号码,当然,按CallLog.Calls.NUMBER +“ASC”排序传入的光标数据;
我还创建了自己的类(ContactObj),它保存了联系人的姓名,号码和ID,并将此类传递给了ArrayList。最终我将此ArrayList传递给自定义适配器,该适配器使用布局inflater来填充网格。
问题:
出于某种原因,该程序运行良好,但前十个联系人反复重复。即。我手机日志中的联系人总数是113。但是,网格仅显示总数为113的前10个。
问题:
或许这个“老手”可以指出我哪里出错了?我猜测与我为Gridview提供的自定义适配器的实现有关。
在调试时,注意到mChildrenCount的值固定为11,这是设计模式下gridview中单元格的计数。由于某种原因,每当达到此数量时,gridview将再次从0开始并重复数据。似乎我错过了一些设置,允许网格超出设计期间显示的单元格。 ......有什么想法吗? 谢谢。
这是主要活动的代码
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();
}
}
此代码是我的自定义适配器
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());
}
}
这是保存联系方式的对象
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;
}
}
最后是gridview和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>
和Gridview
答案 0 :(得分:0)
txtContactName.setText(mItems.get(position).getContactName());
这些陈述应该在if条件之外。另请检查viewholder usage一次。
可能你会得到解决方案。