gpt4 book ai didi

android - setListAdapter 的使用

转载 作者:行者123 更新时间:2023-11-29 00:38:47 26 4
gpt4 key购买 nike

在我的应用程序中,4 个 fragment 附加到一个 Activity 类。在我的 Activity 类中,我使用这个设置了根内容 View

setContentView(R.layout.fragment_pager);

我的四个 fragment 在单独的 java 文件中。

在我的一个 fragment 中,我显示了我使用的联系人列表

mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1}, 0);


setListAdapter(mAdapter);
setListShown(true);

我想知道 setListAdapter 在我的案例中有什么用,以及它将 SimpleCursorAdapter 类给出的内容列表放在哪里?以及如何设置 fragment 的标题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:drawable/gallery_thumb">

<TextView android:id="@+id/text"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/hello_world"/>

<!-- The frame layout is here since we will be showing either
the empty view or the list view. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<!-- Here is the list. Since we are using a ListActivity, we
have to call it "@android:id/list" so ListActivity will
find it -->
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"/>

<!-- Here is the view to show if the list is emtpy -->
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="No items."/>

</FrameLayout>

</LinearLayout>



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

mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1}, 0);
//setListAdapter(mAdapter);
// setListShown(true);
// getLoaderManager().initLoader(0, null, this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Contacts");

return v;
}

当我使用此方法并删除 onCreate 和 onCreateView 两个方法时,我的 Cursor 显示数据

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

mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1}, 0);
setListAdapter(mAdapter);
setListShown(true);
getLoaderManager().initLoader(0, null, this);

}

但是当我同时使用这三种方法时,我的应用程序会意外停止。这三种方法一起使用会出现什么错误?

package com.keepintouch.android;

import android.os.Bundle;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import android.provider.ContactsContract;

import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;

import android.view.View;


import android.widget.ListView;


import com.actionbarsherlock.app.SherlockListFragment;


public class ContactsFragment extends SherlockListFragment implements LoaderManager.LoaderCallbacks<Cursor>{

// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;

// If non-null, this is the current filter the user has provided.
String mCurFilter;
View lv;

// These are the Contacts rows that we will retrieve.
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
};

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

}




public static Fragment newInstance(Context context){
ContactsFragment contactFragment = new ContactsFragment();
return contactFragment;
}


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

mAdapter = new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1, null,
new String[] {ContactsContract.Contacts.DISPLAY_NAME},
new int[] { android.R.id.text1}, 0);

setListAdapter(mAdapter);
setListShown(true);
getLoaderManager().initLoader(0, null, this);

}



@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// Insert desired behavior here.
Log.i("FragmentComplexList", "Item clicked: " + id);
}



public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created. This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(mCurFilter));
} else {
baseUri = ContactsContract.Contacts.CONTENT_URI;
}

// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ ContactsContract.Contacts.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);

// The list should now be shown.
if (isResumed()) {
setListShown(true);
} else {
setListShownNoAnimation(true);
}
}

public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed. We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}



}

最佳答案

如果您使用的是 Listfragement setListAdapter,请在您的默认列表中设置 Adapter,Listfragement 会附带 ..

如果使用操作栏 (android:targetSdkVersion="15")

ActionBar ab =  this.getActionBar();
ab.setTitle(" ");

如果不是那么

  getActivity().setTitle("     "); // after onAttach called

关于android - setListAdapter 的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10945161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com