0

首先非常感谢所有回答问题并提供对挑战的洞察力的专家。您的工作受到赞赏。
现在,我是一个新手,刚开始使用 java 和 Android ......但我很喜欢它。其次,
请原谅我的代码。它是我的第一个 Android 应用程序...从 13 年的 vb 和 vba 发展而来 :) 并且其中大部分是根据 stackoverflow 上的用户问题修改的。

背景:
我有一个网格视图,我想从通话记录中显示联系人数据(姓名和号码)。
为了消除重复号码,我当然会遍历光标并比较电话号码,然后按 CallLog.Calls.NUMBER + "ASC" 对传入的光标数据进行排序;

我还创建了自己的类(ContactObj),其中包含联系人的姓名、号码和 ID,并且我将此类传递给 ArrayList。最终我将这个 ArrayList 传递给一个自定义适配器,该适配器使用布局充气器来填充网格。

问题:
由于某种原因,程序运行良好,但前十个联系人一遍又一遍地重复。IE。我的电话日志上的联系人总数是 113 个唯一的。但是,网格仅反复显示前 10 个,总共 113 个。

问题:
也许这方面的“老手”可以指出我哪里出错了?我猜这与我实现为 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和布局

    <?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>

和网格视图

4

1 回答 1

0

txtContactName.setText(mItems.get(position).getContactName());

这些语句应该在 if 条件之外。还要检查一次查看器的使用情况

可能你会得到解决方案。

于 2013-03-26T10:40:47.460 回答