Analysis of innovative training projects -- Part II

Keywords: Java Android

Analysis of innovative training projects -- Part II



This article begins to analyze the source code of the flying order project. The project of flying order requires two main functions: one is to query all poems containing input keywords, and the other is man-machine simulation flying order game.
This article will continue to analyze the code of the query function.

1, Project environment

android studio 4.1.2

Compile SDK version:30

Build Tools Version 30.0.3

gradle 6.8.3

2, Code analysis file

public class MainActivity extends AppCompatActivity {
    protected void onCreate(@Nullable Bundle savedInstanceState
    //@Nullable means that the defined field can be empty){

onCreate method is mainly the method that is loaded first after the initial startup (using this class).
When rewriting the onCreate method, remember to call super.onCreate(savedInstanceState) to load some components and call the onCreate method of its parent Activity to draw the interface.

Use the setContentView method to locate the activity_main layout file. is related to activity_main.xml,
The meaning of the method name setContentView can be seen. Start the content view and "use" the layout file activity_main.xml.

2. Page Jump

If you click the flying order, the page Jump will be performed. Here, the page Jump is executed using the intent class.
If the Intent object contains the class file of the target, it is a jump to display the intention; if the Intent object does not contain the class file of the target, it is an implicit intention jump, which involves the comparison of attribute values of many Intent objects.

 start.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                EditText key= findViewById(;
                Intent intent = new Intent(MainActivity.this,MainActivity2.class);
                Bundle bundle = new Bundle();

The bundle class is used to carry data. It is similar to a Map. It is used to store values in the form of key value name value pairs. The communication between two activities can be realized through the bundle class.
intent.putExtras() can be used to pass parameters
Method 1:
Set the method intent.putExtra("aaa", "bbbb");
Get the method this.getIntent().getCharSequenceExtra("aaa")
Method 2:
Setting method:
Bundle bd = new Bundle();
Acquisition method
Bundle bd=this.getIntent().getExtras();

The code in this part is that if you click the button of flying order, that is, button1, on the page corresponding to MainActivity, the page will jump to the page of flying order, and the keyword parameters will be passed to facilitate the flying order game.

3. Query return

    public void onClick_go(View view){
     //Here, the name of the folder must be raw, otherwise it cannot be found. Here is the statement to read in the file;
     //Data set in raw
    InputStream input = getResources().openRawResource(R.raw.sumtry);
    //Read the contents of the file
        //Pass the file content to scanner
        Scanner scanner=new Scanner(input);
        int sum=1;
        String line,ans = "";
        String title="";

        while (scanner.hasNext()){
            //Read one line at a time
            for (int i = 0; i < line.length(); i++)
                if(line.charAt(i)==': ') {//It means that it contains the author and the title of the poem
                    title = line; //Title stores the author and title of the poem
                    continue out;

            if(line.contains(key.getText().toString())){//Contains key words and is not the author's name or poem's name
                ans =line +"\n-------"+title.replaceAll("\\d+","")+ "\n";//Verse + author's name: the name of the poem. The title just contains the author's name and poem name of the poem
                //Regular expressions are used to remove all numeric characters

     if(poems.isEmpty()) poems.add("empty");
        MyAdapter myAdapter=new MyAdapter(MainActivity.this,R.layout.words_item,poems);


    }catch (Exception e){

My understanding of this part is to scan the existing Poetry Library from beginning to end according to the keywords, and scan the poetry containing keywords. The poetry + author name + poetry name are stored in the String type and added to the Arraylist container poems. Poems is used as the data source, plus the context, page layout, use the adapter, connect them in series, and connect the adapter It is associated with ListView through setAdapter.

res.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String str=poems.get(position);
Toast is a prompt bar to remind users without affecting their operation. When you click a poem in listview, such a prompt box will appear


After studying how the query function is completed in the previous two articles, I mainly applied adapter+listview. In addition, I also understood that the layout file + activity file is required to complete an app page. I often use the initialization of adapter to specify the display style in listview.

Posted by VagabondKites on Wed, 13 Oct 2021 07:18:41 -0700