简体   繁体   English

从LinkedList获取值到LinkedList

[英]Get value from a LinkedList into a LinkedList

I have a linkedlist name categories. 我有一个链表名称类别。 Into this linkedlist i had values like : id, name, description, entries(linkedlist) 进入此链表,我具有以下值:id,名称,描述,条目(链表)

in the method i get a the id with a bundle. 在方法中,我得到了一个捆绑的ID。 now my problem is, how can i connect/enter/get the data from entries by using the id? 现在我的问题是,如何使用ID连接/输入/获取条目中的数据?

(the id, name, description and entries data will be loaded from the database) (ID,名称,描述和条目数据将从数据库中加载)

Loading from Database and add it to the LinkedList: 从数据库加载并将其添加到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);

From the MainActivity: 从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();

}

From the ItemScreen.java: 从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());

... ...

Now here is my problem... how can i get the entries values? 现在这是我的问题...我如何获取条目值?

It looks like you have to start at the beginning of the linked list and go through each node comparing the bundle ID to the categories node ID. 看来您必须从链表的开头开始,然后遍历每个节点,将包ID与类别节点ID进行比较。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM