在列表视图中单击按钮时更改文本视图的内容

时间:2016-06-21 07:18:14

标签: android listview

this is the image of layout tile.xml 我已经使用自定义适配器创建了一个列表,它有2个textview和2个按钮,现在我想要的是在按钮点击时更改textview之一的可见性。

我正在处理自定义适配器外部的按钮点击。我想使用开启和关闭按钮切换第二个textview的visiblity toggle和id tvstatus。

这是customadapter的代码

package slide.apptech.com.rpiconnect;

/**
 * Created by MOHIT on 09-06-2016.
 */
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

//custom adapter class extends a arrayadapter
public class customadapter2 extends ArrayAdapter<String> {
    private final Context context;
    private final ArrayList values;
    private String stv = "Ststus";

    public customadapter2(Context context, ArrayList values) {

        //for super constructor pass
        // context files
        //layout file required for list
        //arraylist that has strings to be displayed
        super(context, R.layout.tile, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos = position;
        final ViewGroup par = parent;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.tile, parent, false);
        final TextView textView = (TextView) rowView.findViewById(R.id.tvappname);
        Button on = (Button) rowView.findViewById(R.id.bon);
        Button off = (Button) rowView.findViewById(R.id.boff);
        final TextView status = (TextView) rowView.findViewById(R.id.tvstatus);

        on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                status.setVisibility(View.VISIBLE);
                notifyDataSetChanged();
                ((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()

            }
        });

        off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) ;
                ((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
            }
        });

        //get(position method is used to access the elements of arraylist)
        String val = (String) values.get(position);
        textView.setText(val);
        // Change the icon for Windows and iPhone
        String s = (String) values.get(position);

        return rowView;
    }

    public void myClickHandler(View v)
    {

    }
}

这是我用作侦听器的xml文件的代码

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


    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_margin="2dp"
        android:layout_height="100dp"
        card_view:cardCornerRadius="4dp"
        android:layout_alignParentStart="true"
        android:background="#611818"
        android:paddingBottom="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="OFF"
            android:id="@+id/boff"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginStart="37dp"
            android:focusable="false"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bon"
            android:text="ON"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_marginEnd="28dp"
            android:focusable="false"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ttile"
            android:id="@+id/tvappname"
            android:textSize="20sp"
            android:layout_above="@+id/boff"
            android:layout_centerHorizontal="true"
            android:textColor="@color/abc_input_method_navigation_guard" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ON"
                android:id="@+id/tvstatus"
                android:layout_below="@+id/tvappname"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="20dp"
                android:textColor="#060505"
                android:visibility="invisible"/>


        </RelativeLayout>
    </android.support.v7.widget.CardView>


</RelativeLayout>

如果您想要任何其他文件,请发表评论

1 个答案:

答案 0 :(得分:0)

不,不要这样做。直接更改listview中任何视图的值是个坏主意。只需在你的arraylist中进行更改,然后调用notifyDataSetChanged()来反映更改。

@Override
public View getView(final int position, View convertView, ViewGroup parent)    {
    final int pos = position;
    final ViewGroup par = parent;
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.tile, parent, false);
    final TextView textView = (TextView) rowView.findViewById(R.id.tvappname);
    Button on = (Button) rowView.findViewById(R.id.bon);
    Button off = (Button) rowView.findViewById(R.id.boff);
    final TextView status = (TextView) rowView.findViewById(R.id.tvstatus);
    //get(position method is used to access the elements of arraylist)
    String val = (String) values.get(position);
    textView.setText(val);
    if(val.equalsIgnoreCase("off")){
        status.setVisibility(View.INVISIBLE);
    }else{
        status.setVisibility(View.VISIBLE);
    }
    on.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            values.set(position, "On");
            notifyDataSetChanged();
            ((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
        }
    });

    off.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            values.set(position, "Off");
            notifyDataSetChanged();
            ((ListView) par).performItemClick(v, pos, 0); // Let the event be handled in onItemClick()
        }
    });

    // Change the icon for Windows and iPhone
    String s = (String) values.get(position);

    return rowView;
}

希望它会有所帮助:)