我有一个数据库和一个方法,它可以将我的所有信息从我的数据库中获取到 TextView。它只显示每行的标题,还有更多列,当我单击标题时,我想在新意图上显示这些列。我的问题是,我怎样才能让每个标题都可以点击,如果点击它会打开一个带有相关信息的新意图(需要传递 id)?非常感谢。:)
我的历史活动 -
public class HistoryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.history_layout);
final LinearLayout linearLayoutBackground = (LinearLayout) findViewById(R.id.MainLinearLayout);
TextView tv = (TextView) findViewById(R.id.tvHistory);
DataBaseHelper DataBaseHelper = new DataBaseHelper(this);
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
int backgroundImages[] = {R.drawable.background1,
R.drawable.background2};
final Drawable backgroundImage = getResources().getDrawable(
backgroundImages[intValue]);
linearLayoutBackground.setBackgroundDrawable(backgroundImage);
DataBaseHelper.open();
String HistoryData = DataBaseHelper.getListData();
DataBaseHelper.closee();
tv.setText(HistoryData);
}
}
getListData 方法 -
public String getListData() {
open();
Cursor c = myDataBase.query(TABLE_NAME1, columns, WHATTODONOW_COLUMN_ID, null, null, null, null);
String result = "";
int iTitle = c.getColumnIndex(WHATTODONOW_COLUMN_TITLE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getString(iTitle) + "\n";
}
return result;
}
历史布局 -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MainLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="10dp">>
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:text="All the things you have already done!"
android:textSize="20dp"
android:textStyle="bold">
</TextView>
</TableRow>
</TableLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="15dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<TextView
android:id="@+id/tvHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="get info from db"
android:paddingBottom="10dp"
android:textSize="25dp">
</TextView>
</ScrollView>
</LinearLayout>