简体   繁体   中英

How to Add RelativeLayout to ListView Dynamically

I'm just beginning with android and have not for the life of me been able to figure this out. Thanks in advance to everyone!

I have a list view within my alert dialogue. I want each item of this listview (id=dialoguelist) to be a relative layout which is in item.xml layout file (as shown below):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/receipt_items">    
    <EditText
        android:id="@+id/Item"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"          
        android:hint="@string/Item" 
        />

    <EditText
        android:id="@+id/Quantity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_alignParentLeft="true"
        android:hint="@string/Quantity" 
        android:layout_below="@id/Item"/>

    <EditText
        android:id="@+id/Cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_toRightOf="@id/Quantity"
        android:hint="@string/Cost" 
        android:layout_below="@id/Item"/>

</RelativeLayout>

I need this relative layout to be added dynamically when the NeutralButton in the alert dialog is pressed. I have tried using an arrayadapter but don't know how to implement it when the item I want to add is not a String.

I have also included (below) a portion of my main_activity.java file which opens up the alert dialogue:

 // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.add_receipt_custom_alert, null))
           .setTitle("New Receipt")
    // Add action buttons
           .setPositiveButton("Save", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) { 
            // continue with delete
          }
        })
        .setNeutralButton("Add Item", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) { 




       //////What should I do here to add the relative layout to the list dynamically?  



          }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int which) { 
            // do nothing
          }
        })
        .show();

In the area marked, what type of adapter would I need to make this happen and how would I make that work? Thanks bunches!

try using BaseAdapter instead ArrayAdapter.

public class MyAdapter extends BaseAdapter {


    public MyAdapter ()
    {


    }
    @Override
    public int getCount() {

        return 0;
    }

    @Override
    public Object getItem(int arg0) {
        return getItem(arg0);
    }

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

    @Override
    public View getView(final int id, View view, final ViewGroup parent) {


        if(view ==null)
        {

            LayoutInflater inflater=LayoutInflater.from(parent.getContext());
            view=inflater.inflate(R.layout.item, parent, false);

        }





        return view;
    }

}

If you have custom view that needs to be added to your list view, use custom adapter instead of ArrayAdapter. I recommend you to read a complete tutorial to see how it's getting done.

Here you can see a ListView tutorial from Vogella. For custom adapter, see the section named "3. Custom adapter implementations"

The Android developer guides say you should implement a custom array adapter that overrides getView(). Details here:

List view guide

Adapter views guide

Adapter views are one of the difficult topics in android and you may have to read these docs several times before light bulbs go off. There's no shortcut unfortunately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM