我正在构建我的第一个AndroidApp,并且无法使用我的数据库中的数据来填充ListView。数据肯定存在,因为我可以遍历它和Toast()它并且数据显示 - 我只是因为某些原因无法将它输入ListView。以下是代码:
ListSmsRecipients.java
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class ListSmsRecipients extends Base_Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.smslist);
Button addSmsRecipient = (Button) findViewById(R.id.btn_listSms_addSms);
DbHelper dbhelper = new DbHelper(getApplicationContext());
SQLiteDatabase db = dbhelper.getWritableDatabase();
String[] from = new String[] { DbHelper.SMS_NAME, DbHelper.SMS_PHONE, DbHelper.SMS_MESSAGE };
String[] column = new String[] {DbHelper.SMS_ID, DbHelper.SMS_NAME, DbHelper.SMS_PHONE, DbHelper.SMS_MESSAGE };
int[] toViewIDs = new int[] { R.id.temp_smslist_item_name, R.id.temp_smslist_item_phone, R.id.temp_smslist_item_message };
Cursor cursor = db.query(DbHelper.SMS_TABLE_NAME, column, null, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.smslist_item, cursor, from, toViewIDs);
ListView list = (ListView)findViewById(R.id.listview_smsList);
list.setAdapter(adapter);
// This works
cursor.moveToFirst();
while (cursor.moveToNext())
{
String name = cursor.getString(cursor.getColumnIndex(DbHelper.SMS_NAME));
String phone = cursor.getString(cursor.getColumnIndex(DbHelper.SMS_PHONE));
String message = cursor.getString(cursor.getColumnIndex(DbHelper.SMS_MESSAGE));
Toast.makeText(getApplicationContext(), "Name = " + name + "\nPhone = " + phone + "\nMessage = " + message, Toast.LENGTH_SHORT).show();
}
cursor.close();
}
}
BaseActivity.java
import android.support.v4.app.FragmentActivity;
public class Base_Activity extends FragmentActivity
{
}
smslist.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="@+id/listview_smsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
smslist_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/temp_smslist_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text" />
<TextView
android:id="@+id/temp_smslist_item_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text" />
<TextView
android:id="@+id/temp_smslist_item_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
答案 0 :(得分:1)
问题在于适配器声明。将getApplicationContext()更改为getActivity(),如下所示
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.smslist_item, cursor, from, toViewIDs);
希望这会对你有所帮助。
答案 1 :(得分:0)
在Anu和Ramesh_D的帮助下,我只是将我的代码更改为Extend Activity cursor.close(),然后将适配器调用更改为以下内容:SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.smslist_item,cursor ,from,toViewIDs);