Android CustomListAdapter在TAB中从EditText OnClick设置TextView

时间:2017-09-17 14:38:37

标签: java android android-layout listview

在我的一个标签上,我有一个Listview,当用户在edittext上输入文本然后按下按钮时,我想要填充它。 edittext和按钮与listview一起位于选项卡式布局上。

textview和imageview在customlistadapter中设置。但当我按下按钮时没有任何反应,我似乎无法解决原因。

Tab3.java

@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.tab3, container, false);

        final EditText notes = (EditText) v.findViewById(R.id.editText);
        listView=(ListView)v.findViewById(R.id.list);
        Button button = (Button) v.findViewById(R.id.button3);
        String note = notes.getText().toString();

        int[] cross = new int[]{R.drawable.cross};
        String [] notesofrules = new String[]{note};

        final CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), notesofrules, cross);

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

        Button rulesSet = (Button)v.findViewById(R.id.button3);
        rulesSet.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                listView.setAdapter(adapter);
            }
        });
        return v;
        }

CustomListAdaper:

 public CustomListAdapter(Activity context, String[] notes, int[] imageCross) {
        super(context, R.layout.item);
        // TODO Auto-generated constructor stub
        this.context=context;
        this.notes = notes;
        this.imageCross = imageCross;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.item, null,false);

        TextView ruleNotesSet = (TextView) rowView.findViewById(R.id.textView1);
        ImageView image = (ImageView) rowView.findViewById(R.id.icon);

        image.setImageResource(imageCross[position]);
        ruleNotesSet.setText(notes[position]);
        return rowView;
    }

tab3.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
   >

    <TextView android:text="@string/thirdTabTitle" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FF9900"
        android:id="@+id/textView"
        android:textIsSelectable="true"
        android:textSize="55px"
        android:shadowColor="#73ffffff"
        android:fontFamily="sans-serif-bold" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="100px"
        android:ems="10"
        android:padding="10dp"
        android:id="@+id/editText"
        android:background="@drawable/edittext_rounded_corners"
        android:layout_marginTop="18dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true" />
    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="@string/addnote"
        android:background="@drawable/calculate_button"
        android:textColor="#000000"
        android:fontFamily="sans-serif-bold"
        android:layout_below="@+id/editText"
        android:textSize="40px"/>

    <TextView
        android:id="@+id/notes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:textSize="45px"
        android:layout_below="@+id/button3"
        android:fontFamily="sans-serif-bold"
        android:layout_toRightOf="@id/autohighlight_checkbox"
       />
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/notes" />
</RelativeLayout>


item.xml

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:weightSum="1">

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:padding="2dp"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#4CBE99"
                    android:textSize="14dp"
                    android:text="Item"
                    android:textAllCaps="true"
                    android:textStyle="bold" />
                <ImageView
                    android:id="@+id/icon"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:padding="5dp" />

            </LinearLayout>

    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

我认为你的适配器是空的。您只需要设置一次,然后在您的活动中调用适配器中的刷新功能onClick

public void refresh(String[] notes, int[] imageCross) {
    this.notes = notes;
    this.imageCross = imageCross;
    this.notifySataSetChanged();
}