0
我試圖使用自定義ListView
內Fragment
也使用自定義CursorAdapter
,但到目前爲止我無法顯示ListView
。自定義ListView不顯示
我在做什麼錯了?
我Fragment
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class MyListFragment extends Fragment {
private CustomcursorAdapter mCustomcursorAdapter;
private ListView mListView;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_mylist, container, false);
//CursorAdapter
mCustomcursorAdapter = new CustomcursorAdapter(view.getContext(), null, 0);
//ListView
mListView = (ListView) view.findViewById(R.id.itemslist);
mListView.setAdapter(mCustomcursorAdapter);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
我的MainActivity
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager.findFragmentById(android.R.id.content) == null) {
MyListFragment myListFragment = new MyListFragment();
fragmentManager.beginTransaction().add(android.R.id.content, myListFragment).commit();
}
}
}
CustomcursorAdapter
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
public class CustomcursorAdapter extends CursorAdapter {
private LayoutInflater mLayoutInflater;
public CustomcursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textview = (TextView) view.findViewById(R.id.itemTextView);
textview.setText(cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME_ITEM)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return mLayoutInflater.inflate(R.layout.itemslist, parent, false);
}
}
後CustomcursorAdapter –
@Rod_Algonquin自定義的CursorAdapter貼! – ivoencarnacao
@ivoencarnacao您使用空遊標初始化您的適配器 - 無數據>無需顯示>無列表。 – nitzanj