我想用2个文本框来定制ListView,当我首先显示号码时,我可以显示其未接来电号码,已接或外拨号码,以及日期,我可以在ListView的第二行中显示日期和通话类型,但是当我尝试时显示数字会产生以下错误。

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);其中都打印呼叫类型和日期,但当我写
lviewAdapter = new ListViewAdapter(this, timeArray, timeArray);
它给出了ArrayIndexoutOfBoundException

ListView2Activity.java

import java.sql.Date;


导入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;
}
}

最佳答案

上面的代码运行良好,我只需将其复制并粘贴并运行此代码即可获得以下结果。

08-05 08:03