0

我正在尝试使用两个单独的项目实现我自己的自定义列表视图,该部分的标题,然后是每个标题下的可检查项目。

我想要做的是在edittext中输入文本,然后遍历可检查元素的列表,找到与输入匹配的元素并设置该行的复选框。

我也在使用 fragmentpageradapter 来管理两个滑动片段。

所以基本上我需要知道能够更改列表项的选中状态的最佳方法是什么,但不仅来自 onitemcheckedlistener,而且还来自我在文本框中输入的内容。

public class RetrieveListAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<ScanInfo> list = new ArrayList<ScanInfo>();
private static LayoutInflater inflater = null;
private TreeSet mSeparatorsSet = new TreeSet();

public RetrieveListAdapter(Activity a) {
    activity = a;
    //list = si;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    return list.size();
}

public void addItem(final ScanInfo item) {
    list.add(item);
    notifyDataSetChanged();
}

public void addSeparatorItem(final ScanInfo item) {
    list.add(item);
    mSeparatorsSet.add(list.size() - 1);
    // save separator position
    //mSeparatorsSet.add(mData.size() - 1);
    notifyDataSetChanged();
}

public ScanInfo getItem(int position) {
    return list.get(position);
}

@Override
public int getItemViewType(int position) {
    return mSeparatorsSet.contains(position) ? 0 : 1;
}

@Override
public int getViewTypeCount() {
    return 2;
}

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

public void checkItem(String tracking) {
    for (int i = 0; i < list.size(); i++) {
        if (getItemViewType(i) == 1) {
            if (tracking.equals(getItem(i).getTracking())) {
                RetrievePackage.checkedItems[i] = true;
            }
        }
    }
}

public View getView(final int position, View convertView, ViewGroup parent) {
    int type = getItemViewType(position);
    ViewHolder holder = null;

    if (convertView == null) {
        holder = new ViewHolder();
        switch (type) {
            case 0:
                convertView = inflater.inflate(R.layout.single_list_item, null);
                holder.textView = (TextView) convertView.findViewById(R.id.text1);
                holder.textView.setText(list.get(position).getLocation());

                break;
            case 1:
                convertView = inflater.inflate(R.layout.retrieve_package_item, null);
                holder.textView = (TextView) convertView.findViewById(R.id.trackingListItem);
                holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
                if (list.get(position).getShortDisplayTracking().equals("")) {
                    holder.textView.setText(list.get(position).getTracking() + " [" + list.get(position).getCarrier() + "]");
                } else {
                    holder.textView.setText(list.get(position).getShortDisplayTracking() + " [" + list.get(position).getCarrier() + "]");
                }

                holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        RetrievePackage.checkedItems[position] = isChecked;
                    }
                });
                holder.checkBox.setChecked(RetrievePackage.checkedItems[position]);

                break;
        }
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    switch (type) {
        case 0:
            holder.textView.setText(list.get(position).getLocation());
            break;
        case 1:
            if (list.get(position).getShortDisplayTracking().equals("")) {
                holder.textView.setText(list.get(position).getTracking() + " [" + list.get(position).getCarrier() + "]");
            } else {
                holder.textView.setText(list.get(position).getShortDisplayTracking() + " [" + list.get(position).getCarrier() + "]");
            }
            break;
    }
    return convertView;
}

public static class ViewHolder {

    public TextView textView;
    public CheckBox checkBox;
}

}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View V = inflater.inflate(R.layout.retrieve_packages, null);
    submitRetrieve = (Button) V.findViewById(R.id.retrievePackages);
    tracking = (EditText) V.findViewById(R.id.scanTrackingRet);
    lv = (ListView) V.findViewById(R.id.retrieveList);

    submitRetrieve.setEnabled(false);

    activity = getActivity();
    context = getActivity().getApplicationContext();

    adapter = new RetrieveListAdapter(activity);


    return V;

}

private static void initializePackageScan() {

    checkedItems = new boolean[adapter.getCount()];

    tracking.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_NEXT || actionId == EditorInfo.IME_ACTION_DONE) {
                String number = tracking.getText().toString();
                adapter.checkItem(number);
            }
            return true;
        }
    });
}
4

1 回答 1

1

看起来您的想法是正确的;将该项目设置为“已选中”,然后调用 notifyDataSetChanged(),就像您添加了一个项目一样。

于 2013-05-29T18:32:27.510 回答