6

I'm a newbie android.I have some problem about my mini app. You can see figure below: http://i481.photobucket.com/albums/rr175/viethungit/android.png

Firstly,row 1(layout 1) appear listview when click button add in row 1, row 2(layout 2) appear, and continue...click button add in row 1 ,layout 2 appear in listview.... I try search from Mr.Google but i don't find... Anybody could help me!

This is layout, and activity I don't know how to implement

add_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#EEEEEE"
    android:orientation="horizontal"
    android:padding="5dip">

    <!-- Image Item-->
    <ImageButton
            android:id="@+id/imgItem"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="2dp"
            android:contentDescription="@string/imgView"  />

    <!-- Name item  -->

    <EditText
        android:id="@+id/edtItem"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/imgItem"
        android:background="@drawable/bg"
        android:hint="@string/txtTitle"
        android:textSize="20dip" />   

    <!-- Button add -->
    <Button        
        android:id="@+id/btnAdd"
        android:background="@drawable/add"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>
    <!-- item -->
    <TextView
        android:id="@+id/txtSubtitle"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_toLeftOf="@+id/btnAdd"
        android:gravity="right|center_vertical"
        android:layout_centerVertical="true"
        android:textSize="20dip"
        android:text="@string/txtSubtitle" />

</RelativeLayout>


plus_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#EEEEEE"
    android:orientation="horizontal"
    android:padding="5dip" >

    <!-- Image Item -->
    <ImageView
        android:id="@+id/imgItem"
        android:src="@drawable/chomsao"
        android:layout_width="40dip"
        android:layout_height="40dip"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="2dp"
        android:contentDescription="@string/imgView" />

    <!-- Name Item -->
    <TextView
        android:id="@+id/edtItem"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/imgItem"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:text="@string/txtTitle"
        android:textSize="20dip" />

    <!-- Quantity Item -->
    <TextView 
        android:id="@+id/txtQtyItem"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/edtItem"
        android:layout_marginLeft="20dp"
        android:gravity="center_vertical"
        android:textSize="20dip"
        android:text="@string/QuantityItem"/>

    <!-- Button plus -->
    <Button
        android:id="@+id/btnPlus"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/plus" />

    <!-- Button minus -->
    <Button
        android:id="@+id/btnMinus"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_alignBaseline="@id/btnPlus"
        android:layout_toLeftOf="@+id/btnPlus"
        android:background="@drawable/minus" />

    <!-- Price Item -->
    <TextView
        android:id="@+id/txtSubtitle"
        android:layout_width="100dp"
        android:layout_height="40dp"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btnMinus"
        android:gravity="right|center_vertical"
        android:text="@string/txtSubtitle"
        android:textSize="20dip" />

</RelativeLayout>



activity_main.xml
        <RelativeLayout 
        android:id="@+id/relative1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/relative"
        android:layout_above="@id/relative2"
        android:background="#EEEEEE"
        android:layout_marginLeft="1.5dp"
        android:layout_marginRight="1.5dp"
        android:layout_marginTop="1.5dp">

        <ListView
            android:id="@+id/listSale"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>        
    </RelativeLayout>
3
  • 1
    can you post some code for the activity and layout? Commented Nov 16, 2012 at 7:45
  • don't get it.. :S a bit detailed explanation maybe? Commented Nov 16, 2012 at 7:51
  • you can see my figure with link i481.photobucket.com/albums/rr175/viethungit/android.png. I can not post image on stackoverflow. Commented Nov 16, 2012 at 7:56

2 Answers 2

10
public class AndroidCustomListViewActivity extends Activity {
    private ListView myList;
    private MyAdapter myAdapter;
    private ImageView myImage;
    public static  String upload= " ";
    public static String GalleryImage;
    public ArrayList<ListItem> myItems = new ArrayList<ListItem>();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listandimage);

        myList = (ListView) findViewById(R.id.MyList);
        myImage= (ImageView)findViewById(R.id.image1);

        myList.setItemsCanFocus(true);
        myAdapter = new MyAdapter();

        ListItem listItem = new ListItem();
        listItem.textdata="@";
        listItem.caption = "";
        myItems.add(listItem);

        myList.setAdapter(myAdapter);
        myAdapter.notifyDataSetChanged();


    }


    public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public MyAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        }

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

        public ListItem getItem(int position) {
            return myItems.get(position);
        }

        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 = mInflater.inflate(R.layout.item, null);
                holder.text=(TextView )convertView.findViewById(R.id.textView1); 
                holder.captionEditText = (EditText) convertView.findViewById(R.id.ItemCaption);

                holder.addOrDeleteButton = (Button) convertView.findViewById(R.id.buttonAdd);

                holder.captionEditText.setFocusable(true);
                holder.captionEditText.requestFocus();

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

            // Fill EditText with the value you have in data source
            // holder.captionEditText.setId(position);
            holder.text.setTag(position);
            holder.captionEditText.setTag(position);
            holder.captionEditText.setText(getItem(position).caption);

            holder.addOrDeleteButton.setTag(position);

            // / this updates tag of
            // the button view as we
            // scroll ///

            holder.addOrDeleteButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    int tag = (Integer) view.getTag();
                    if (tag != (myItems.size() - 1)) {
                        myItems.remove(tag);
                        Log.d("GCM", "Item removed from " + tag);
                        myAdapter.notifyDataSetChanged();
                    } else {
                        ListItem listItem = new ListItem();
                        listItem.textdata="@";
                        listItem.caption = "";
                        myItems.add(listItem);


                        /*
                         * Log.d("GCM", holder.captionEditText.getText()
                         * .toString()); myItems.get((Integer)
                         * view.getTag()).caption = holder.captionEditText
                         * .getText().toString();
                         */
                        myAdapter.notifyDataSetChanged();
                        myList.setSelection(myAdapter.getCount() - 1);
                        // holder.captionEditText.setFocusable(true);
                        // holder.captionEditText.requestFocus();

                    }
                }
            });

            holder.captionEditText.addTextChangedListener(new TextWatcher() {

                public void onTextChanged(CharSequence s, int start,
                        int before, int count) {
                    // TODO Auto-generated method stub

                }

                public void beforeTextChanged(CharSequence s, int start,
                        int count, int after) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable s) {
                    // if(position < myItems.size())
                    // getItem(position).caption = s.toString();

                    myItems.get((Integer) holder.captionEditText.getTag()).caption = holder.captionEditText
                            .getText().toString();

                }
            });

            if (position != (myItems.size() - 1)) {
                holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttarecloseicon);
            } else {
                holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttareaddicon);
                holder.text.setFocusable(true);
                holder.captionEditText.setFocusable(true);
                holder.text.requestFocus();
                holder.captionEditText.requestFocus();

            }

            return convertView;
        }

    }

    class ViewHolder {

        TextView text;
        EditText captionEditText;
        Button addOrDeleteButton;
    }

    class ListItem {
        String textdata;
        String caption;
    }
}

use this code edit it according to your need.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
     >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="15dp"
        android:layout_height="50dp"
        android:text=" @"
       />

    <EditText
        android:id="@+id/ItemCaption"
        android:layout_width="270dp"
        android:layout_height="50dp"
        android:layout_margin="3dip"
        android:imeOptions="actionDone|flagNoExtractUi"
        android:inputType="textNoSuggestions"
        android:singleLine="true" >
    </EditText>

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="75dp"
        android:layout_height="50dp"
        android:layout_margin="3dip"
        />

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#fff"
    android:orientation="horizontal" >

    <ListView
        android:id="@+id/MyList"
        android:layout_width="370dp"
        android:layout_height="160dp"
        android:layout_gravity="center|top"
        android:layout_marginLeft="150dp"
        android:descendantFocusability="beforeDescendants" 
             >
    </ListView>

    <ImageView
        android:id="@+id/image1"
        android:layout_width="80dp"
        android:layout_height="80dp"

         />

</LinearLayout>

mark the answer right if it was useful!!

Sign up to request clarification or add additional context in comments.

Comments

0

You can create Elements (like Buttons, Views, and so on) and add them to an existing View like:

myListView.addChild(CreatedButton);

1 Comment

I custom item listview with ArrayAdapter<>...it include button,textview...so on, but i want add item1 in listview and when click button add of item1, item2 appear in listview. You can see my figure above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.