Celsius allows you to go from proficiency to entry: ListView instance interpretation and package name naming habits

Keywords: Android Java Database xml

Writing idle articles with more reflection, not making money for human misdeeds

  • At first, the author never liked to say that the day before yesterday, reflection, more nonsense, not to work with people, three contemplations, contemptuous short-sighted, should not play with ink, so mystifying, hurt the hearts of readers. I'm sorry, Wang Haihan.

  • I don't make a penny or take a piece of it, but I don't read it twice with all my heart's likes and dislikes. But technology is not the same as it. The greatest temptation of technology lies in its one, rigorous and meticulous, not two, and I'm good at it. But in the same way, the problems arise, and today's reflection is particularly alarming. So there are three definite moments nowadays: first, words and beads; secondly, three-day written articles; thirdly, do not forget their hearts and minds; fourthly, reason is not human reason, Tao is Tao, it must be said from the beginning.

Blue, blue, blue; ice, water, and cold water.

  • Example 1. The effect is as follows:

    Now let's talk about it: Simply put, ListView should have been invented by a smart lazy person. In this way, he has greatly improved the efficiency of his work. Simple text lists require only four steps:

First step, declare data, adapters, and what you have in the layout file
ListView, and also bind ListView events:
private String[] proNames;
private ArrayAdapter<String> adapter;
private ListView lvProlist;

Step 2: Preparing data
this.proNames = new String []{Negative Ion Deacarifier,""Multifunctional Cooking Machine,""Docobi Stirring Rod,""Cocoa Breakfast Machine,""Yamei Noodle Machine,""Shuangli Man Cooking Pot Ground Net,""..."};

The third step, the view layer, you have a list, but the contents of the list have not been filled, I am filling this text, so there must be a separate layout file:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    android:textColor="#A10000">

</TextView>

The fourth step is to prepare and bind the adapter (the adapter is to find the layout content, the adapter is to bind the listView to the adapter):

  // Ready adapter
        this.adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.lv_item, proNames);

        // Binder Adapter
        this.lvProlist.setAdapter(adapter);

Then we succeeded. The source code address is as follows: http://pan.baidu.com/s/1jHD7I9w

Let's look at the advanced state of ListView:

  • When the content of ListView is complex, our data source becomes complex, so it's better to have an entity class. General entity classes are generally placed in the persistence layer, and adapters are generally written in the tool class. I just give an example here, so the persistence layer is written together:
  • Step 1: Write out the entity class:
package com.chinasoft.listviewdemo;

/**
 * Created by Arvin on 2017/2/20.
 */

public class Product {
    private int pid;
    private String name;
    private float price;
    private String desc;
    private int image;

    public Product(int pid, String name, float price, String desc, int image) {
        this.pid = pid;
        this.name = name;
        this.price = price;
        this.desc = desc;
        this.image = image;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }
}

Step 2: Write out the tool class of the adapter (remember that the adapter is generally associated with the small layout, List and adapter are related). The function of the adapter is to associate the overseas Chinese as if there is an esophagus between the stomach and the mouth.

package com.chinasoft.listviewdemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Arvin on 2017/1/20.
 */

public class ProductListAdapter extends BaseAdapter {

    private Context context;
    private List<Product> products;

    public ProductListAdapter(Context context, List<Product> products) {
        this.context = context;
        this.products = products;
    }

    @Override
    public int getCount() {
        return products.size();
    }

    @Override
    public Object getItem(int position) {
        return products.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // Get small layouts
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.lv_pro_item, null);
        }

        // Get the components in the small layout
        ImageView ivProImage = (ImageView) convertView.findViewById(R.id.iv_pro_image);
        TextView tvProId = (TextView) convertView.findViewById(R.id.tv_pro_id);
        TextView tvProName = (TextView) convertView.findViewById(R.id.tv_pro_name);
        TextView tvProPrice = (TextView) convertView.findViewById(R.id.tv_pro_price);
        TextView tvProDesc = (TextView) convertView.findViewById(R.id.tv_pro_desc);

        // Setting data for components
        Product product = products.get(position);
        ivProImage.setImageResource(product.getImage());
        tvProId.setText(product.getPid()+"");
        tvProName.setText(product.getName());
        tvProPrice.setText("¥" + product.getPrice());
        tvProDesc.setText(product.getDesc());

        // Return to Small Layout View
        return convertView;
    }
}

The third step, data initialization, and new operations:

package com.chinasoft.listviewdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class ProductShowActivity extends AppCompatActivity {

    private List<Product> products;
    private ListView lvProduct;

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

        this.lvProduct = (ListView) findViewById(R.id.lv_product);

        // Initialize product data
        this.products = new ArrayList<Product>();
        Product pro1 = new Product(1001,"Aijing vacuum cleaner",1200,"Super silent eight times system achievement, can milling cutter each feel, do not let go of any dead corner",R.drawable.pro01);
        products.add(pro1);
        Product pro2 = new Product(1002,"Advanced electric rice cooker",2388,"The fragrant rice comes from it.",R.drawable.pro04);
        products.add(pro2);
        Product pro3 = new Product(1003,"Negative ion acaricide remover",890,"What do you want to do with spicy mites?", R.drawable.pro06);
        products.add(pro3);
        Product pro4 = new Product(1004,"Multifunctional Cooking Machine",2359,"Rich in function, do as you like...", R.drawable.pro08);
        products.add(pro4);
        Product pro5 = new Product(1005,"Docobi Stirring Bar",300,"Easy to operate, fresh food can be obtained at any time and anywhere.",R.drawable.pro10);
        products.add(pro5);

        ProductListAdapter adapter = new ProductListAdapter(getApplicationContext(), products);
        this.lvProduct.setAdapter(adapter);
    }
}

The layout file is not written, and the source code address is as follows: http://pan.baidu.com/s/1gfol6EZ

Next, I'll talk to you about the "package name problem" in the development specification. The package name of package is very troublesome for beginners. For many mature developers, putting different classes in different packages is helpful for sorting out their own logic. Now let's talk about it (let's look at the picture first):

The arrow in the figure above refers to the package name. The name of the package name varies from person to person, so we introduce some general rules:

1. dao: Operational classes related to databases, typically written here
2. utils: Usually put some tool classes, such as some people in Android development, will put database tables and other operations here.
3. sqlite: As I provided in the sample diagram, I put database-related operations into it ~personal habits.
4. vo layer: Some people are also called entity or domain, which means persistence layer. So-called persistence, put some entity classes, in general, the things inside are immovable.
5. fagment: Here are some fragments. Personally, I think they are similar to Active, but they are different.
6. common: General toolkit, I've left some global variables here.
7. adapter: Usually used in Android development, it writes adapters, such as: ListView <> need adapters.
8. application: Usually this thing comes with the system, but if you want to write it yourself, remember to inherit the Application.
9. service layer: Service layer is mainly responsible for the logic application design of business module.
10. controller layer: Control layer, Android, is really not common.
11. ui: In Android development, some programmers like to write layout-related classes in ui, but others like to write out, the author belongs to the second kind.
12. view: This is actually more responsible for the presentation of the page.

Note: The biggest difference in learning lies in the same foundation, the same language, the same structure, and the same reason. It is natural to say that if the full text is forwarded, please indicate the source.
MOE: http://www.imooc.com/u/4102287
CSDN: http://blog.csdn.net/baidu_34750904
Brief Book: http://www.jianshu.com/u/6db129d0ef0f

Posted by BigJohn on Wed, 03 Apr 2019 09:33:31 -0700