我在 android 中创建了项目。我有两个 java 类和两个 xml 类。我的项目中有错误。
在这个程序中,我在最后一行出现错误。(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>