Aaron Smentkowski
Aaron Smentkowski

Reputation: 278

Android listview with checkbox make selected based on input from edittext

I am trying to implement my own custom listview with two separate items, a header for the section and then checkable items under each header.

What I want to be able to do is type text into the edittext and then loop over the list of checkable elements and find which one matches the input and set the checkbox of that row.

I am using a fragmentpageradapter also to manage the two sliding fragments.

So basically I need to know what the best way would be to be able to change the checked state of the list item, but not only from the onitemcheckedlistener, but also from just me typing something into a text box.

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;
        }
    });
}

Upvotes: 0

Views: 596

Answers (1)

Sector95
Sector95

Reputation: 641

It looks like you have the right idea; set the item to "checked" and then call notifyDataSetChanged(), just like if you had added an item.

Upvotes: 1

Related Questions