我需要一些帮助,我正在从主要活动的数据库表中检索项目(类别)列表,这些都运行良好。问题是用户应该单击其中一个项目,这将导致数据库中的另一个表被查询但在另一个活动(workbook.java)中。 那么如何将点击的类别的ID带到下一个活动中,并且还确切地知道单击了哪个项目以便查询正确的表格?
从一个db表中检索类别并在列表视图中显示它们的代码,以及处理列表项的点击的方法: MainActivity.java
@SuppressLint("NewApi")
public void displayCategories(){
cursor = dbhelper.getCategories();
String[] FROM = { FeedReaderDbHelper.KEY_CATEGORY_NAME};
int[] TO = {R.id.textView1};
adapter = new SimpleCursorAdapter(this, R.layout.row_item, cursor, FROM, TO, 0);
categories = (ListView) findViewById(android.R.id.list);
categories.setAdapter(adapter);
categories.setOnItemClickListener(new ListClickHandler());
}
public class ListClickHandler implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long arg3) {
// TODO Auto-generated method stub
TextView category_text = (TextView) view.findViewById(R.id.textView1);
String category = category_text.getText().toString();
Intent intent = new Intent(MainActivity.this, WorkBook.class);
intent.putExtra("selected-item", category);
intent.putExtra("item-ID", position);
startActivity(intent);
}
}
下面的代码在WorkBook.java中,它应该根据前一个活动中选择的类别(MainActivity.java)查询另一个表:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.work_book);
text = (TextView) findViewById(R.id.text);
//get the intent from which this activity is called
Intent intent = getIntent();
//fetch value from key-value pair and make it visible on textView
String category = intent.getStringExtra("selected-item");
text.setText("You are here: "+category);
}
我的数据库代码。类别表是在主活动中查询的类别列表。然后,如果用户选择“问候语”类别,则应在下一个活动中查询表格问候语。
package database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.ateso.learnateso.Category;
public class FeedReaderDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "LearnAteso.db";
public static final int DATABASE_VERSION = 1;
//Table Names
public static final String CATEGORIES_TABLE = "categories";
public static final String GREETINGS_TABLE = "greetings";
//Common column name
public static final String KEY_ID = "_id";
//Categories table column names
public static final String KEY_CATEGORY_NAME = "catname";
//greetings table column names
public static final String KEY_GREETING = "greeting";
public static final String KEY_HINT = "hint";
public static final String KEY_AUDIO = "audio";
public static final String KEY_ANSWER = "answer";
public static final String KEY_OPTION_1 = "option_1";
public static final String KEY_OPTION_2 = "option_2";
public static final String KEY_OPTION_3 = "option_3";
//Table create statements
//categories table create statement
private static final String CREATE_TABLE_CATEGORIES =
"create table " + CATEGORIES_TABLE + "(" + KEY_ID + " integer primary key, "
+ KEY_CATEGORY_NAME + " text);";
//greetings create statement
private static final String CREATE_TABLE_GREETINGS =
"create table " + GREETINGS_TABLE + "(" + KEY_ID + " integer primary key, "
+ KEY_GREETING+ " text, " + KEY_HINT + " text, " + KEY_AUDIO + " text, " + KEY_ANSWER + " text, "
+ KEY_OPTION_1 + " text, " + KEY_OPTION_2 + " text, " + KEY_OPTION_3 + " text);";
//constructor
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// @Override
public void onCreate(SQLiteDatabase db) {
//creating the tables
db.execSQL(CREATE_TABLE_CATEGORIES);
db.execSQL(CREATE_TABLE_GREETINGS);
}
// @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//on upgrade drop old tables
db.execSQL("DROP TABLE IF EXISTS " + CATEGORIES_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + GREETINGS_TABLE);
//create new tables
onCreate(db);
}
//-----------------categories table methods---------------//
//getting the categories out of the db
public Cursor getCategories(){
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM categories;";
Cursor c =db.rawQuery(sql, null);
return c;
}
//getting greetings table content from the db
public Cursor getAteso(){
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM " + GREETINGS_TABLE + ";";
Cursor c = db.rawQuery(sql, null);
if (c != null && c.getCount()>0){
c.moveToFirst();
do{
String audio = c.getString(c.getColumnIndex(KEY_AUDIO));
String greeting = c.getString(c.getColumnIndex(KEY_GREETING));
String hint = c.getString(c.getColumnIndex(KEY_HINT));
String answer = c.getString(c.getColumnIndex(KEY_ANSWER));
String opt_1 = c.getString(c.getColumnIndex(KEY_OPTION_1));
String opt_2 = c.getString(c.getColumnIndex(KEY_OPTION_2));
String opt_3 = c.getString(c.getColumnIndex(KEY_OPTION_3));
} while (c.moveToNext());
}
return c;
}
}
那么如何跟踪用户在MainActivity.java中选择哪个类别以查询WorkBook.java中的相应表。
感谢您的帮助
答案 0 :(得分:0)
Hye,我最近也在我的App中使用了这种模式,用户点击了list-Item,然后在其他活动中处理了有关该项目点击的信息。 来自 MainActivity 从任意点击的List-Item的类别列表中获取ID,
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View view, int position, long time) {
Intent i = new Intent(Categories.this,WorkBook.class);
i.putExtra("category",YourArrayLIST.get(position).Category);
startActivity(i);
}
});
}
<强> YourArrayLIST.get(位置).Category 强>
此处 YourArrayLIST 是您已映射所有API元素的类的实例,例如;您从JSON / XML响应获得的BookName,Author等。 类别是列表项中的视图,您将获得在下一个活动中处理的参考。
现在在您的接收活动中,即WorkBook,请在您的onCreate方法中执行此操作。
bundle = getIntent().getExtras();
if (bundle != null) {
String strName = bundle.getString("category");
String url = "http://192.168.0.100:7001/XYZ/abc/Info/" + strName + "/";
//now you have the URL to send it to the DB to get info from respective table.
String URL = Uri.encode(url, "@#&=*+-_.,:!?()/~'%"); //this is for the case if
there is any space between your Category Name so API will not process it
}