最初に番号を表示でき、別のテキストボックスで不在着信番号、受信または発信番号を日付とともに表示できる2つのテキストボックスを備えたカスタムListViewが必要です。ListViewの2行目に日付と通話タイプを表示できますが、試してみると番号を表示するには、次のエラーが発生します。
04-30 16:38:02.764: E/AndroidRuntime(7312): FATAL EXCEPTION: main
04-30 16:38:02.764: E/AndroidRuntime(7312): java.lang.ArrayIndexOutOfBoundsException
04-30 16:38:02.764: E/AndroidRuntime(7312): at com.custom.ListViewAdapter.getView (ListViewAdapter.java:64)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.AbsListView.obtainView(AbsListView.java:2203)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.ListView.measureHeightOfChildren(ListView.java:1204)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.ListView.onMeasure(ListView.java:1115)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.View.measure(View.java:8339)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.os.Handler.dispatchMessage(Handler.java:99)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.os.Looper.loop(Looper.java:123)
04-30 16:38:02.764: E/AndroidRuntime(7312): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-30 16:38:02.764: E/AndroidRuntime(7312): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 16:38:02.764: E/AndroidRuntime(7312): at java.lang.reflect.Method.invoke(Method.java:521)
04-30 16:38:02.764: E/AndroidRuntime(7312): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870)
04-30 16:38:02.764: E/AndroidRuntime(7312): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
04-30 16:38:02.764: E/AndroidRuntime(7312): at dalvik.system.NativeStart.main(Native Method)
ここで、ListをString[]
lviewAdapter = new ListViewAdapter(this, timeArray, timeArray);
に変換しましたが、両方のプリントが Typeとdateを呼び出す書き込みを行うと出力を取得できます が、書き込む
lviewAdapter = new ListViewAdapter(this, timeArray, timeArray);
とArrayIndexoutOfBoundExceptionが返されます
ListView2Activity.java
import java.sql.Date;
import java.util.ArrayList; java.util.List をインポートします。
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ListView2Activity extends Activity implements OnItemClickListener
{
/** Called when the activity is first created. */
ListView lview;
ListViewAdapter lviewAdapter;
List<String> dir;
List<String> num;
String[] numArray ;
String[] timeArray;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dir = new ArrayList<String>();
num = new ArrayList<String>();
Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER );
int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
Log.d("tag","call details");
while ( managedCursor.moveToNext() )
{
String phNumber = managedCursor.getString( number );
num.add(phNumber);
String callType = managedCursor.getString( type );
String callDate = managedCursor.getString( date );
Date callDayTime = new Date(Long.valueOf(callDate));
int dircode = Integer.parseInt( callType );
switch( dircode )
{
case CallLog.Calls.OUTGOING_TYPE:
dir.add("OUTGOING" + " , "+ callDayTime);
break;
case CallLog.Calls.INCOMING_TYPE:
dir.add("INCOMING"+ " , "+ callDayTime);
break;
case CallLog.Calls.MISSED_TYPE:
dir.add("MISSED"+ " , "+ callDayTime);
break;
}// switch end
Log.d("tag",phNumber +"," + dir );
} // while end
numArray = new String[num.size()];
for (int i = 0; i < num.size(); i++)
{
numArray[i] = num.get(i);
// Log.i("tag", "in array :" + num[i]);
} // for end
timeArray = new String[dir.size()];
for (int j = 0; j < dir.size(); j++) {
timeArray[j] = dir.get(j);
} // for end
managedCursor.close();
lview = (ListView) findViewById(R.id.listView2);
lviewAdapter = new ListViewAdapter(this, numArray, timeArray);
System.out.println("adapter => "+lviewAdapter.getCount());
lview.setAdapter(lviewAdapter);
lview.setOnItemClickListener(this);
} // create end
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
{
// TODO Auto-generated method stub
Toast.makeText(this,"Title => "+numArray[position]+"=> n Description"+timeArray[position], Toast.LENGTH_SHORT).show();
}}
ListViewAdapter.java
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListViewAdapter extends BaseAdapter
{
Activity context;
String title[];
String description[];
public ListViewAdapter(Activity context, String[] title, String[] description) {
super();
this.context = context;
this.title = title;
this.description = description;
}
public int getCount() {
// TODO Auto-generated method stub
return title.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
private class ViewHolder {
TextView txtViewTitle;
TextView txtViewDescription;
}
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
ViewHolder holder;
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null)
{
convertView = inflater.inflate(R.layout.listitem_row, null);
holder = new ViewHolder();
holder.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
holder.txtViewDescription = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtViewTitle.setText(title[position]);
holder.txtViewDescription.setText(description[position]);
return convertView;
}
}