2013-07-25 41 views
0

我android.I創建的項目中有兩個Java類和兩個XML classes.I在我的項目有錯誤。我的項目有一定的邏輯錯誤

在這個節目,我在最後一行。(colleg.setadapter(適配器))有錯誤,否則沒有對這個文件沒有問題。 主要活動:

package com.example.collegedetails; 



import android.os.Bundle; 
import android.app.Activity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.view.Menu; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleCursorAdapter; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    protected EditText searchText; 
    protected SQLiteDatabase db; 
    protected Cursor cursor; 
    protected ListAdapter adapter; 
    protected TextView colleg; 


/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    db = (new Database(this)).getWritableDatabase(); 
    searchText = (EditText) findViewById (R.id.searchText); 
    colleg = (TextView) findViewById (R.id.text); 
} 

@SuppressWarnings("deprecation") 
public void search(View view) { 
    // || is the concatenation operation in SQLite 

      cursor = db.rawQuery("SELECT _id, CName FROM college", 
              new String []{"%" + Integer.parseInt(searchText.getText().toString() + "%")}); 
      adapter = new SimpleCursorAdapter(
          this, 
          R.layout.listitem, 
          cursor, 
          new String[] {"CName"}, 
          new int[] {R.id.clgName}); 
      colleg.setAdapter(adapter); 
} 

} 

我沒有錯誤,此文件 Database.java:

package com.example.collegedetails; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class Database extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "collegeinfo"; 

    public Database(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     /* 
     * Create the employee table and populate it with sample data. In step 
     * 6, we will move these hardcoded statements to an XML document. 
     */ 
     String sql = "CREATE TABLE IF NOT EXISTS college (" 
       + "_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "Code INTEGER, " 
       + "CName TEXT)"; 
     db.execSQL(sql); 

     ContentValues values = new ContentValues(); 

     values.put("Code", "1309"); 
     values.put("CName", "MEENAKSHI SUNDARAJAN ENGINEERING COLLEGE"); 
     db.insert("college", null, values); 

     values.put("Code", "1447"); 
     values.put("CName", "JAWAHAR ENGINEERING COLLEGE"); 
     db.insert("college", null, values); 

     values.put("Code", "1450"); 
     values.put("CName", "LOYOLA ENGINEERING COLLEGE"); 
     db.insert("college", null, values); 

     values.put("Code", "2005"); 
     values.put("CName", "GOVERNMENT COLLEGE OF ENGINEERING"); 
     db.insert("college", null, values); 

     values.put("Code", "2005"); 
     values.put("CName", "PSG COLLEGE OF TECHNOLOGY"); 
     db.insert("college", null, values); 

     values.put("Code", "2007"); 
     values.put("CName", "COIMBATORE INSTITUTE OF TECHNOLOGY"); 
     db.insert("college", null, values); 

     values.put("Code", "2360"); 
     values.put("CName", "SUGUNA COLLEGE OF ENGINEERING ENGINEERING"); 
     db.insert("college", null, values); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS college"); 
     onCreate(db); 
    } 

} 

我對這個文件沒有錯誤。 activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 

       <EditText 
         android:id="@+id/searchText" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="1"/> 

       <Button 
         android:id="@+id/searchButton" 
         android:text="Search" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:onClick="search"/> 

     </LinearLayout> 

     <TextView 
       android:id="@+id/text" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"/> 

</LinearLayout> 

我沒有錯誤此文件... listitem.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="8dp" > 

    <TextView 
     android:id="@+id/clgName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 






</RelativeLayout> 
+0

請將您的錯誤的堆棧跟蹤。沒有它,很難說出什麼問題。 – AlexVogel

回答

0

你設置適配器的TextView?不可能。你應該有編譯錯誤。

什麼你可能想要做的,是要改變的TextView到ListView控件,然後設置適配器。