简体   繁体   中英

How to dynamically add rows from a table (sqlite) to layout? (Android)

I am trying to take all the rows from my db and add it to the current layout, also, making each row clickable in the layout to take the user to a new screen with the id...

Here is my current code, but stuck on that part... I understand that I can put an onClickListener, but then does it have to be a button?

For a visual representation refer to a notepad app on any device where each note title appears and clicking on it takes you to that note.

public class MainActivity extends Activity implements OnClickListener {

    private Button add_new_dictionary;

    // Database helper
    private DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // db setup
        db = new DatabaseHelper(getApplicationContext());

        // get all dictionaries
        List<db_dictionary> allDictionaries = db.getAllDictioniaries();
        for (db_dictionary dictionary_found : allDictionaries) {
            // create new view for each dictionary name include id and make it
            // dynamic and include onclick to take to dictionary_view screen
            Button dictionary_button = new Button(this);

        }

        add_new_dictionary = (Button) findViewById(R.id.add_new_dictionary);

    }

@Override
    public void onClick(View v) {
        if (v == add_new_dictionary) {
            Intent add_new_dictionary_intent = new Intent(MainActivity.this,
                    add_new_dictionary.class);
            startActivity(add_new_dictionary_intent);
        }

    }

}

To re-iterate the question: How do I go about dynamically taking rows from my db and adding it to my layout dynamically based on how many results are returned from the query? (However, the rows should be able to point to a new screen with the dictionary id)

All views in android can implement the OnClickListener interface. So no, it doesn't HAVE to be a button.

As you've decided to use the activity to handle this then you need to tell your code to pass the event to your implementation wihin your activity.

// create new view for each dictionary name include id and make it
// dynamic and include onclick to take to dictionary_view screen
Button dictionary_button = new Button(this);
dictionary_button.setOnClickListener(this);

A trick I use to store information is the setTag method which would allow you to retrieve the correct reference during your onClick:

dictionary_button.setTag(some_record_id);

Then retrieve it later:

@Override
    public void onClick(View v) {
        if (v == add_new_dictionary) {
            Intent add_new_dictionary_intent = new Intent(MainActivity.this,
                    add_new_dictionary.class);
            startActivity(add_new_dictionary_intent);
        }
        else (
            Object tag = v.getTag();
            //now launch the detail activity using the data from the tag
        }
    }

You should really look into ListAdapters and cursors to do this properly, but this method should get you going for now

If you need to pick data from a db and show it as a list (getting click events) you should probably look into CursorAdapter and ListView

http://developer.android.com/reference/android/widget/CursorAdapter.html

http://developer.android.com/reference/android/widget/ListView.html

You can fins many examples on the web on how to use a cursoradapter and the listview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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