如何从新 activity 中的特定数据库 table 中检索数据作为从先前 activity 中的数据库 table 中选择列表项的结果

How to retrieve data from a particular db table in a new activity as a result of selecting a list item from a db table in the previous activity

我在这里需要一些帮助,我正在从主 activity 中的数据库 table 中检索项目(类别)列表,一切正常。问题是用户应该单击其中一个项目,这将导致查询数据库中的另一个 table 但在另一个 activity (workbook.java) 中。 那么如何将点击的类别的ID携带到下一个activity,并且还确切地知道点击了哪个项目,以便查询右边的table?

从一个数据库中检索类别的代码 table 并将它们显示在列表视图中,以及处理列表项点击的方法: 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中,它应该根据前面activity(MainActivity.java)中选择的类别查询另一个table:

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);

}

我的数据库代码。类别 table 是在主要 activity 中查询的类别列表。那么如果用户选择 Greetings 类别,table greetings 应该被查询但是在下一个 activity.

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 中查询相应的 table。

感谢您的帮助

嘿,我最近也在我的应用程序中使用了这种模式,用户点击列表项,然后在其他 activity 中处理有关该项目点击的信息。 从 MainActivityCategories 列表中获取任何点击列表项的 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(位置).类别

此处 YourArrayLIST 是 class 的实例,您在其中映射了所有 API 元素,例如;您从 JSON/XML 响应中获得的书名、作者等。 并且 Category 是列表项中的视图,您将在下一个 activity.

中对其进行处理。

现在在您接收 activity 即 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       
}