1

我有一个列表视图,其设计类似于下面的代码,其中每个图片旁边都有一个 textview 和一个 edittext,我想要的是当单击按钮时我必须获取 edittextes 的数据。并且可能有一些没有任何数据的空编辑文本!但我不知道如何访问每个edittext,因为我不能以这种编码形式使用“findById”!任何人都可以帮助我吗?

    <?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>

爪哇代码:

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

1 回答 1

1

您可以在ArrayAdapter的getView()函数中执行此操作。IconicAdapter

首先使用 getView 函数膨胀您的视图

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

然后你可以使用

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

获取你的 textView 或 EditText 或任何你想得到的

如果您的按钮在列表视图中,您也可以在 getView 函数中定义它的单击侦听器函数

于 2013-08-30T12:51:37.227 回答