-1

我想要这样的界面

在此处输入图像描述

所以我创建了 2 个文件 XML Place 和 Place1

代码地点.xml:

<?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="vertical" >

<EditText
    android:id="@+id/filter_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" >
</EditText>

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

代码 Place1.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >

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

    <ImageView
        android:id="@+id/icon_place"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="7dp"
        android:src="@drawable/icon_landscapes" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="10" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/label_place"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="@+id/label_place" />

            <TextView
                android:id="@+id/label_address"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@+id/label_address" />
        </LinearLayout>

        <TextView
            android:id="@+id/label_Phone"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginTop="5dp"
            android:layout_weight="2"
            android:gravity="center"
            android:text="@+id/label_Phone" />
    </LinearLayout>
</LinearLayout>

</LinearLayout>

这是活动类中的代码:

public class Place extends Activity {

private String DB_NAME = "Danang Travel.sqlite";

String[] from;
int[] to;

ListView lvPlace;
ArrayAdapter<String> adapterPlace;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.place);

    EditText filterEditText = (EditText) findViewById(R.id.filter_text);

    lvPlace = (ListView) findViewById(R.id.listView1);
    lvPlace.setTextFilterEnabled(true);

    // **********************************************************************
    SQLiteDatabase DB = null;
    Intent t = getIntent();
    Bundle extra = t.getExtras();
    String temp = extra.getString("k");
    try {
        DB = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);

        Cursor c = DB.rawQuery(
                "SELECT Name,Address,Phone FROM ServiceDetail Where SerID = '"
                        + temp + "' ORDER BY Name", null);

        from = new String[]{"Name","Address","Phone"};
        to = new int[]{R.id.label_place,R.id.label_address,R.id.label_phone};

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.place1, c, from, to);


        lvPlace.setAdapter(adapter);

它不起作用!我真的不明白为什么?请帮我!

4

1 回答 1

0

我发现这个带有 ListActivity 类的工作示例代码,对初学者非常有用。只需正确研究它并在您的代码中实现,

它只是从 db 中读取数据并使用 ArrayList 类来存储值,并使用 ArrayAdapter 在 ListActivity 中设置值。

public class CRUDonDB extends ListActivity {

    private final String SAMPLE_DB_NAME = "myFriendsDb";
    private final String SAMPLE_TABLE_NAME = "friends";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ArrayList<String> results = new ArrayList<String>();
        SQLiteDatabase sampleDB = null;

        try {
            sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                    SAMPLE_TABLE_NAME +
                    " (LastName VARCHAR, FirstName VARCHAR," +
                    " Country VARCHAR, Age INT(3));");

            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Makam','Sai Geetha','India',25);");
            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Chittur','Raman','India',25);");
            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Solutions','Collabera','India',20);");

            Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
                    SAMPLE_TABLE_NAME +
                    " where Age > 10 LIMIT 5", null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("FirstName"));
                        int age = c.getInt(c.getColumnIndex("Age"));
                        results.add("" + firstName + ",Age: " + age);
                    }while (c.moveToNext());
                } 
            }

            this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (sampleDB != null) 
                sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
                sampleDB.close();
        }
    }
}
于 2012-09-20T15:31:34.013 回答