eng.m.a
eng.m.a

Reputation: 479

get data of edittexes in a listview

I have a list view that has been designed like the code below , some picture that each of them has a textview and an edittext beside it, what I want is that I have to get the data of edittextes when the button is clicked. and there may be some edittext which are empty without any data ! but I don't know how can I access each edittext , because I can't use "findById" in this form of coding ! can any one help me by that ?

    <?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="vertical" >

    <TextView
        android:id="@+id/selection"
        android:layout_width="30dp"
        android:layout_height="30dp" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="512dp"
        android:drawSelectorOnTop="false" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.79" >

 <Button
            android:id="@+id/button1"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="Button" />

    <Button
            android:id="@+id/button2"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="Button" />

    </RelativeLayout>

    </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 = "wrap_content"
android:orientation   = "horizontal" >

    <ImageView
    android:id = "@+id/pic"
    android:padding       = "2dip"
    android:layout_width  = "100dp"
    android:layout_height = "100dp"
    android:src = "@drawable/download" />

<EditText
        android:id="@+id/editpizza"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:height="5dp"
        android:inputType="number"
        android:width="10dp" >

        <requestFocus />
    </EditText>

    <TextView
    android:id = "@+id/label"
    android:layout_width  = "wrap_content"
    android:layout_height = "wrap_content"
    android:textSize      = "40sp" />

</LinearLayout>

java code :

 public class ListView extends ListActivity {
        /** Called when the activity is first created. */
        private TextView selection;
        private EditText choose;
        private static final String[] items = {"مخصوص", "مخلوط", "یونانی", "قارچ و گوشت", "پپرونی", "مرغ و قارچ", "سبزیجات", "دریایی"};

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.view);

            setListAdapter(new IconicAdapter());
            selection = (TextView)findViewById(R.id.selection);
            choose=(EditText) findViewById(R.id.editpizza);
        }

        public void OnListItemClick(ListView parent, View v, int position, long id){
            selection.setText(items[position]);

        }

        private String getModel(int position){
            return(((IconicAdapter)getListAdapter()).getItem(position));
        }


        //=====================
        //New Class Starts Here
        //=====================
        class IconicAdapter extends ArrayAdapter<String> {

            public IconicAdapter() {
                super(ListView.this, R.layout.row, R.id.label, items);
            }

            public View getView(int position, View convertView, ViewGroup parent){          
                View row = super.getView(position, convertView, parent);
                view holder = (view)row.getTag();

                if(holder == null){                                                 
                    holder = new view(row);
                    row.setTag(holder);
                }

                if(getModel(position).contentEquals("مخصوص")){
                    holder.icon.setImageResource(R.drawable.download);
                }
                else if (getModel(position).contains("دریایی")){
                    holder.icon.setImageResource(R.drawable.drink);
                }
                else if (getModel(position).contains("سبزیجات")){
                    holder.icon.setImageResource(R.drawable.french);
                }
                else if (getModel(position).contains("مرغ و قارچ")){
                    holder.icon.setImageResource(R.drawable.frenchfrise);
                }
                else if (getModel(position).contains("پپرونی")){
                    holder.icon.setImageResource(R.drawable.sandwich);
                }
                else if (getModel(position).contains("قارچ و گوشت")){
                    holder.icon.setImageResource(R.drawable.ic_launcher);
                }
                else if (getModel(position).contains("یونانی")){
                    holder.icon.setImageResource(R.drawable.sandwich);
                }
                else if (getModel(position).contains("مخلوط")){
                    holder.icon.setImageResource(R.drawable.sandwich);
                }
                else if (getModel(position).contains("مخصوص")){
                    holder.icon.setImageResource(R.drawable.sandwich);
                }

                return(row);
            }       
        }
    }


   public class view {
    ImageView icon = null;

    view(View base){
        this.icon = (ImageView)base.findViewById(R.id.pic);
    }
}

Upvotes: 1

Views: 108

Answers (1)

Hevski
Hevski

Reputation: 1871

You can do this in the getView() function for the IconicAdapter ArrayAdapter.

First Inflate your view in getView function using

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.YourRowLayout, parent, false);

You can then use

TextView txtViewSelection = (TextView) rowView.findViewById(R.id.editpizza);

To get your textView or EditText or whatever you want to get

If your button is in the listview you can define it's on click listener function in the getView function also

Upvotes: 1

Related Questions