我有一个数据库,它有三个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();
}
});
}
}
有什么建议吗?
提前致谢。
答案 0 :(得分: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, "....");
}
}
};