iT邦幫忙

2021 iThome 鐵人賽

DAY 25
0
自我挑戰組

創業歷程系列 第 25

企劃實現(25)

  • 分享至 

  • xImage
  •  

在fragment裡面使用元件
常常在寫程式時會遇到再fragment抓不到元件的事情
用這個方式就能解決

 View view=inflater.inflate(R.layout.fragment_message, container, false);
 message_listview=view.findViewById(R.id.messa_listview);

這樣就可以了
完整程式碼

package com.example.graduationtopic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

/**
 */
public class MessageFragment extends Fragment implements  AdapterView.OnItemClickListener{
    private ListView message_listview;
    private String[] search=new String[]{};
    private String[] test=new String[]{};
    private String[] fruit_name=new String[]{"Apple","Banana","Orange","Grape","Strawberry"};
    private String[] mdates=new String[]{"Apple","Banana","Orange","Grape","Strawberry"};

    EditText editText;
    ListAdapter listAdapter;
    public MessageFragment() {
        // Required empty public constructor
    }

    public  void  onATTach(Context context){
        super.onAttach(context);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_message, container, false);
        editText=view.findViewById(R.id.editText);
        message_listview=view.findViewById(R.id.messa_listview);
        search =mdates;

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                search=test;
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
               if (editText.length()==0){
                search =mdates;}
            }

            @Override
            public void afterTextChanged(Editable editable) {

                for(int x =0; x < mdates.length;x++){
                    if (editText.getText().equals(mdates[x]))
                        mdates[x]=search[x];
                    adapter();
                }
                }


        });

        adapter();

        return  view;}

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
    public void adapter() {
        ArrayAdapter<String> ada =new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,search);
        message_listview.setAdapter(ada);
        message_listview.setOnItemClickListener(this);
    }
    public static boolean isEmpty(@Nullable CharSequence str) {
        return str == null || str.length() == 0;
    }

}

上一篇
企劃實現(24)
下一篇
企劃實現(26)
系列文
創業歷程30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言