无法使用include标记进行参考

时间:2015-07-07 14:16:07

标签: java android xml android-layout listview

我开发了一款应用。在我的应用程序中有: Product.java(片段) product.xml(包含listview和include标记) list_single(listview行)

这是我的代码:

Product.java

public class Product extends Fragment{

    public int ITEM_SELECTED = 1;
    ArrayAdapter<String> PROD;

    @Nullable
    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.product, container, false);

        String [] ELEMENTS1 = {
            "A","b","c"};

        ListView listView = (ListView)v.findViewById(R.id.listView);

        TextView a = (TextView)v.findViewById(R.id.textView);
        ImageButton add_cart = (ImageButton)v.findViewById(R.id.button2);
        switch(ITEM_SELECTED){

            case 1: PROD = new ArrayAdapter<String>(getActivity(), R.layout.single_row, R.id.textView, ELEMENTS1);break;

        }
        listView.setAdapter(PROD);

        add_cart.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.d("asd","asd");
           }
        });
        return v;
    }

}

product.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:id="@+id/layout">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="PRODOTTI"
        android:id="@+id/textView10"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:textColor="#000"
        android:textSize="20dp"
        android:textStyle="bold"
        android:typeface="monospace" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_marginTop="85dp"
        android:id="@+id/view1"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="35dp"
        android:layout_marginRight="35dp" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_below="@+id/view1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp" />

    <include layout="@layout/single_row"
        android:id="@+id/ly"
        android:visibility="gone"/>

</RelativeLayout>

这是list_single.xml

<?xml version="1.0" encoding="utf-8"?>

<EditText
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:inputType="numberSigned"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_alignBottom="@+id/button2"
    android:layout_toLeftOf="@+id/button2"
    android:layout_toStartOf="@+id/button2" />

<ImageButton
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button2"
    android:src="@drawable/product"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@null"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="20dp"
    android:layout_marginRight="5dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Q.TA"
    android:textColor="#083CBF"
    android:id="@+id/textView2"
    android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/editText"
    android:layout_alignStart="@+id/editText"
    android:layout_marginTop="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="PREZZO"
    android:textColor="#083CBF"
    android:id="@+id/textView3"
    android:layout_alignTop="@+id/textView2"
    android:layout_toLeftOf="@+id/textView2"
    android:layout_toStartOf="@+id/textView2"
    android:layout_marginRight="10dp" />

<EditText
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:inputType="number"
    android:ems="10"
    android:id="@+id/editText2"
    android:layout_alignTop="@+id/editText"
    android:layout_alignLeft="@+id/textView3"
    android:layout_alignStart="@+id/textView3" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="COLLI"
    android:textColor="#083CBF"
    android:id="@+id/textView4"
    android:layout_alignTop="@+id/textView3"
    android:layout_toLeftOf="@+id/textView3"
    android:layout_toStartOf="@+id/textView3"
    android:layout_marginRight="20dp" />

<EditText
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:inputType="numberSigned"
    android:ems="10"
    android:id="@+id/editText3"
    android:layout_alignTop="@+id/editText2"
    android:layout_alignLeft="@+id/textView4"
    android:layout_alignStart="@+id/textView4" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/textView"
    android:layout_toStartOf="@+id/textView4"
    android:id="@+id/ded"
    android:layout_toLeftOf="@+id/textView4"
    android:layout_marginRight="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:singleLine="false"
        android:id="@+id/textView"
        android:layout_below="@+id/textView4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_gravity="center" />
</FrameLayout>

问题: 当我尝试点击imagebutton时,它没有显示Log.d.看起来 OnClickListener未运行。我尝试过:setText("xy");但这也不起作用。

问题出在listview中吗? 我不能使用single_row的元素。我的应用程序没有崩溃。请帮忙!谢谢

更新:

listView.setAdapter(PROD);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            ImageButton add_cart = (ImageButton) view.findViewById(R.id.button2);

            add_cart.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Log.d("asd", "asd");
                    Toast.makeText(getActivity(), "hi",
                            Toast.LENGTH_LONG).show();
                }
            });
        }
    });

这对我不起作用......

1 个答案:

答案 0 :(得分:0)

你做错了。

ImageButton add_cart = (ImageButton)v.findViewById(R.id.button2);

您的图片按钮button2位于list_single.xml,您正试图在使用product.xml充气的View中找到它。在为ListView设置适配器之前也是如此。因此,当您获得对ImageButton的引用时,会有ListView的视图呈现。这是你理想的做法

listView.setAdapter(PROD);
listView.setOnItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    ImageButton add_cart = (ImageButton)view.findViewById(R.id.button2);
     //more logic
  }
});