我认为我已经阅读了关于这个主题的所有答案,但没有得到正确答案。
我在一个名为Library的活动中有一个列表视图。当我点击某个项目时,我想在另一个活动中检索有关所点击项目的信息。我希望这些信息可以在不同的领域。 您可能会对我的代码了解得更好。
这是图书馆活动,我在其上列出了我称之为“标签”的列表视图:
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;
public class InfoTagActivity extends ActionBarActivity {
TextView textTagName;
TextView textTagId;
TextView textTagData;
String tagId;
private static final String TAG_NAME = "tag_name";
private static final String TAG_MID = "tag_mid";
private static final String TAG_DATA = "tag_data";
static SQLiteDatabase db = null;
private DatabaseHelper databaseHelper;
long tag_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_tag);
getSupportActionBar().hide();
//String tagName = this.getIntent().getStringExtra(TAG_NAME);
databaseHelper = new DatabaseHelper(this);
textTagName = (TextView) findViewById(R.id.tagName);
textTagId = (TextView) findViewById(R.id.tagId);
textTagData = (TextView) findViewById(R.id.tagInfo);
Intent intent = getIntent();
Cursor cursor = databaseHelper.getAllData();
if (cursor != null && cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex("tag_name"));
textTagName.append(name);
String id = cursor.getString(cursor.getColumnIndex("tag_mid"));
textTagId.append(id);
String info = cursor.getString(cursor.getColumnIndex("tag_data"));
textTagData.append(info);
}
}
}
DatabaseHelper.java:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseHelper {
private static final String TAG = DatabaseHelper.class.getSimpleName();
// database configuration
// if you want the onUpgrade to run then change the database_version
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "mydatabase.db";
// table configuration
public static final String TABLE_NAME = "person_table"; // Table name
private static final String TAG_ID = "_id"; // a column named "_id" is required for cursor
private static final String TAG_NAME = "tag_name";
private static final String TAG_MID = "tag_mid";
private static final String TAG_DATA = "tag_data";
private DatabaseOpenHelper openHelper;
private SQLiteDatabase database;
// this is a wrapper class. that means, from outside world, anyone will communicate with PersonDatabaseHelper,
// but under the hood actually DatabaseOpenHelper class will perform database CRUD operations
public DatabaseHelper(Context aContext) {
openHelper = new DatabaseOpenHelper(aContext);
database = openHelper.getWritableDatabase();
}
public void insertData (String aTagName, String aTagId, String aTagData) {
// we are using ContentValues to avoid sql format errors
ContentValues contentValues = new ContentValues();
contentValues.put(TAG_NAME, aTagName);
contentValues.put(TAG_MID, aTagId);
contentValues.put(TAG_DATA, aTagData);
database.insert(TABLE_NAME, null, contentValues);
}
public Cursor getAllData () {
String buildSQL = "SELECT * FROM " + TABLE_NAME;
Log.d(TAG, "getAllData SQL: " + buildSQL);
return database.rawQuery(buildSQL, null);
}
// this DatabaseOpenHelper class will actually be used to perform database related operation
private class DatabaseOpenHelper extends SQLiteOpenHelper {
public DatabaseOpenHelper(Context aContext) {
super(aContext, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
// Create your tables here
String buildSQL = "CREATE TABLE " + TABLE_NAME + "( " + TAG_ID + " INTEGER PRIMARY KEY, " +
TAG_NAME + " TEXT, " + TAG_MID + " TEXT, " + TAG_DATA + " TEXT )";
Log.d(TAG, "onCreate SQL: " + buildSQL);
sqLiteDatabase.execSQL(buildSQL);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
// Database schema upgrade code goes here
String buildSQL = "DROP TABLE IF EXISTS " + TABLE_NAME;
Log.d(TAG, "onUpgrade SQL: " + buildSQL);
sqLiteDatabase.execSQL(buildSQL); // drop previous table
onCreate(sqLiteDatabase); // create the table from the beginning
}
}
}
InfoTagActivity.java,我想在此活动中获取有关所点击项目的信息:
public class InfoTagActivity extends ActionBarActivity {
TextView textTagName;
TextView textTagId;
TextView textTagData;
String tagId;
private static final String TAG_NAME = "tag_name";
private static final String TAG_MID = "tag_mid";
private static final String TAG_DATA = "tag_data";
static SQLiteDatabase db = null;
private DatabaseHelper databaseHelper;
long tag_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info_tag);
getSupportActionBar().hide();
//String tagName = this.getIntent().getStringExtra(TAG_NAME);
databaseHelper = new DatabaseHelper(this);
textTagName = (TextView) findViewById(R.id.tagName);
textTagId = (TextView) findViewById(R.id.tagId);
textTagData = (TextView) findViewById(R.id.tagInfo);
Intent intent = getIntent();
Cursor cursor = databaseHelper.getAllData();
if (cursor != null && cursor.moveToFirst()) {
String name = cursor.getString(cursor.getColumnIndex("tag_name"));
textTagName.append(name);
String id = cursor.getString(cursor.getColumnIndex("tag_mid"));
textTagId.append(id);
String info = cursor.getString(cursor.getColumnIndex("tag_data"));
textTagData.append(info);
}
}
}
使用关联的activity_info_tag.xml:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/relativeLayout"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_above="@+id/relativeLayout2"
android:paddingTop="10dp">
<TextView
android:id="@+id/tagId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/main_theme_blue"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_above="@+id/tagDate"
android:layout_toRightOf="@+id/tagIdText"
android:layout_toEndOf="@+id/tagIdText" />
<TextView
android:id="@+id/tagDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/main_theme_blue"
android:layout_alignTop="@+id/tagDateText"
android:layout_toRightOf="@+id/tagDateText"
android:layout_toEndOf="@+id/tagDateText"
android:paddingLeft="5dp"
android:paddingRight="5dp"/>
<TextView
android:id="@+id/tagInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@color/main_theme_blue"
android:layout_below="@+id/tagDate"
android:layout_toRightOf="@+id/tagInfoText"
android:layout_toEndOf="@+id/tagInfoText"
android:paddingLeft="5dp"
android:paddingRight="5dp"
/>
</RelativeLayout>
我的问题是,到目前为止,当我点击列表视图的标签时,它总是只向我提供有关第一个标签的信息。我想获得有关我点击的标签的信息。 有人可以帮我吗?
非常感谢您的回答!