android/java 编程新手。我在作为选项卡应用程序一部分的 Fragment 中有一个 ListView 和一个 Button。我遇到的问题是,即使我将 CursorAdapter 附加到 listView 并且即使来自 CursorAdapter 的 getCount() 返回的不是 0,也不会调用来自 Adapter 的 newView() 方法。
我可以看到按钮,我可以使用它。如果我更改列表的背景,我会看到背景颜色。
我正在使用支持库 - API 级别 11。
public class MyListFragment extends Fragment {
private static final String TAG = "FragmentTabs";
private String mTag ;
private MainLogCursorAdapter mainAdapter;
private Cursor cursor;
private MainLogSource mainLogSource;
private LayoutInflater mInflater;
private ListView listView;
private Button newLogButton;
public MyListFragment() {
}
public MyListFragment(String tag) {
mTag = tag;
Log.d(TAG, "Constructor: tag=" + tag);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mInflater = LayoutInflater.from(getActivity());
mainLogSource = new MainLogSource(getActivity().getApplicationContext());
cursor = mainLogSource.getCursor();
mainAdapter = new MainLogCursorAdapter(getActivity ().getApplicationContext (), cursor);
View view = (View) mInflater.inflate(R.layout.listview1, null);
listView = (ListView) view.findViewById(R.id.listView1);
listView.setAdapter(mainAdapter);
System.out.println("Adapter set in onActivityCreated");
if (mTag.equals("Log")) {
loadMainLog();
} else {
if (mTag.equals("Rem")) {
loadReminderLog();
} else
loadConfig();
}
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (mTag==null)
mTag = new String("Log");
if (mTag.equals("Log")) {
return inflater.inflate(R.layout.listview1, container, false);
}
return null;
}
适配器:
public class MainLogCursorAdapter extends CursorAdapter implements Filterable{
private LayoutInflater mLayoutInflater;
private Context mContext;
private MainLogSource dbh;
private String mainTypeDesc;
public MainLogCursorAdapter(Context context, Cursor c) {
super(context, c, 0);
mLayoutInflater = LayoutInflater.from(context);
dbh = new MainLogSource(context);
mContext = context;
mCursor = c;
public void bindView(View view, Context context, Cursor cursor) {
String key = (String) cursor.getString(cursor
.getColumnIndex(MainLogHelper.KEY));
some other code here ...
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mLayoutInflater.inflate(R.layout.row, parent, false);
System.out.println("newView");
return v;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
System.out.println(super.getCount() + " count returned");
return super.getCount();
}
}
和 XML:
<?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/listView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="0.9"
android:transcriptMode="alwaysScroll"
>
</ListView>
<Button
android:id="@+id/NewLogButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:onClick="createLog"
android:text="@string/NewLogCreation" />
</LinearLayout>