0

I'm using a custom adapter to set up my ListView for an activity (that is reached from the main activity). I'm trying to add a button (and an EditText later) dynamically considering I'm not using a defined layout file (just a layout for each row of the ListView). Can't seem to get it to work (last 5 lines). Here is my activity code:

public class SubSelectActivity extends ListActivity {
private List<Subs> list;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ArrayAdapter<Subs> adapter = new DialogAdapter(this, list);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(SubSelectActivity.this, "TEST", Toast.LENGTH_SHORT).show();
            CheckBox checkBox = (CheckBox) view.findViewById(R.id.check);
            checkBox.toggle();
            if (checkBox.isChecked()) {
                imageTF.add(images.get(position));
            } else {
                imageTF.remove(images.get(position));
            }

        }
    });

    Button myButton = new Button(this);
    myButton.setText("Push Me");
    LinearLayout ll = new LinearLayout(this);
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    ll.addView(myButton, lp);
}

And here is my adapter set up:

public class DialogAdapter extends ArrayAdapter<SubSelectActivity.Subs> {
private final List<SubSelectActivity.Subs> list;
private final Activity context;

static class ViewHolder {
    protected TextView name;
    protected CheckBox checkBox;
}

public DialogAdapter(Activity context, List<SubSelectActivity.Subs> list) {
    super(context, R.layout.row, list);
    this.context = context;
    this.list = list;
}

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

    View view = null;
    // reuse views
    if (convertView == null) {
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(R.layout.row, null);
        // configure view holder
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.name = (TextView) view.findViewById(R.id.text1);
        viewHolder.checkBox = (CheckBox) view.findViewById(R.id.check);
        view.setTag(viewHolder);
    } else {
        view = convertView;
    }

    final ViewHolder holder = (ViewHolder) view.getTag();
    holder.name.setText(list.get(position).getName());
    holder.checkBox.setChecked(list.get(position).IsChecked());
    return view;
    }
}

Can anyone help me figure out how to add widgets below my ListView?

EDIT: My row.xml is of type CheckableRelativeLayout:

public class CheckableRelativeLayout extends RelativeLayout implements
    Checkable {

private boolean isChecked;
private List<Checkable> checkableViews;

public CheckableRelativeLayout(Context context, AttributeSet attrs,
                               int defStyle) {
    super(context, attrs, defStyle);
    initialise(attrs);
}

public CheckableRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialise(attrs);
}

public CheckableRelativeLayout(Context context, int checkableId) {
    super(context);
    initialise(null);
}

/*
  * @see android.widget.Checkable#isChecked()
  */
public boolean isChecked() {
    return isChecked;
}

/*
  * @see android.widget.Checkable#setChecked(boolean)
  */
public void setChecked(boolean checked) {
    if (isChecked != checked) {
        isChecked = checked;
        refreshDrawableState();
    }
}

/*
  * @see android.widget.Checkable#toggle()
  */
public void toggle() {
    this.isChecked = !this.isChecked;
    for (Checkable c : checkableViews) {
        c.toggle();
    }
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    final int childCount = this.getChildCount();
    for (int i = 0; i < childCount; ++i) {
        findCheckableChildren(this.getChildAt(i));
    }
}

/**
 * Read the custom XML attributes
 */
private void initialise(AttributeSet attrs) {
    this.isChecked = false;
    this.checkableViews = new ArrayList<Checkable>(5);
}

/**
 * Add to our checkable list all the children of the view that implement the
 * interface Checkable
 */
private void findCheckableChildren(View v) {
    if (v instanceof Checkable) {
        this.checkableViews.add((Checkable) v);
    }

    if (v instanceof ViewGroup) {
        final ViewGroup vg = (ViewGroup) v;
        final int childCount = vg.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(vg.getChildAt(i));
        }
    }
}
}

SOLUTION:

This link was the solution that worked for me Add buttons to a listactivity

Community
  • 1
  • 1
blurbs78
  • 23
  • 10

2 Answers2

0
    //I **** xml
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {

            convertView = new LinearLayout(context);

            TextView tv = new TextView(context);
            tv.setText(list.get(position).getName());

            CheckBox ch =new CheckBox(context);
            ch.setChecked(list.get(position).IsChecked());      

            ((LinearLayout)convertView).addView(tv);
            ((LinearLayout)convertView).addView(ch);
        } 
        return convertView ;
    }
Stav Bodik
  • 2,018
  • 3
  • 18
  • 25
  • "list" is just a List<>, and I can't do addView on that. I'm not looking to have a button on each row of my ListView, just underneath the whole ListView at the bottom of the screen. – blurbs78 Jun 30 '16 at 00:05
  • Please look i understood something wrong , i edit my answer . – Stav Bodik Jun 30 '16 at 00:07
  • Viewholder was just a class to hold my textView and checkBox (see top of adapter code). I added my row.xml layout type code. Have a look, it won't let me use "llView = convertView" due to incompatible types (View and CheckableRelativeLayout) – blurbs78 Jun 30 '16 at 00:26
  • row.xml is not looks like xml its more like .java , convertView is type of View , llView(LinearLayout extends ViewGroup and Viewgroup extends View) so there is not way convertView = new LinearLayout are incompatible types – Stav Bodik Jun 30 '16 at 00:34
0

I personally consider you should use an Xml Layout for the ListView, you can add the button and the EditText Hidden (visibility="GONE"), then show it when you need.

BUT, From my personal experience and other posts i have read, it is not recommended to use an EditText in an ListView, EditText Fails to retain focus and thus, Text is not editable when displayed on a ListView.

So my recommendation for you is to use a LinearLayout and add your custom Views dynamically to it, its a little more of work but you'll have less trouble.

Daniel Andujar
  • 1,184
  • 1
  • 11
  • 22