2013-03-25 66 views
0

首先非常感謝所有回答問題並提供洞察挑戰的專家。感謝您的工作。
現在,我是一個新手,剛開始使用Java和Android ....但我很喜歡它。 其次,
請原諒我的代碼。它是我的第一個Android應用程序...從13年的vb和vba開始:)而且它的大部分內容都是從用戶的問題修改過來的。Gridview使用自定義適配器不顯示所有結果

背景:
我有一個gridview,我想從通話記錄中顯示聯繫人數據(姓名和號碼)。
爲了消除重複的數字,我循環遍歷遊標並比較電話號碼,當然,通過CallLog.Calls.NUMBER +「ASC」對傳入的遊標數據進行排序;

我也創建了我自己的類(ContactObj),它包含聯繫人的姓名,號碼和ID,並將此類傳遞給ArrayList。最終我將這個ArrayList傳遞給一個自定義的適配器,它使用佈局充氣器來填充網格。

問題:
出於某種原因,程序運行正常,但前十個聯繫人一遍又一遍地重複。即。我的手機日誌上的總聯繫人數是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和對象佈局

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

txtContactName.setText(mItems.get(位置).getContactName());

這些語句應該超出if條件。另請檢查viewholder usage一次。

可能你會得到解決方案。

相關問題