1

我有一个包含三列 Maker、Model 和 Price 的数据库,并且我已经做到了,以便在按下按钮时将所有内容存储在字符串中并将字符串传递给 TextView。这个想法是我想以某种方式使文本可点击,并且当点击给定产品的文本时,将产品的价格存储在变量中。

这是显示数据库中所有内容的代码

    package com.WareHouse;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class viewall extends Activity {

SQLiteDatabase myDB = null;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.vall);

    Button vAll = (Button) findViewById(R.id.viewAll);
    final TextView view2 = (TextView) findViewById(R.id.multiAutoCompleteTextView1);

    vAll.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            try {
                String Data = "";

                String TableName = "myTable";

                myDB = getBaseContext().openOrCreateDatabase(
                        "DatabaseName", MODE_PRIVATE, null);

                Cursor c = myDB
                        .rawQuery("SELECT * FROM " + TableName, null);

                int Column1 = c.getColumnIndex("Field1");
                int Column2 = c.getColumnIndex("Field2");
                                    int Column3 = c.getColumnIndex("Field3");

                // Check if our result was valid.
                c.moveToFirst();
                if (c != null) {
                    // Loop through all Results
                    do {
                        String Name = c.getString(Column1);
                        String Model = c.getString(Column2);
                                                    String Price = c.getStrin(Column3);
                        Data = Data + "Maker: " + Name + "\n" + "Model: "
                                + Model + "\n" +"Price: " + Price+ "\n"+"\n";
                    } while (c.moveToNext());

                }
                //////////////////////////////////////////
                view2.setText(Data.toString());
            } catch (Exception e) {
                Log.e("Error", "Error", e);
            } finally {
                if (myDB != null)
                    myDB.close();
            }

        }
    });

    Button next = (Button) findViewById(R.id.buttonBack);
    next.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        }

    });


}
}

有什么建议么 ?

提前致谢。

4

1 回答 1

2

为 TextView 添加 android:clickable="true" 为:

<TextView  
    android:id="@+id/multiAutoCompleteTextView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:text="@string/hello"
    />

编辑:

在 viewall 活动中:

final TextView view2 = (TextView) findViewById(R.id.multiAutoCompleteTextView1);
view2.setOnClickListener(listener);
 private OnClickListener listener = new OnClickListener() {
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == (R.id.multiAutoCompleteTextView1)) {
            view2.setText(inname.getText() + "!" + "Welcome to Android");
        } 
        else {
            Log.v(TAG, "....");
        }
    }

};
于 2012-05-11T17:19:58.383 回答