将嵌套类移动到外部文件

时间:2013-10-07 20:47:45

标签: java class constructor encapsulation

在我的主活动文件中,我有两个类,我想移出到新文件(只是为了清理代码)。这两个类是AdapterForPostsListViewPostFilter。我尝试使用Eclipse的“Refractor” - > “将类型移动到新文件”但它生成错误,因为在主/父活动中定义了一些变量。

public class Amawal extends SherlockActivity {

    // Declare Variables
    ListView list;
    EditText editsearch;
    private List<Post> wp_posts = null;
    private AdapterForPostsListView dataAdapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_activity);

        // Initialise DB; copy it from assets to system if not already done.
        AmawalDB db = new AmawalDB(this, null, 1);
        db.initialise();

        wp_posts = db.getAllPosts();

        populateListView();
        registerClickCallback();
    }

    private void populateListView() {
        // create an ArrayAdaptar from the Posts object
        dataAdapter = new AdapterForPostsListView(this, R.layout.item_view, wp_posts);
        ListView list = (ListView) findViewById(R.id.posts_container_listview);

        // Assign adapter to ListView
        list.setAdapter(dataAdapter);

        // enables filtering for the contents of the given ListView
        list.setTextFilterEnabled(true);

    }

    /*
     * http://www.mysamplecode.com/2012/07/android-listview-custom-layout-filter.
     * html
     */
    private class AdapterForPostsListView extends ArrayAdapter<Post> {

        private ArrayList<Post> originalList;
        private ArrayList<Post> newList;
        private PostFilter filter;

        public AdapterForPostsListView(Context context, int textViewResourceId, List<Post> countryList) {
            super(context, textViewResourceId, countryList);
            this.newList = new ArrayList<Post>();
            this.newList.addAll(countryList);
            this.originalList = new ArrayList<Post>();
            this.originalList.addAll(countryList);
        }

        @Override
        public Filter getFilter() {
            if (filter == null) {
                filter = new PostFilter();
            }
            return filter;
        }

        private class ViewHolder {
            TextView holder_post_title;
            TextView holder_post_content;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            Log.v("ConvertView", String.valueOf(position));
            if (convertView == null) {

                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = vi.inflate(R.layout.item_view, null);

                holder = new ViewHolder();
                holder.holder_post_title = (TextView) convertView.findViewById(R.id.item_postTitle);
                holder.holder_post_content = (TextView) convertView.findViewById(R.id.item_postContent);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            Post post = newList.get(position);
            holder.holder_post_title.setText(post.getpostTitle());
            holder.holder_post_content.setText(post.getPostContent());

            return convertView;

        }

        private class PostFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                constraint = constraint.toString().toLowerCase(Locale.getDefault());
                FilterResults result = new FilterResults();
                if (constraint != null && constraint.toString().length() > 0) {
                    ArrayList<Post> filteredItems = new ArrayList<Post>();

                    for (int i = 0, l = originalList.size(); i < l; i++) {
                        Post post = originalList.get(i);
                        if (post.toString().toLowerCase(Locale.getDefault()).contains(constraint))
                            filteredItems.add(post);
                    }
                    result.count = filteredItems.size();
                    result.values = filteredItems;
                } else {
                    synchronized (this) {
                        result.values = originalList;
                        result.count = originalList.size();
                    }
                }
                return result;
            }

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                newList = (ArrayList<Post>) results.values;
                notifyDataSetChanged();
                clear();
                for (int i = 0, l = newList.size(); i < l; i++)
                    add(newList.get(i));
                notifyDataSetInvalidated();
            }
        }
    }

    private void registerClickCallback() {
        ListView list = (ListView) findViewById(R.id.posts_container_listview);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {

                Post clickedPost = wp_posts.get(position);

                // New intent
                Intent i = new Intent(Amawal.this, SinglePost.class);

                int ID = clickedPost.getID();

                String message = "You clicked position " + position + " Which is ID " + ID;
                Toast.makeText(Amawal.this, message, Toast.LENGTH_LONG).show();

                // supply a Tag
                i.putExtra("ID", ID);

                // launch activity
                startActivity(i);
            }
        });
    }

    // Create the options menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Get the options menu view from menu.xml in menu folder
        getSupportMenuInflater().inflate(R.menu.menu, menu);

        // Locate the EditText in menu.xml
        editsearch = (EditText) menu.findItem(R.id.menu_search).getActionView();

        // Listen to search text change
        editsearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void afterTextChanged(Editable s) {
                String text = editsearch.getText().toString().toLowerCase(Locale.getDefault());

                dataAdapter.getFilter().filter(text);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

            @Override
            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

        });

        // Show the search menu item in menu.xml
        MenuItem menuSearch = menu.findItem(R.id.menu_search);

        menuSearch.setOnActionExpandListener(new OnActionExpandListener() {

            // Menu Action Collapse
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                // Empty EditText to remove text filtering
                editsearch.setText("");
                editsearch.clearFocus();
                return true;
            }

            // Menu Action Expand
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                // Focus on EditText
                editsearch.requestFocus();

                // Force the keyboard to show on EditText focus
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                return true;
            }
        });

        // Show the about us menu item in menu.xml
        MenuItem menuAbout = menu.findItem(R.id.menu_about);

        // Capture the about menu click
        menuAbout.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // TODO Auto-generated method stub
                // Open the about us activity
                Toast.makeText(getApplicationContext(), "Nothing here!", Toast.LENGTH_LONG).show();
                return false;
            }

        });

        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Close the database here
    }

}

由于我是Java的OOP的新手,我在使用新创建的类时遇到了问题:(

1 个答案:

答案 0 :(得分:0)

将一个构造函数添加到要移出的类中,这些类将从父类中获取所需的变量作为参数。将这些值存储为其方法可以访问的该类的成员变量。

例如,如果我们为了简单起见假设只需要传递originalList:

class PostFilter extends Filter {
    private ArrayList<Post> originalList;

    public PostFilter(ArrayList<Post> originalList) {
        this.originalList = originalList;
    }
...