无法解析方法'settext(java.lang.String)'
问题描述:
在这里发生的一些奇怪的事情,在item.settext(name);
我得到Cannot resolve method 'settext(java.lang.String)'
。无法解析方法'settext(java.lang.String)'
我正在创建应用程序,其中列表视图将由数据库结果动态生成。
布局
<?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="horizontal" >
<ListView
android:id="@+id/lvItems"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Study cursors"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
方法
public void bindView(View view, Context context, Cursor cursor) {
ListView item = (ListView) view.findViewById(R.id.lvItems);
String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
item.settext(name);
}
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dashboard_completed, container, false);
myDB = null;
try {
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
File file = new File(dir, "database.db");
myDB = SQLiteDatabase.openDatabase(file.toString(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READONLY);
String q = "SELECT * FROM companies ORDER BY name ASC";
Cursor mCursor = myDB.rawQuery(q, null);
// Find ListView to populate
ListView lvItems = (ListView) view.findViewById(R.id.lvItems);
// Setup cursor adapter using cursor from last step
dashboardCursorAdaptor todoAdapter = new dashboardCursorAdaptor(getActivity().getApplicationContext(), mCursor);
// Attach cursor adapter to the ListView
lvItems.setAdapter(todoAdapter);
myDB.close();
} catch (SQLException e) {
e.printStackTrace();
}
return view;
}
答
我看不到你的完整代码。我不知道你在哪里定义你的ListView
。许多开发人员在这一步中犯了错误。请记住,ListView
和TextView
将有不同的Layout
。
如果我必须给你一个解决方案,那么我会建议去检查你是否有ListView
和另一个布局item_WHATEVER,你会在其中定义TextView
。
在bindView
方法参考TextView
。
答
ListView中没有的setText方法 - 我想你想要什么就有什么一个TextView ..
+0
是的,你是对的。 –
谢谢你,你是对的。我需要为ListView创建2个布局,为TextView创建2个布局。它运作良好。 –
很高兴帮助你。 –