1

最初に番号を表示でき、別のテキストボックスで不在着信番号、受信または発信番号を日付とともに表示できる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)

ここで、ListString[]
lviewAdapter = new ListViewAdapter(this, timeArray, timeArray);に変換しましたが、両方のプリントが Typedateを呼び出す書き込みを行うと出力を取得できます が、書き込む
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;
}
}
4

3 に答える 3

3

上記のコードはうまく機能しています。コピーして貼り付け、このコードを実行すると、次の結果が得られます。

ここに画像の説明を入力してください

于 2012-04-30T11:39:47.863 に答える
0

これはランダムな推測ですが、配列の 1 つが異なるサイズであると考えています。これにより、title.length > description.length を返す場合に表示されるエラーが発生します。

存在するビューの数を返す getCount() が呼び出されたときの推論は問題ありません。title.length = 5 と仮定します。ListView は 5 つのビューを生成する必要があるため、位置 =1...5 に対して getView を呼び出します。たとえば、description.length = 3 の場合、getView() で呼び出し時に position = 4 で例外が発生します。

holder.txtViewDescription.setText(description[position]);
于 2012-04-30T11:30:03.017 に答える
0
lviewAdapter = new ListViewAdapter(this, numArray, timeArray);
System.out.println("adapter => "+lviewAdapter.getCount());
lview.setAdapter(lviewAdapter);
lview.setOnItemClickListener(this);

上記のコードを置き換えます

if(timeArray.length > 0 )
{
   lviewAdapter = new ListViewAdapter(this, numArray, timeArray);
   System.out.println("adapter => "+lviewAdapter.getCount());
   lview.setAdapter(lviewAdapter);
   lview.setOnItemClickListener(this);
}
于 2012-04-30T11:31:25.763 に答える