まず、質問に答え、課題への洞察を提供してくれたすべての専門家に感謝します。あなたの仕事はありがたいです。
今、私は初心者で、JavaとAndroidを使い始めたばかりですが、大好きです。第二に、
私のコードを許してください。それは私の最初のAndroidアプリです...13年間のvbとvbaから移動します:)そしてその多くはstackoverflowに関するユーザーの質問から変更されています。
背景:
通話履歴の連絡先データ(名前と番号)を表示したいグリッドビューがあります。
重複する番号を排除するために、もちろんカーソルをループして電話番号を比較し、着信カーソルデータをCallLog.Calls.NUMBER+"ASC"で並べ替えます。
また、連絡先の名前、番号、IDを保持する独自のクラス(ContactObj)を作成し、このクラスをArrayListに渡します。最終的に、このArrayListをカスタムアダプターに渡します。カスタムアダプターは、レイアウトインフレーターを使用してグリッドにデータを入力します。
問題:
何らかの理由で、プログラムは正常に実行されますが、最初の10件の連絡が何度も繰り返されます。すなわち。私の電話ログの連絡先の総数は113個です。ただし、グリッドには最初の10回だけが表示され、合計で113回表示されます。
質問:
おそらくこれでの「古い手」は、私がどこで間違っているのかを私に指摘する可能性がありますか?グリッドビューにフィードするカスタムアダプタの実装と関係があると思います。
デバッグしていると、mChildrenCountの値が11に固定されていることに気付きました。これは、デザインモードのグリッドビューのセルの数です。何らかの理由で、この数に達すると、グリッドビューは再び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;
}
}
最後に、グリッドビューとレイアウト
<?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