1

我已经实现了我的 ListView,我正在使用扩展 BaseAdapter 的自定义适配器填充列表,并使用基于 .NET 的服务,拉取和填充列表中的值。这工作正常,但是当我尝试在列表上连续​​执行一些操作(如删除条目、刷新、编辑..)时,我收到以下错误。这个错误不会一直出现。但是 15/20 次尝试中只有 1 次。

尝试解决,在网络上漫游,找出为什么没有发生..仍在搜索...

11-25 21:38:39.257: ERROR/AndroidRuntime(2169): Uncaught handler: thread main
                    exiting due to uncaught exception

11-25 21:38:39.325: ERROR/AndroidRuntime(2169): java.lang.IndexOutOfBoundsException:
                    Invalid location 0, size is 0

11-25 21:38:39.325: ERROR/AndroidRuntime(2169):at 
                     java.util.ArrayList.get(ArrayList.java:341)

ListActivity 类:STSBankDetailsListingScreenActivity.java

 public class STSBankDetailsListingScreenActivity extends ListActivity implements Runnable {

// All my Declarations are here....

   .....….....................
…....................
        …..................

private STSAddClientBankDetailsListingScreenAdapter mBankDetailsAdapter = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    …......................
    ….....................
    // Set the activity content from the XML resource
    setContentView(R.layout.XXX);

    …....................
    …....................

    // Set the typeface with the specified font
    ….......................................................
    …........................................................
    // Initializing the progress dialog to show the loading status
    mProgressDialog = ProgressDialog.show(this,"Loading....","Please wait");


    // Capture the widgets that are defined in the XML resource
    mBack = (Button) findViewById(R.id.backButton);

    …................................
    …...............................
    // Setting the custom fonts & Text
    …...............................
    …..............................

    // Create a Thread for long running operations
    mThread = new Thread(STSBankDetailsListingScreenActivity.this);
    mThread.start();

    // Instantiate the listview adapter class
    mBankDetailsAdapter = new  STSAddClientBankDetailsListingScreenAdapter(this);

    // Adding the listners for the items in the listview
    getListView().setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    mDetailsHolder = new ArrayList<String>();
    mDetailsHolder.add(STSURLConstants.AUTH_KEY);
    mDetailsHolder.add(mBankICode.get(position).toString());
    mDetailsHolder.add(mUserTypeHolder.get(position).toString());
    mDetailsHolder.add(mAccountNameHolder.get(position).toString());
    mDetailsHolder.add(mBankCodeHolder.get(position).toString());
    mDetailsHolder.add(mBankNameHolder.get(position).toString());
    mDetailsHolder.add(mBankAddressHolder.get(position).toString());
    mDetailsHolder.add(mOpeningBalanceHolder.get(position).toString());
    mDetailsHolder.add(mAmtInHolder.get(position).toString());

    }

    });
}

@Override
protected void onStart() {
    Log.i(TAG, "onStart");
    super.onStart();
}

@Override
protected void onRestart() {
    Log.i(TAG, "onRestart");
// Show the Progress Dialog 
mProgressDialog = ProgressDialog.show(STSBankDetailsListingScreenActivity.this,     "Loading...", "Please wait");
// Create a Thread for long running operations
    mThread = new Thread(STSBankDetailsListingScreenActivity.this);
    mThread.start();
    super.onRestart();
}

@Override
protected void onPause() {
    Log.i(TAG, "onPause");
    super.onPause();
}

@Override
protected void onResume() {
    Log.i(TAG, "onResume");
    //updateUI();
    super.onResume();
}

@Override
protected void onStop() {
    Log.i(TAG, "onStop");
    super.onStop();
}

@Override
protected void onDestroy() {
    Log.i(TAG, "onDestroy");
    if(mThread != null) {
        Thread dummyThread = mThread;
        mThread = null;
           // Post an interrupt request to this thread
        dummyThread.interrupt(); 
    }
    super.onDestroy();
}

@Override
public void run() {
    // TODO Auto-generated method stub
    try {
        // Clear the arraylist before adding up the entries
        mBankNameHolder.clear(); 
        ….............................

        // Instantiate the class 
        mWSConsumer = new STSWebServiceConsumer();
        // Call to fetch the bank details from the service
        mSTSSelectBankAccountList = mWSConsumer.SelectBankAccountDetails();

// Do the necessary actions to pull the values from the list
for(STSSelectBankAccountDetails s : mSTSSelectBankAccountList) {
// Populate the list....
mBankNameHolder.add(String.valueOf(s.getmBaBankName().toString()));
…..............................
…..............................
}
} else { Log.i(TAG, "Something went wrong !"); }
        } catch(Exception err) {
        err.printStackTrace();
    } 
    handler.sendEmptyMessage(0);  }

private void updateUI() {
Log.i(TAG, "updateUI");
runOnUiThread(new Runnable() {
    @Override
    public void run() {
    try {
    // Set the data behind the list view
 mBankDetailsAdapter = new  STSAddClientBankDetailsListingScreenAdapter(STSBankDetailsListingScreenActivity.this,           mBankNameHolder, mAccountNameHolder, mIsCheckedHolder);
    getListView().setAdapter(mBankDetailsAdapter);
    // Notifies the attached view that the underlying data has been changed
    mBankDetailsAdapter.notifyDataSetChanged();
    } catch(Exception err) {
        err.printStackTrace();      }
        }
        });
    }

public Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {

        switch (msg.what) {
        case 0:
            // Do Nothing...
            break;
        case 1:
            // Calls the run method of the runnable object
            mThread.run();
            break;
        default:
            break;
        }
        // Update the UI
        updateUI();

        // Dismiss the progress dialog once done
        if(mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            //mQuickAction.dismiss();
        }
    }
};

}

适配器类:STSAddClientBankDetailsListingScreenAdapter.java

public class STSAddClientBankDetailsListingScreenAdapter extends
    BaseAdapter {
// Declare all the needed
private Activity mActivity = null;
private List<String> mBankNameHolder = new ArrayList<String>();
private List<String> mAccountNameHolder = new ArrayList<String>();
private List<String> mIsChecked = new ArrayList<String>();

private static final String TAG = STSAddClientBankDetailsListingScreenAdapter.class.getSimpleName();
private static LayoutInflater mLayoutInflater = null; // This class is responsible for instantiating the layout XML file in to its corresponding view objects

public STSAddClientBankDetailsListingScreenAdapter(Context context) {
    // Obtains the layout interface from the given context
    mLayoutInflater = LayoutInflater.from(context);
}

public STSAddClientBankDetailsListingScreenAdapter(Activity activity, List<String> bankName, List<String> accountName, List<String> isChecked) {
    mActivity = activity;
    mBankNameHolder = bankName;
    mAccountNameHolder = accountName;
    mIsChecked = isChecked;
}
/* (non-Javadoc)
 * @see android.widget.Adapter#getCount()
 */
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mBankNameHolder.size();
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getItem(int)
 */
@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getItemId(int)
 */
@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

/* (non-Javadoc)
 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder viewHolder;
    if(convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.profile_client_bank_details_listing_item, null);
        viewHolder = new ViewHolder();
        // Capture the widgets defined in the XML resource
        viewHolder.mBankName = (TextView) convertView.findViewById(R.id.message01);
        viewHolder.mAccountName = (TextView) convertView.findViewById(R.id.message02);
        viewHolder.mChecked = (ImageView) convertView.findViewById(R.id.image01);
        convertView.setTag(viewHolder); // Set the tag associated with this view
     } else {
         viewHolder = (ViewHolder) convertView.getTag(); // Returns the view tag
         }
    // Bind the controls with the resource
    viewHolder.mBankName.setText(mBankNameHolder.get(position).toString());
    viewHolder.mAccountName.setText(mAccountNameHolder.get(position).toString());
    // Set the tick mark according to the values
    boolean checked = Boolean.parseBoolean(mIsChecked.get(position).toString());
    if(checked) {
        viewHolder.mChecked.setVisibility(View.VISIBLE);
    } else {
        viewHolder.mChecked.setVisibility(View.INVISIBLE);
    }
    return convertView;
}

static class ViewHolder {
    TextView mBankName, mAccountName;
    ImageView mChecked;
}

}

请高度赞赏任何形式的帮助。

再一次感谢你...

4

1 回答 1

0

我强烈感觉到您的适配器方法 - getCount() 在这种特殊情况下会给出错误的输出。我建议不要 mBankNameHolder从其他参数中获取大小,或者在每种情况下使用日志/调试检查这个值。

public int getCount() {
    // TODO Auto-generated method stub
    return mBankNameHolder.size();
}
于 2013-07-05T10:38:55.840 回答