2016-11-12 261 views
0

在這裏發生的一些奇怪的事情,在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; 
    } 

回答

1

我看不到你的完整代碼。我不知道你在哪裏定義你的ListView。許多開發人員在這一步中犯了錯誤。請記住,ListViewTextView將有不同的Layout

如果我必須給你一個解決方案,那麼我會建議去檢查你是否有ListView和另一個佈局item_WHATEVER,你會在其中定義TextView

bindView方法參考TextView

+0

謝謝你,你是對的。我需要爲ListView創建2個佈局,爲TextView創建2個佈局。它運作良好。 –

+1

很高興幫助你。 –

1

ListView中沒有的setText方法 - 我想你想要什麼就有什麼一個TextView ..

+0

是的,你是對的。 –

相關問題