简体   繁体   中英

How to create and call elements from this type of list?

I'm really new to the android world, at the moment I am using sketchware to simplify the process and using custom code when needed.

All tips are welcomed, and also if you have relevant information with sources where I can read from that would be great!

So what I want to do is to make a simple app where there is a texteditor, a textview and a button. That's no problem. But I am stuck as I want to make a list or datastructure that will hold all the postcodes in Norway, with the areas they belong to. So as I input 0653 in the text editor and press the button it will put "Oslo" in the textview. for 7500 "Trondheim" etc.

I cant really put any code here, but what I tried now was insert key Oslo value 0653 at 1 in map postnr if that makes any sense. with this way I would have to add 9999 codes though, which will take very long time to do, not to mention the loading time on the creation.

Well.. As I can understand , you want to put the data into a List of map and then traverse the list wherever it is required. Right?? Now the question is how you can going to insert data into that list. If u have your data(postalCode and place) in an excel sheet then you can write a java code which reads the file and insert into your list of map. Here is a code which reads an xlsx file having key value as follows-

OSLO 6053

BOSTON 1841

package com.vibbs.demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReadDemo {

public static void readXLSFile(Map<String, String> mapByPostCode , Map<String, String> mapByPostValue) throws IOException, EncryptedDocumentException, InvalidFormatException
{
    InputStream ExcelFileToRead = new FileInputStream("/pathtoFile/postalDemo.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(ExcelFileToRead);
    Sheet worksheet = workbook.getSheetAt(0);
    Iterator<Row> rows = worksheet.rowIterator();

    Cell postalCodeCell = null;
    Cell postalValueCell = null;
    while(rows.hasNext()) {
        Row row = (Row) rows.next();
        postalValueCell  = row.getCell(0);
        postalCodeCell = row.getCell(1);

        postalValueCell.setCellType(Cell.CELL_TYPE_STRING);
        postalCodeCell.setCellType(Cell.CELL_TYPE_STRING);

        String postalValue = postalValueCell.getStringCellValue();
        String postalCode = postalCodeCell.getStringCellValue();
        mapByPostCode.put(postalCode, postalValue);
        mapByPostValue.put(postalValue, postalCode);
    }
}

public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
    //1841 BOSTON , 6053
    Map<String, String> mapByPostCode = new HashMap<String, String>();
    Map<String, String> mapByPostValue = new HashMap<String, String>();

    ExcelReadDemo.readXLSFile(mapByPostCode , mapByPostValue);

    System.out.println(mapByPostCode.get("1841")); // If u provide postcode it will give you place
    System.out.println(mapByPostValue.get("BOSTON")); // If u provide place it will give you postCode
}
}

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