- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,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/
import android.app.Activity; import android.os.Bundle; import android.app.ListActivity; import andro
我正在尝试构建 android 应用程序,它似乎将 Force Close 保持在同一位置 (setListAdapter/SimpleCursorAdapter) 现在不管我做什么。这是 DBAda
我有一个 Activity,其布局包含一个 DrawerLayout(来自支持的库)和一个 FrameLayout + ListView。在我的 FrameLayout 中,我有一个 ListFrag
这里我有一个奇怪的问题,有一种方法可以刷新我的 ListView : setListAdaptor(); 每当我从我的服务中调用此方法(以正确的方式)时,每当存在更改以更新列表时: @Overr
我尝试在 fragment 中显示 listView。但是方法 setListAdapter - 没有解决。我认为,我必须获取 listView 的 id (android.R.id.list);然后
在我的应用程序中,4 个 fragment 附加到一个 Activity 类。在我的 Activity 类中,我使用这个设置了根内容 View setContentView(R.layout.frag
我的 ListActivity 有问题,我使用一个线程来显示 ProgressDialog,其中获取所有已安装应用程序的列表。我把它变成一个列表适配器,然后我想设置 Activity 的列表适配器,但
正如标题所述,我在将我的数据从数据库链接到 ListView 时遇到了一些问题,问题在于适配器将仅设置返回的最后一行,而不是数据库表中的每一行。 我正在努力实现的一个例子: 餐 table 动物:猴猫
嗨,我正在尝试让我的 ListView 在我的 MainActivity 中使用 json。我的 onPostExecute(string result) 方法 出现错误。它给我以下错误:“MainA
我不知道发生了什么我首先在扩展 Activity 的类中发布了它。但即使将其更改为 ListActivity 后它仍然不起作用。我会把它简化到我认为问题所在的地方。 public class Pa
我正在开发一个应用程序,该应用程序(在 AsyncTask 中)执行对远程服务器的查询以获取 JSON 字符串。为了在我的 ListView 上显示数据,我扩展了一个 ArrayAdapter。 当我
我有一个 ListFragment,其数据由自定义适配器填充(在我的例子中是 SimpleAdapter)。我在扩展 ListFragment 的类中使用 notifyDataSetChanged()
我的程序具有 listArray 功能和 extends AppCompatActivity 但我的代码有错误 我的代码 新闻 Activity public class NewsActivity e
list_item.xml:http://pastebin.com/bn56L3NN 在 onCreate() 和创建 Comm 对象之后发生的情况是,我收到一条“已建立连接”消息,该消息在另一个线程
protected void onPostExecute(String file_url) { // dismiss the dialog after getting all prod
我有一个 Activity 类,其中包含一个扩展 AsyncTask 的内部 protected 类。当我单击一个按钮然后显示基于该数据的列表时,我试图让 AsyncTask 在后台加载数据。 这是我
我有一个 ListView ,它只是这样填充的: setListAdapter(new ArrayAdapter(this, android.R.layout.simp
我在 MainAvtivity 中有以下代码: private DBOperations DataDBoperation; @Override protected void onCre
当我尝试使用标题中列出的任一方法(getListView() 和 setListAdapter())时,我收到一条错误消息,指出这些方法无法解析。 这是我的代码: public void onRes
我制作了一个 ListView 来显示来自 url 的已解析 JSON 数据!相同的代码在没有 fragment 的情况下工作正常,但有 fragment 时 setListAdapter 不工作!我
我是一名优秀的程序员,十分优秀!