1

我需要一个网格视图,每个字段中都有 imageView、editText 和 textView,最后下面的一个按钮我尝试了很多代码,但主要问题是按钮也复制到 gridview 的每个字段中。除非我在我的代码中没有提到它!这是getView的代码:

 public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View v;
    if(convertView==null){
             LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        v= inflater.inflate(R.layout.gridview,parent, false);

            tv = (TextView)v.findViewById(R.id.textView1);
            tx = (EditText)v.findViewById(R.id.editText1);
            tv.setText(""+R.drawable.download);
            ImageView iv = (ImageView)v.findViewById(R.id.imageView1);
            iv.setImageResource(mThumbIds[position]);




    }
    else
    {
        v = convertView;
    }
    return v;

xml文件:

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" >

</GridView>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/download" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="textview" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>





    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />



</LinearLayout>
4

1 回答 1

1

最后我可以解决我的问题。(使用listview,但编码之间没有太大区别)

<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);
    }
}
于 2013-08-30T12:30:56.330 回答