簡體   English   中英

從LinkedList獲取值到LinkedList

[英]Get value from a LinkedList into a LinkedList

我有一個鏈表名稱類別。 進入此鏈表,我具有以下值:id,名稱,描述,條目(鏈表)

在方法中,我得到了一個捆綁的ID。 現在我的問題是,如何使用ID連接/輸入/獲取條目中的數據?

(ID,名稱,描述和條目數據將從數據庫中加載)

從數據庫加載並將其添加到LinkedList:

CRUDDatabase dbHandler = new CRUDDatabase(this);
        SQLiteDatabase db1 = dbHandler.getWritableDatabase();
        dbHandler.open();

        String[] categoryColumns = {dbHandler.ID, dbHandler.NAME, dbHandler.DESCRIPTION};
        String[] entryColumns = {dbHandler.ID, dbHandler.CATEG_ID, dbHandler.NAME, dbHandler.DESCRIPTION};

        Cursor cursorLoadCategory = db1.query(dbHandler.tableCategory, categoryColumns, null, null, null, null, null);
        cursorLoadCategory.moveToFirst();
        while(!cursorLoadCategory.isAfterLast()){

            String whereClause = dbHandler.CATEG_ID + "= ?";
            String whereArgs = String.valueOf(cursorLoadCategory.getInt(0));
            Cursor cr2 = db1.query(dbHandler.tableEntry, entryColumns,whereClause,new String[]{whereArgs},null,null,null);
            cr2.moveToFirst();
            Log.v("Get Entry Data by ID: ", "" + whereClause+" ::: " + whereArgs);

            LinkedList<Entry> entries = new LinkedList<Entry>();

                while(!cr2.isAfterLast()){
                    entries.add(new Entry(cr2.getInt(0), cr2.getInt(1), cr2.getString(2), cr2.getString(3)));

                    cr2.moveToNext();                       
                }

            System.out.println("--- CREATE CATEGORY ---");  
            Category category = new Category(cursorLoadCategory.getInt(0), cursorLoadCategory.getString(1), cursorLoadCategory.getString(2), null, null, null, entries);                


            MainActivity.categories.add(category);

從MainActivity:

@Override
public void onClick(View v) {

    ViewGroup elements = (ViewGroup)v.getParent();
    LinearLayout lL = (LinearLayout)elements.findViewById(R.id.CatTemp_menuBoxContainer);
    int categoryId = (Integer)lL.getTag();


    Intent intentSelectedCategory = new Intent(MainActivity.this, ItemScreen.class);
    intentSelectedCategory.putExtra("catId", (int)categoryId);
    startActivity(intentSelectedCategory);
    finish();

}

從ItemScreen.java:

public void onCreate(Bundle savedInstanceState ){
    super.onCreate(savedInstanceState);

    setContentView(R.layout.item_screen);

    Intent i = getIntent();
    Bundle extra = getIntent().getExtras();
    int categoryId = extra.getInt("catId");

    Log.v("THIS IS THE ID OF THIS CATEGORY:", ""+categoryId);

    TextView headTBox = (TextView)findViewById(R.id.HeadTextBox);
    headTBox.setText(MainActivity.categories.get(categoryId-1).getName());

...

現在這是我的問題...我如何獲取條目值?

看來您必須從鏈表的開頭開始,然后遍歷每個節點,將包ID與類別節點ID進行比較。

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM