textview.settext在微调器Onitemselected事件中不起作用

时间:2018-10-02 07:47:02

标签: android listview android-spinner

我有一个列表视图,该列表视图的项目具有自定义布局。 在此自定义布局中有一个微调器,我想根据微调器的选择更改自定义布局中某些项目的文本。 触发onItemSelected事件就可以了,但是textview.settext()函数不会更改textviews的文本。 这是我的实现:
listview_layout:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.28"
        android:orientation="vertical"
        android:paddingStart="5dp"
        android:paddingEnd="5dp">

        <TextView
            android:id="@+id/name_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:text="نام دستگاه:" />

        <TextView
            android:id="@+id/address_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:text="آدرس محل نصب:" />

        <TextView
            android:id="@+id/diameter_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:text="قطر لوله:" />

        <TextView
            android:id="@+id/batt1_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="میزان باتری1" />

        <TextView
            android:id="@+id/batt2_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="میزان باتری 2" />

        <TextView
            android:id="@+id/last_data_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="زمان ارسال آخرین داده:" />

        <Spinner
            android:id="@+id/sensor_no_spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/spin_items"/>

        <TextView
            android:id="@+id/sensor_type_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="نوع سنسور:" />

        <TextView
            android:id="@+id/sensor_range_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="بازه سنسور:" />

        <TextView
            android:id="@+id/sensor_val_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="آخرین داده:" />

    </LinearLayout>

</LinearLayout>

listview_adapter:

package com.dezelectronic.www.datalogger_abfa;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.List;

class listview_adapter extends BaseAdapter {

    private Context context;
    private List<datalogger> data;
    private TextView name,address,diameter,last_time,sensor_type,sensor_range,sensor_lastlog,battery1,battery2;
    private Spinner spin;
    private LayoutInflater inflater = null;

    public listview_adapter(Context context, List<datalogger> data) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.data = data;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
    public  void settxt(String type,String range,String lastlog){
        sensor_type.setText(type);
        sensor_range.setText(range);
        sensor_lastlog.setText(lastlog);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.listview_layout, parent,false);
        name =vi.findViewById(R.id.name_txt);
        address =vi.findViewById(R.id.address_txt);
        diameter =vi.findViewById(R.id.diameter_txt);
        spin=vi.findViewById(R.id.sensor_no_spinner);
        last_time=vi.findViewById(R.id.last_data_txt);
        sensor_type=vi.findViewById(R.id.sensor_type_txt);
        sensor_range=vi.findViewById(R.id.sensor_range_txt);
        sensor_lastlog=vi.findViewById(R.id.sensor_val_txt);
        battery1=vi.findViewById(R.id.batt1_txt);
        battery2=vi.findViewById(R.id.batt2_txt);
        spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                switch (pos){
                    case 0:
                        sensor_type.setText("نوع سنسور: "+data.get(position).sensor1_type);
                        sensor_range.setText("بازه سنسور: "+data.get(position).sensor1_range);
                        sensor_lastlog.setText("آخرین داده: "+data.get(position).sensor1_lastlog);
                        break;
                    case 1:
                        sensor_type.setText("نوع سنسور:"+data.get(position).sensor2_type);
                        sensor_range.setText("بازه سنسور: "+data.get(position).sensor2_range);
                        sensor_lastlog.setText("آخرین داده: "+data.get(position).sensor2_lastlog);
                        break;
                    case 2:
                        sensor_type.setText("نوع سنسور: "+data.get(position).sensor3_type);
                        sensor_range.setText("بازه سنسور: "+data.get(position).sensor3_range);
                        sensor_lastlog.setText("آخرین داده: "+data.get(position).sensor3_lastlog);
                        break;
                    case 3:
                        sensor_type.setText("نوع سنسور: "+data.get(position).sensor4_type);
                        sensor_range.setText("بازه سنسور: "+data.get(position).sensor4_range);
                        sensor_lastlog.setText("آخرین داده: "+data.get(position).sensor4_lastlog);
                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                sensor_type.setText("نوع سنسور: "+data.get(position).sensor1_type);
                sensor_range.setText("بازه سنسور: "+data.get(position).sensor1_range);
                sensor_lastlog.setText("آخرین داده: "+data.get(position).sensor1_lastlog);
            }
        });

        name.setText("نام دستگاه:"+data.get(position).name);//WORKS FINE
        address.setText("آدرس محل نصب:"+data.get(position).address);//WORKS FINE
        diameter.setText("قطر لوله:"+data.get(position).diameter);//WORKS FINE
        last_time.setText("آخرین داده:"+data.get(position).last_time);//WORKS FINE
        battery1.setText("میزان باتری یک:"+data.get(position).battery1);//WORKS FINE
        battery2.setText("میزان باتری دو:"+data.get(position).battery2);//WORKS FINE
        return vi;
    }
}

0 个答案:

没有答案