构建一个搜索我的ListView并被卡住了

时间:2015-10-09 11:03:48

标签: android android-listview

我收到2个错误,一个说cannot find symbol method findViewById(int),另一个说cannot find symbol variable arraylist

我查看了整个代码,但在这段代码中我找不到什么错误。

这是我的代码:

package activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.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.EditText;
import android.widget.ListView;
import com.example.echezona.myfettle.R;
import java.util.ArrayList;
import java.util.Locale;
import adapter.ListViewAdapter;
import model.Pharmacy;

public class PharmacyFragment extends Fragment  {

    // Declare Variables
    ListView list;
    ListViewAdapter adapter;
    EditText editsearch;
    String[] name;
    String[] location;
    ArrayList<Pharmacy> arrayList = new ArrayList<Pharmacy>();

    public PharmacyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_pharmacy, container, false);


        // Inflate the layout for this fragment
        return rootView;

        // Generate sample data


        name = new String[]{"China", "India", "United States",
                "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh",
                "Russia", "Japan"};

        location = new String[]{"1,354,040,000", "1,210,193,422",
                "315,761,000", "237,641,326", "193,946,886", "182,912,000",
                "170,901,000", "152,518,015", "143,369,806", "127,360,000"};


        // Locate the ListView in listview_main.xml
     list = (ListView) findViewById (R.id.listview);

        for (int i = 0; i < name.length; i++)
        {
            Pharmacy p = new Pharmacy(name[i], location[i]);
            // Binds all strings into an array
            arrayList.add(p);
        }

        // Pass results to ListViewAdapter Class
        adapter = new ListViewAdapter(this, arrayList);

        // Binds the Adapter to the ListView
        list.setAdapter(adapter);

        // Locate the EditText in listview_main.xml
        editsearch = (EditText) findViewById(R.id.search);

        // Capture Text in EditText
        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
                String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
                adapter.filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                                          int arg2, int arg3) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
                // TODO Auto-generated method stub
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

1.Issue找不到符号方法findViewById(int)

在findviewById

之前添加rootView
list = (ListView) rootView .findViewById (R.id.listview);

return rootView;移至onCreate关闭的位置

答案 1 :(得分:0)

好吧,我得到了你的错。在你的oncreate view方法中,你没有调用任何视图就返回你的视图。所以你可以做以下事情。

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_pharmacy, container, false);

    // Inflate the layout for this fragment


    // Generate sample data


    name = new String[]{"China", "India", "United States",
            "Indonesia", "Brazil", "Pakistan", "Nigeria", "Bangladesh",
            "Russia", "Japan"};

    location = new String[]{"1,354,040,000", "1,210,193,422",
            "315,761,000", "237,641,326", "193,946,886", "182,912,000",
            "170,901,000", "152,518,015", "143,369,806", "127,360,000"};


    // Locate the ListView in listview_main.xml
 list = (ListView) findViewById (R.id.listview);

    for (int i = 0; i < name.length; i++)
    {
        Pharmacy p = new Pharmacy(name[i], location[i]);
        // Binds all strings into an array
        arrayList.add(p);
    }

    // Pass results to ListViewAdapter Class
    adapter = new ListViewAdapter(this, arrayList);

    // Binds the Adapter to the ListView
    list.setAdapter(adapter);

    // Locate the EditText in listview_main.xml
    editsearch = (EditText) findViewById(R.id.search);

    // Capture Text in EditText
    editsearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
            String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                                      int arg2, int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTextChanged(CharSequence arg0, int arg1, int arg2,
                                  int arg3) {
            // TODO Auto-generated method stub
        }
    });
          return rootView;

}