我的项目有一些逻辑错误

时间:2013-07-25 09:44:32

标签: android xml adapter

我在android中创建了项目。我有两个java类和两个xml类。我的项目出错了。

在这个程序中我在最后一行收到错误。(colleg.setadapter(adapter))。否则这个文件没有问题。 主要活动:

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>

1 个答案:

答案 0 :(得分:0)

您是否将适配器设置为TextView?不可能。你应该有编译错误。

您可能想要做的是将TextView更改为ListView,然后设置适配器。