从链接列表中获取值到链接列表

时间:2014-02-17 14:26:17

标签: java android

我有一个链接列表名称类别。 在这个链表中我有以下值:id,name,description,entries(linkedlist)

在方法中我得到了一个带有一个包的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());

...

现在这是我的问题......我怎样才能获得条目值?

1 个答案:

答案 0 :(得分:0)

看起来你必须从链表的开头开始,然后浏览每个节点,将包ID与类别节点ID进行比较。

祝你好运!