2

从数据库中获取数据到 LisFragment。我需要使用这个 ListFragment 文件内容到 MainActivity.java 中。

扩展 ListFragment 的 SecondActivity:

String DB = "TestDB";
String TABLE_NAME = "addcamera";
SQLiteDatabase sampleDB = null;
ArrayList<String> results = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sampleDB =  SQLiteDatabase.openOrCreateDatabase(DB, null);          
Cursor c = sampleDB.rawQuery("SELECT CameraName FROM " + TABLE_NAME , null);            
if (c != null) {
if  (c.moveToFirst()) {
do {
    String CameraName = c.getString(c.getColumnIndex("CameraName"));
    results.add(CameraName);
    }while (c.moveToNext());
} 
} 
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, results);
}

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
 <fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:name="com.exercise.FragmentTest.SecondActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/image_list_fragment">
 </fragment>
</LinearLayout>

谁能帮我?

4

1 回答 1

3

好吧,第一件事是第一 - 你Activity不应该扩展ListFragment,你自己的Fragment班级应该这样做。希望我没有误解任何事情。:-)

但是,您可以很容易地将您的ListFragment派生视为ListActivity,即您可以使用该方法从您的 XMLgetListView()中获取ListView相应的元素 ( )。@android:id/list

看到您已经Cursor准备好了,您应该考虑使用 aSimpleCursorAdapter并让它为您管理光标,并且*所有*您需要做的就是指定要使用哪些字段填充元素。

例子:

getListView().setAdapter( 
    new SimpleCursorAdapter(
        getActivity(),
        R.layout.your_list_item_layout,
        cursor,
        new String[] { "database_table_1", "database_table_2" },
        new int[] { R.id.element_1, R.id.element_2 }

    )
);

更新:

这是您的自定义 ListFragment 的样子:

public class CustomListFragment extends ListFragment {

    // Attributes
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private SQLiteDatabase mSampleDb;
    private List<String> mResults;
    private Cursor mCursor;

    // Elements
    private ListView mListView;
    private SimpleCursorAdapter mListAdapter;

    // Constants
    private final String DB = "TestDB";
    private final String TABLE_NAME = "addcamera";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // Set our attributes
        mContext = getActivity();
        mLayoutInflater = inflater;

        // Let's inflate & return the view
        View view = mLayoutInflater.inflate(R.layout.your_fragment_layout, container, false);

        // Get the database handler & the cursor
        mSampleDb =  SQLiteDatabase.openOrCreateDatabase(DB, null);          
        mCursor = mSampleDb.rawQuery("SELECT CameraName FROM " + TABLE_NAME , null);  

        // Init
        init(view);

        // Return
        return view;

    }

    public void init(View v) {

        // Setup the listAdapter
        mListAdapter = new SimpleCursorAdapter(

            mContext,
            R.layout.your_list_item_layout,
            cursor,
            new String[] { "database_table_1", "database_table_2" },
            new int[] { R.id.element_1, R.id.element_2 }

            )
        );
        mListView.setAdapter(mListAdapter);

    }

    @Override
    public void onListItemClick(ListView l, View v, int pos, long id) {

        Toast.makeText(mContext, "CLICKED ON POS #" + pos + "!", Toast.LENGTH_SHORT).show();

    }
}
于 2012-07-10T08:13:15.450 回答