java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3 in listview last item onclick event in android

拥有回忆 提交于 2020-01-03 05:58:10

问题


hello friends below is my code i m trying to show listitem in one listview with diffrent adapter so below is my code.

   public class ManagePropertyActivity extends FragmentActivity{


int getCommonID;



ArrayList<Rent_Master>mArrayListRent_Masters;
ArrayList<Expense_Master>mArrayListExpense_Masters;




ListView mListView;
View  mViewHeader;

RentAdpater mRenntAdpater; 
ExpenseAdpater mExpenseAdpater;

@SuppressLint("NewApi")

@Override
protected void onCreate(Bundle arg0) {

    super.onCreate(arg0);
    setContentView(R.layout.layout_manage_property);

    mArrayListRent_Masters=new ArrayList<Rent_Master>();

    mArrayListExpense_Masters=new ArrayList<Expense_Master>();

    mTextViewExpenseSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mStringGetExpenseAmount=mEditTextExpenseAmount.getText().toString().trim();
            mStringGetExpenseNote=mEditTextExpenseNotes.getText().toString().trim();
            if (mStringGetExpenseAmount.equalsIgnoreCase("")) {
                mAllMethods.ShowDialog(ManagePropertyActivity.this, "Validation","Enter amount", "Ok");
            }
            else {
                ContentValues mContentValues=new ContentValues();
                mContentValues.put("p_id",GetPropertId);
                mContentValues.put("e_amount", mStringGetExpenseAmount);
                mContentValues.put("e_note", mStringGetExpenseNote);
                mContentValues.put("e_date", mStringGetExpenseDate);
                mContentValues.put("e_time", mStringGetExpenseTime);
                mContentValues.put("e_status", mStringGetExpenseStauts);
                mContentValues.put("e_type", mStringGetExpenseType);
                mContentValues.put("e_freq_type", mStringGetExpenseFreq);
                mContentValues.put("e_status_type",mStringGetCurrentTabOption);
                mContentValues.put("e_rim_type",mStringGetExpenseRimType);
                int id=mDatabaseConnectionAPI.Insert("expense_master", mContentValues);
                if (id>0) {

                    mArrayListExpense_Masters=new ArrayList<Expense_Master>();
                    mArrayListExpense_Masters=mDatabaseConnectionAPI.getAllExpenseData(GetPropertId,mStringGetCurrentTabOption);
                    mExpenseAdpater=new ExpenseAdpater(ManagePropertyActivity.this, mArrayListExpense_Masters);
                    mListView.setAdapter(mExpenseAdpater);


                }
            }
        }
    });



    mTextViewRentSave.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            mStringGetRentAmount=mEditTextRentRentAmount.getText().toString().trim();
            mStringGetRentNote=mEditTextRentNote.getText().toString().trim();
            mStringGetRentPartialAMount=mEditTextRentPartialAMount.getText().toString().trim();
            mStringGetRentLateFee=mEditTextRentLateFee.getText().toString().trim();
            if (mStringGetRentAmount.equalsIgnoreCase("")) {
                mAllMethods.ShowDialog(ManagePropertyActivity.this, "Validation", "Enter rent amount", "OK");
            }
            else if (mStringGetRentAmountType.equalsIgnoreCase("Partial")) {
                if (mStringGetRentPartialAMount.equalsIgnoreCase("")) {
                    mAllMethods.ShowDialog(ManagePropertyActivity.this, "Validation", "Enter partial amount", "OK");

                }
                else if (Double.parseDouble(mStringGetRentPartialAMount)>Double.parseDouble(mStringGetRentAmount)) {
                    mAllMethods.ShowDialog(ManagePropertyActivity.this, "Validation", "Partial amount should be less than rent amount", "OK");
                }
                else {
                    ContentValues mContentValues=new ContentValues();
                    mContentValues.put("p_id", GetPropertId);
                    mContentValues.put("rent_amount", mStringGetRentAmount);
                    mContentValues.put("rent_note", mStringGetRentNote);
                    mContentValues.put("rent_date", mStringGetRentDate);
                    mContentValues.put("rent_time", mStringGetRentTime);
                    mContentValues.put("rent_amount_status", mStringGetCurrentTabOption);
                    mContentValues.put("rent_partial_amount", mStringGetRentPartialAMount);
                    mContentValues.put("rent_late_fee", mStringGetRentLateFee);
                    mContentValues.put("rent_status", mStringGetRentStatus);
                    mContentValues.put("rent_rim_date", mStringGetRentRimDate);
                    mContentValues.put("rent_rim_time",mStringGetRentRimTime);
                    mContentValues.put("rent_freq_type",mStringGetRentFreqType);
                    mContentValues.put("rent_rim_type",mStringGetRentRimType);
                    mContentValues.put("rent_type", mStringGetRentAmountType);
                    int id=mDatabaseConnectionAPI.Insert("rent_master", mContentValues);
                    if (id>0) {
                        mScrollViewAddRent.setVisibility(View.GONE);
                        mListView.setVisibility(View.VISIBLE);
                        mArrayListRent_Masters=new ArrayList<Rent_Master>();
                        mArrayListRent_Masters=mDatabaseConnectionAPI.getAllRentData(GetPropertId,mStringGetCurrentTabOption);
                        mRenntAdpater=new RentAdpater(ManagePropertyActivity.this, mArrayListRent_Masters);
                        mListView.setAdapter(mRenntAdpater);
                        mRenntAdpater.notifyDataSetChanged();
                        mEditTextRentRentAmount.setText("");
                        mEditTextRentNote.setText("");
                        mEditTextRentPartialAMount.setText("");
                        mEditTextRentLateFee.setText("");
                        mCheckBoxMarkComplete.setChecked(false);
                        mSpinnerRentAmountType.setSelection(0);
                    }
                }
            }
            else {
                ContentValues mContentValues=new ContentValues();
                mContentValues.put("p_id", GetPropertId);
                mContentValues.put("rent_amount", mStringGetRentAmount);
                mContentValues.put("rent_note", mStringGetRentNote);
                mContentValues.put("rent_date", mStringGetRentDate);
                mContentValues.put("rent_time", mStringGetRentTime);
                mContentValues.put("rent_amount_status", mStringGetCurrentTabOption);
                mContentValues.put("rent_partial_amount", mStringGetRentPartialAMount);
                mContentValues.put("rent_late_fee", mStringGetRentLateFee);
                mContentValues.put("rent_status", mStringGetRentStatus);
                mContentValues.put("rent_rim_date", mStringGetRentRimDate);
                mContentValues.put("rent_rim_time",mStringGetRentRimTime);
                mContentValues.put("rent_freq_type",mStringGetRentFreqType);
                mContentValues.put("rent_rim_type",mStringGetRentRimType);
                int id=mDatabaseConnectionAPI.Insert("rent_master", mContentValues);
                if (id>0) {
                    mScrollViewAddRent.setVisibility(View.GONE);
                    mListView.setVisibility(View.VISIBLE);
                    mArrayListRent_Masters=new ArrayList<Rent_Master>();
                    mArrayListRent_Masters=mDatabaseConnectionAPI.getAllRentData(GetPropertId,mStringGetCurrentTabOption);
                    mRenntAdpater=new RentAdpater(ManagePropertyActivity.this, mArrayListRent_Masters);
                    mListView.setAdapter(mRenntAdpater);
                    mEditTextRentRentAmount.setText("");
                    mEditTextRentNote.setText("");
                    mEditTextRentPartialAMount.setText("");
                    mEditTextRentLateFee.setText("");
                    mCheckBoxMarkComplete.setChecked(false);
                }
            }
        }
    });



    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,   int position, long id) {
            if (mStringCurrentMode.equalsIgnoreCase("Rent")) {

                getCommonID=    mArrayListRent_Masters.get(position).getRent_id();

                System.out.println("ID "+getCommonID);

            }
            else if (mStringCurrentMode.equalsIgnoreCase("Expense")) {

                getCommonID=mArrayListExpense_Masters.get(position).getE_id();
                System.out.println("ID "+getCommonID);
            }

        }
    });


mLinearLayoutRent.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        mRelativeLayoutHeaderDelete.setVisibility(View.GONE);
        mRelativeLayoutHeaderOther.setVisibility(View.VISIBLE);
        mScrollViewMainDetail.setVisibility(View.GONE);
        mListView.setVisibility(View.VISIBLE);
        mLinearLayoutOptionTab.setVisibility(View.VISIBLE);
        mRelativeLayoutMileageTrackerHeader.setVisibility(View.GONE);

        mArrayListRent_Masters=mDatabaseConnectionAPI.getAllRentData(GetPropertId,mStringGetCurrentTabOption);
        mRenntAdpater=new RentAdpater(ManagePropertyActivity.this, mArrayListRent_Masters);
        mListView.setAdapter(mRenntAdpater);
        mRenntAdpater.notifyDataSetChanged();
        mTextViewTitelSubHeader.setText("Rent");
        mStringCurrentMode="Rent";


    }
});
mLinearLayoutExpense.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        mScrollViewMainDetail.setVisibility(View.GONE);
        mListView.setVisibility(View.VISIBLE);
        mTextViewTitelSubHeader.setText("Expense");
        mStringCurrentMode="Expense";
        mLinearLayoutOptionTab.setVisibility(View.VISIBLE);
        mRelativeLayoutMileageTrackerHeader.setVisibility(View.GONE);

        mArrayListExpense_Masters=mDatabaseConnectionAPI.getAllExpenseData(GetPropertId,mStringGetCurrentTabOption);
        mExpenseAdpater=new ExpenseAdpater(ManagePropertyActivity.this,mArrayListExpense_Masters);
        mListView.setAdapter(mExpenseAdpater);
        mExpenseAdpater.notifyDataSetChanged();
        mRelativeLayoutHeaderDelete.setVisibility(View.GONE);
        mRelativeLayoutHeaderOther.setVisibility(View.VISIBLE);



    }
});

}


public class RentAdpater extends BaseAdapter
{

    Activity mActivity;
    ArrayList<Rent_Master>list;

    private LayoutInflater inflator;

    public RentAdpater(ManagePropertyActivity managePropertyActivity, ArrayList<Rent_Master> mArrayListRent_Masters) {

        mActivity=managePropertyActivity;
        list=mArrayListRent_Masters;
    }
    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position ;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflator = mActivity.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row_rent_expense, null);
        RelativeLayout mRelativeLayoutRentEx=(RelativeLayout)convertView.findViewById(R.id.rel_rent_expence_data_item);
        RelativeLayout mRelativeLayoutTask=(RelativeLayout)convertView.findViewById(R.id.rel_task_data_item);
        RelativeLayout mRelativeLayoutTenant=(RelativeLayout)convertView.findViewById(R.id.rel_tanant_data_item);
        RelativeLayout mRelativeLayoutDelete=(RelativeLayout)convertView.findViewById(R.id.rel_row_delete);
        RelativeLayout mRelativeLayoutFinanace=(RelativeLayout)convertView.findViewById(R.id.rel_finance);
        LinearLayout mLinearLayoutMLTable=(LinearLayout)convertView.findViewById(R.id.row_ll_mileage_tarck_table_title);



        mLinearLayoutMLTable.setVisibility(View.GONE);
        mRelativeLayoutRentEx.setVisibility(View.VISIBLE);
        mRelativeLayoutTask.setVisibility(View.GONE);
        mRelativeLayoutTenant.setVisibility(View.GONE);
        mRelativeLayoutDelete.setVisibility(View.GONE);
        mRelativeLayoutFinanace.setVisibility(View.GONE);

        TextView mTextViewDateTime=(TextView)convertView.findViewById(R.id.rent_date);
        TextView mTextViewRimDAteTime=(TextView)convertView.findViewById(R.id.rent_rim);

        TextView mTextViewOne=(TextView)convertView.findViewById(R.id.rent_txt_amount_title);
        TextView mTextViewTwo=(TextView)convertView.findViewById(R.id.rent_txt_rent_collect_title);
        TextView mTextViewThree=(TextView)convertView.findViewById(R.id.rent_txt_rent_note_title);
        TextView mTextViewFour=(TextView)convertView.findViewById(R.id.rent_txt_rent_freq_title);
        TextView mTextViewOneValue=(TextView)convertView.findViewById(R.id.rent_txt_amount_value);
        TextView mTextViewTwoValue=(TextView)convertView.findViewById(R.id.rent_txt_rent_collect_value);
        TextView mTextViewThreeValue=(TextView)convertView.findViewById(R.id.rent_txt_rent_note_value);
        TextView mTextViewFourValue=(TextView)convertView.findViewById(R.id.rent_txt_rent_freq_value);

        mTextViewFour.setText("Frequncy:" );
        mTextViewOne.setText("Rent Amount: " );
        mTextViewTwo.setText("Expense Collected: ");
        mTextViewThree.setText("Note: " );

        mTextViewDateTime.setText(list.get(position).getRent_date()+" "+list.get(position).getRent_time());
        mTextViewRimDAteTime.setText("Reminder : "+list.get(position).getRent_rim_type());
        mTextViewFourValue.setText(list.get(position).getRent_freq_type());
        mTextViewOneValue.setText(list.get(position).getRent_amount());
        if (list.get(position).getRent_partial_amount().equalsIgnoreCase("")) {
            mTextViewTwoValue.setText("0.00");
        }
        else {
            mTextViewTwoValue.setText(list.get(position).getRent_partial_amount());     
        }
        if (!list.get(position).getRent_late_fee().equalsIgnoreCase("")) {
            if (list.get(position).getRent_partial_amount().equalsIgnoreCase("")) {
                mTextViewTwoValue.setText("Late fees : " +list.get(position).getRent_late_fee());       
            }
            else {
                mTextViewTwoValue.setText(list.get(position).getRent_partial_amount()+ " + Late fees : " +list.get(position).getRent_late_fee());       

            }
        }

        if (list.get(position).getRent_note().equalsIgnoreCase("")) {
            mTextViewThreeValue.setText("");
            mTextViewThreeValue.setVisibility(View.GONE);
            mTextViewThree.setVisibility(View.GONE);
        }
        else {
            mTextViewThreeValue.setText(list.get(position).getRent_note());
        }


        return convertView;
    }

}

public class ExpenseAdpater extends BaseAdapter
{

    Activity mActivity;
    ArrayList<Expense_Master>list;
    public ExpenseAdpater(ManagePropertyActivity managePropertyActivity, ArrayList<Expense_Master> mArrayListExpense_Masters) {

        mActivity=managePropertyActivity;
        list=mArrayListExpense_Masters;
    }

    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflator = mActivity.getLayoutInflater();
        convertView = inflator.inflate(R.layout.row_rent_expense, null);
        RelativeLayout mRelativeLayoutRentEx=(RelativeLayout)convertView.findViewById(R.id.rel_rent_expence_data_item);
        RelativeLayout mRelativeLayoutTask=(RelativeLayout)convertView.findViewById(R.id.rel_task_data_item);
        RelativeLayout mRelativeLayoutTenant=(RelativeLayout)convertView.findViewById(R.id.rel_tanant_data_item);
        RelativeLayout mRelativeLayoutDelete=(RelativeLayout)convertView.findViewById(R.id.rel_row_delete);
        RelativeLayout mRelativeLayoutFinanace=(RelativeLayout)convertView.findViewById(R.id.rel_finance);
        LinearLayout mLinearLayoutMLTable=(LinearLayout)convertView.findViewById(R.id.row_ll_mileage_tarck_table_title);



        mLinearLayoutMLTable.setVisibility(View.GONE);
        mRelativeLayoutRentEx.setVisibility(View.VISIBLE);
        mRelativeLayoutTask.setVisibility(View.GONE);
        mRelativeLayoutTenant.setVisibility(View.GONE);
        mRelativeLayoutDelete.setVisibility(View.GONE);
        mRelativeLayoutFinanace.setVisibility(View.GONE);

        TextView mTextViewDate=(TextView)convertView.findViewById(R.id.rent_date);
        TextView mTextViewRim=(TextView)convertView.findViewById(R.id.rent_rim);
        TextView mTextViewOne=(TextView)convertView.findViewById(R.id.rent_txt_amount_title);
        TextView mTextViewTwo=(TextView)convertView.findViewById(R.id.rent_txt_rent_collect_title);
        TextView mTextViewThree=(TextView)convertView.findViewById(R.id.rent_txt_rent_note_title);
        TextView mTextViewFour=(TextView)convertView.findViewById(R.id.rent_txt_rent_freq_title);
        TextView mTextViewOneValue=(TextView)convertView.findViewById(R.id.rent_txt_amount_value);
        TextView mTextViewTwoValue=(TextView)convertView.findViewById(R.id.rent_txt_rent_collect_value);
        TextView mTextViewThreeValue=(TextView)convertView.findViewById(R.id.rent_txt_rent_note_value);
        TextView mTextViewFourValue=(TextView)convertView.findViewById(R.id.rent_txt_rent_freq_value);

        mTextViewFour.setText("Frequncy:");
        mTextViewOne.setText("Expense Type:");
        mTextViewTwo.setText("Expense Amount:");
        mTextViewThree.setText("Note:");

        if (list.get(position).getE_note().equalsIgnoreCase("")) {
            mTextViewThree.setVisibility(View.GONE);
            mTextViewThreeValue.setVisibility(View.GONE);
        }
        else {
            mTextViewThreeValue.setVisibility(View.VISIBLE);
            mTextViewThreeValue.setText(list.get(position).getE_note());
            mTextViewThree.setVisibility(View.VISIBLE);
        }
        mTextViewDate.setText(list.get(position).getE_date()+" "+list.get(position).getE_time());
        mTextViewRim.setText(list.get(position).getE_rim_type());
        mTextViewOneValue.setText(list.get(position).getE_type());
        mTextViewTwoValue.setText(list.get(position).getE_amount());
        mTextViewFourValue.setText(list.get(position).getE_freq_type());

        return convertView;
    }

}

}

when i run above code i get error message in listview last item any idea how can isolve it?

My logcat:

FATAL EXCEPTION: main
 java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at pkg.android.rootways.rental.ManagePropertyActivity$30.onItemClick(ManagePropertyActivity.java:1389)
at android.widget.AdapterView.performItemClick(AdapterView.java:292)
at android.widget.AbsListView.performItemClick(AbsListView.java:1099)
at android.widget.ListView.performItemClick(ListView.java:4758)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2556)
at android.widget.AbsListView$1.run(AbsListView.java:3223)
at android.os.Handler.handleCallback(Handler.java:608)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:4987)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

回答1:


The problem is, that, if You want to retrieve a value from arrayList, the count from arrayList starts at 0. If You use for example "position" from the ListView onItemClick method, this starts with count 1. So just give arrayList.get(position-1). For example:

getCommonID=    mArrayListRent_Masters.get(position-1).getRent_id(); 

and

getCommonID=mArrayListExpense_Masters.get(position-1).getE_id();

But to give You an exact solution, please tell us the line where this error causes.



来源:https://stackoverflow.com/questions/24261428/java-lang-indexoutofboundsexception-invalid-index-3-size-is-3-in-listview-last

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!