我有一个DB,我正在尝试连接到listView(通过CursorAdapter)
我关注:Populating a ListView with a CursorAdapter 但是我错过了什么,因为我在列表视图中看不到任何东西。
我有一个包含片段的活动:
<fragment
android:id="@+id/fragmnet_main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="keepInTouch.today.MainListFragment"
tools:layout="@layout/fragment_main_list" />
片段看起来:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="keepInTouch.today.MainListFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mainFragmentListTitle"
android:background="#e43d3d"
android:textColor="#371b1b" />
<ListView
android:id="@+id/lvKits"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
每个列表项看起来都是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
</LinearLayout>
cursorAdpter看起来:
public class ContactsCursorAdapter extends CursorAdapter{
public ContactsCursorAdapter(Context context, Cursor cursor)
{
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.today_view, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
String title = cursor.getString(cursor.getColumnIndexOrThrow(KitSqliteOpenHelper.KEY_KIT_TITLE));
tvTitle.setText(title);
}
}
在激活onCreate(和更新后)我正在调用此函数:
private void updateListView() {
MainListFragment mainListFragment = (MainListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmnet_main_list);
Cursor cursor = KitSqliteOpenHelper.getInstance(this).getAllRawKits();
TextView textView = (TextView) findViewById(R.id.textV);
textView.setText("Total: " + cursor.getCount());
mainListFragment.updateListView();
}
public Cursor getAllRawKits()
{
SQLiteDatabase db = getWritableDatabase();
String QUERY_SELECT_ALL = String.format("SELECT * FROM %s", TABLE_KIT);
Cursor cursor = db.rawQuery(QUERY_SELECT_ALL, null);
return cursor;
}
public class MainListFragment extends Fragment {
ContactsCursorAdapter contactsCursorAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_list, container, false);
createContactsCursorAdapter(view);
return inflater.inflate(R.layout.fragment_main_list, container, false);
}
private void createContactsCursorAdapter(View view)
{
Cursor cursor = KitSqliteOpenHelper.getInstance(getActivity()).getAllRawKits();
contactsCursorAdapter = new ContactsCursorAdapter(getActivity(), cursor);
ListView lvItems = (ListView) view.findViewById(R.id.lvKits);
lvItems.setAdapter(contactsCursorAdapter);
}
public void updateListView() {
Cursor cursor = KitSqliteOpenHelper.getInstance(getActivity()).getAllRawKits();
contactsCursorAdapter.changeCursor(cursor);
contactsCursorAdapter.notifyDataSetChanged();
//contactsCursorAdapter.swapCursor(cursor);
}
}
我看到我有6个项目(光标大小),但在listView
中没有查看任何元素我曾尝试更换(仅用于调试):
tvTitle.setText(title);
tvTitle.setText("test");
在ContactsCursorAdapter中,bindView 我得到了同样的结果......
经过更多调试后,似乎没有调用来自ContactsCursorAdapter的newView和bindView。为什么?
主要活动看起来:
public class MainActivity extends AppCompatActivity {
//public class MainActivity extends FragmentActivity {
public MemoResultReciver memoResultReciver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
KitSqliteOpenHelper.getInstance(this);
updateListView();
}
static final int PICK_CONTACT_DETAILS = 1;
public void onClickButton(View v) {
Intent intent = new Intent(this, ChooseContactActivity.class);
startActivityForResult(intent, PICK_CONTACT_DETAILS);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_DETAILS) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
TextView textView = (TextView) findViewById(R.id.textV);
KitContactInfo kitContactInfo = data.getParcelableExtra("KitInfo");
String kitName = kitContactInfo.title;
Toast.makeText(getApplicationContext(), kitName + " was added", Toast.LENGTH_SHORT).show();
updateListView();
}
}
}
private void updateListView() {
MainListFragment mainListFragment = (MainListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmnet_main_list);
Cursor cursor = KitSqliteOpenHelper.getInstance(this).getAllRawKits();
TextView textView = (TextView) findViewById(R.id.textV);
textView.setText("Total: " + cursor.getCount());
mainListFragment.updateListView();
}
public void scheduleAlarm(KitContactInfo kitContactInfo)
{
// Construct an intent that will execute the AlarmReceiver
Intent intent = new Intent(getApplicationContext(), SchdulerBroadcastReceiver.class);
udateMemoService(intent, kitContactInfo.contactPhone, kitContactInfo.contactName);
// Create a PendingIntent to be triggered when the alarm goes off
final PendingIntent pIntent = PendingIntent.getBroadcast(this, SchdulerBroadcastReceiver.REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Calendar calander = Calendar.getInstance();
calander.set(kitContactInfo.year ,kitContactInfo.month, kitContactInfo.day, kitContactInfo.hour, kitContactInfo.min, 0);
alarm.set(AlarmManager.RTC, calander.getTimeInMillis(), pIntent);
}
private void udateMemoService(Intent intent,String number, String msg) {
intent.putExtra("PhoneNumber", number);
intent.putExtra("Message", msg);
}
}
答案 0 :(得分:0)
好吧,我看不到足够的信息,因为我看不到整个代码的实现,但有一些东西可以计数,也许是问题,当你在活动中onCreate()时片段没有连接到活动,所以当您从updateListView()
onCreate()
调用片段中的getActivity()
时,ListView
应返回空值。
正如我告诉你的那样,我不确定,如果你添加了整个Activity实现,你确定你正在创建并正确地将片段附加到活动,因为问题可能与活动有关显示片段而不是# weekend cache
<FilesMatch ".(jpg|jpeg|png|gif|swf)$">
<IfModule mod_headers.c>
Header set Cache-Control "max-age=604800, public"
</IfModule>
</FilesMatch>
<FilesMatch ".(xml|txt|css|js)$">
<IfModule mod_headers.c>
Header set Cache-Control "max-age=604800, proxy-revalidate"
</IfModule>
</FilesMatch>
本身。
答案 1 :(得分:0)
尝试通过以下方式替换MainActivity updateListView:
private void updateListView() {
FragmentManager fm = getFragmentManager();
FragmentTransaction f = fm.beginTransaction();
f.add(R.id.R.id.fragmnet_main_list, new MainListFragment());
f.commit();
MainListFragment mainListFragment = (MainListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmnet_main_list);
Cursor cursor = KitSqliteOpenHelper.getInstance(this).getAllRawKits();
TextView textView = (TextView) findViewById(R.id.textV);
textView.setText("Total: " + cursor.getCount());
mainListFragment.updateListView();
}
}