Android开发之ListFragment结合LoaderManager加载数据

2019-03-01 09:45:19 浏览数 (1)

之前实现过Android开发之ContentProvider结合LoaderManager加载数据,现在来实现用ListFragment加载 先看main.xml

代码语言:javascript复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@ id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.listfragmentloadermanager.MainActivity" >

    <LinearLayout
        android:id="@ id/left"
        android:layout_width="0sp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <EditText
            android:id="@ id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@ id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加姓名" />

    </LinearLayout>

    <LinearLayout
        android:id="@ id/right"
        android:layout_width="0sp"
        android:layout_weight="2"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>

说明:右边的linearlayout是要加载listFragment的子类的,通过“添加”按钮实现 由于是listFragment的子类,所以还是需要布局文件的(就是listfragment的默认布局) list.xml

代码语言:javascript复制
<?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="vertical" >
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />
</LinearLayout>

这里我们把listview每一项的item也自定义,就用最简单的textview item.xml

代码语言:javascript复制
<?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="vertical" >
    <TextView
        android:id="@ id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />
</LinearLayout>

讲完了布局,我们来谈谈MainActivity.java和RightFragment.java MainActivity.java

代码语言:javascript复制
package com.example.listfragmentloadermanager;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
    private Button button;
    private EditText editText;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //加载listfragment
        transaction = getFragmentManager().beginTransaction();
        RightFragment rightFragment = new RightFragment();
        transaction.replace(R.id.right, rightFragment);
        transaction.commit();

        editText = (EditText)findViewById(R.id.editText1);
        button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                String name = editText.getText().toString();
                ContentResolver resolver = getContentResolver();
                Uri uri = Uri.parse("content://com.example.loadermanagertest2."  
                        "PersonContentProvider/person");
                ContentValues values = new ContentValues();
                values.put("name", name);
                resolver.insert(uri, values);
            }
        });
    }
}

为什么这里不需要初始化LoaderManager呢?因为LoaderManager监听的是listview和contentprovider的数据变化。而这个例子的listview在listfragment里面,所以要挪到listfragment里面初始化 RightFragment.java:

代码语言:javascript复制
package com.example.listfragmentloadermanager;

import java.util.ArrayList;
import java.util.List;

import android.app.ListFragment;
import android.app.LoaderManager;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class RightFragment extends ListFragment implements LoaderCallbacks<Cursor>{
    private LoaderManager loaderManager;
    private MyAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        loaderManager.initLoader(0, null, this);
        View view = inflater.inflate(R.layout.list, null);
        return view;
    }
    //onCreate里面初始化一些参数,而onCreateView里面创建布局
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loaderManager = getLoaderManager();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri uri = Uri.parse("content://com.example.loadermanagertest2."  
                "PersonContentProvider/person");
        CursorLoader loader = new CursorLoader(getActivity(), uri, 
                null, null, null, null);
        return loader;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        List<String> list = new ArrayList<String>();
        while (data.moveToNext()) {
            list.add(data.getString(data.getColumnIndex("name")));
        }
        adapter = new MyAdapter();
        adapter.bindData(list);
        setListAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }

    //自定义adapter取数据
    class MyAdapter extends BaseAdapter {
        private List<String> list;

        public void bindData(List<String> list) {
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            if (convertView == null) {
                view = LayoutInflater.from(getActivity()).inflate(
                        R.layout.item, null);
            } else {
                view = (View) convertView;
            }
            TextView textView = (TextView) view.findViewById(R.id.textView1);
            textView.setText(list.get(position).toString());
            return view;
        }
    }
}

接下来就是实验结果

当然,com.example.loadermanagertest2.PersonContentProvider也就是上一篇这里写链接内容提到的manifest清单文件android:exported=”true”才行

0 人点赞