-2

因此,当使用操作中的下拉菜单在片段之间切换时,我遇到了重复项出现在列表视图中的问题,我放置了一个 clear(); 解决了问题的语句,但它下面的 convertView.setOnClickListener 崩溃了。如果我删除 clear(); 声明然后一切正常,但我在列表视图中得到重复的项目。我正在使用 ActionBarSherlock,这段代码取自http://www.androidbegin.com/tutorial/implementing-actionbarsherlock-search-collapsible-view-in-android/ 任何帮助都将在试图弄清楚一切如何一起工作。如果您需要任何其他代码,请告诉我。

public class ListViewAdapter extends BaseAdapter
{
// Declare Variables
Context mContext;
LayoutInflater inflater;
private List<ErrorCode> errorcodelist = null;
private ArrayList<ErrorCode> arraylist;

public ListViewAdapter(Context context, List<ErrorCode> errorcodelist) 
{
    mContext = context;
    this.errorcodelist = errorcodelist;
    inflater = LayoutInflater.from(mContext);
    this.arraylist = new ArrayList<ErrorCode>();
    this.arraylist.addAll(errorcodelist);
}

public class ViewHolder 
{
    TextView errorName;
    TextView codeValue;
    TextView description;
    TextView solution;
}

@Override
public int getCount() 
{
    return errorcodelist.size();
}

@Override
public ErrorCode getItem(int position) 
{
    return errorcodelist.get(position);
}

@Override
public long getItemId(int position) 
{
    return position;
} 

public View getView(final int position, View convertView, ViewGroup parent) 
{
    final ViewHolder holder;
    if (convertView == null) 
    {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.listview_item, null);
        // Locate the TextViews in listview_item.xml
        holder.errorName = (TextView) convertView.findViewById(R.id.errorName);
        holder.codeValue = (TextView) convertView.findViewById(R.id.codeValue);
        holder.description = (TextView) convertView.findViewById(R.id.description);
        holder.solution = (TextView) convertView.findViewById(R.id.solution);
        convertView.setTag(holder);
    }
    else 
    {

        holder = (ViewHolder) convertView.getTag();
    }
    // Set the results into TextViews
    holder.errorName.setText(errorcodelist.get(position).getErrorName());
    holder.codeValue.setText(errorcodelist.get(position).getCodeValue());
    holder.description.setText(errorcodelist.get(position).getDescription());
    holder.solution.setText(errorcodelist.get(position).getSolution());
    errorcodelist.clear();

    // Listen for ListView Item Click
    convertView.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View arg0) 
        {
            // Send single item click data to SingleItemView Class
            Intent intent = new Intent(mContext, SingleItemView.class);
            // Pass all data errorName
            intent.putExtra("errorName",(errorcodelist.get(position).getErrorName()));
            // Pass all data errorCode
            intent.putExtra("codeValue",(errorcodelist.get(position).getCodeValue()));
            // Pass all data description
            intent.putExtra("description",(errorcodelist.get(position).getDescription()));
            // Pass all data solution
            intent.putExtra("solution",(errorcodelist.get(position).getSolution()));
            // Start SingleItemView Class
            mContext.startActivity(intent);
        }

    });
    return convertView;
}

// Filter Class
public void filter(String charText) 
{
    charText = charText.toLowerCase(Locale.getDefault());
    errorcodelist.clear();
    if (charText.length() == 0) 
    {
        errorcodelist.addAll(arraylist);
    }
    else
    {
        for (ErrorCode ec : arraylist)
        {
            // Search filter code
            if (ec.getErrorName().toLowerCase(Locale.getDefault()).contains(charText))
            {
                errorcodelist.add(ec);
            }
            if (ec.getCodeValue().toLowerCase(Locale.getDefault()).contains(charText))
            {
                errorcodelist.add(ec);
            }
        }

    }       
    notifyDataSetChanged();
}

}

这是 convertView.setOnClickListener 崩溃时的 logcat:

10-07 15:09:26.872: E/AndroidRuntime(21500): FATAL EXCEPTION: main
10-07 15:09:26.872: E/AndroidRuntime(21500): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
10-07 15:09:26.872: E/AndroidRuntime(21500):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at java.util.ArrayList.get(ArrayList.java:304)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at com.testapplication.test2v5.ListViewAdapter$1.onClick(ListViewAdapter.java:93)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at android.view.View.performClick(View.java:4204)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at android.view.View$PerformClick.run(View.java:17355)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at android.os.Handler.handleCallback(Handler.java:725)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at android.os.Looper.loop(Looper.java:137)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at android.app.ActivityThread.main(ActivityThread.java:5041)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at java.lang.reflect.Method.invokeNative(Native Method)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at java.lang.reflect.Method.invoke(Method.java:511)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-07 15:09:26.872: E/AndroidRuntime(21500):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

您不能在自己的适配器方法中更改适配器正在迭代的列表,因为您正在更改位置。最糟糕的是清除该列表。这解释了新的错误。因此,删除 getView 方法中的 clear() 。

重复的项目可能来自您的过滤器,因为如果它匹配错误和代码,它可以添加一个项目两次。改成:

         // Search filter code
        if (ec.getErrorName().toLowerCase(Locale.getDefault()).contains(charText))
        {
            errorcodelist.add(ec);
        }
        else if (ec.getCodeValue().toLowerCase(Locale.getDefault()).contains(charText))
        {
            errorcodelist.add(ec);
        }
于 2013-10-07T15:35:37.037 回答