简体   繁体   中英

create button on xml file (not new activity) and open new activity

I'm new in android studio and currently creating an app which will retrieve data from existing db of sqlite. based on what I'd found is to create a layout file (not by creating new empty activity) to design on how the display the data. But in the layout, I want the button to open new activity, but somehow it didn't and I didn't found any solution so far. and the image also didn't appeared. This is the code:

DataPOI.java

public class DataPOI {
    private int id;
    private String category;
    private String name;
    private String hour;
    private String phone_number;
    private String address;
    private String website;
    private int fee_adult_standard;
    private int fee_child_standard;
    private int fee_senior_standard;
    private int fee_adult_MyKad;
    private int fee_child_MyKid;
    private int fee_senior_MyKad;
    private int fee_student_standard;
    private int fee_student_MyKad;
    private String description;
    private byte[] photo;
    private String coordinate;
    private String door;
    private String parking1;
    private String parking2;

public DataPOI(int id, String category, String name, String hour, String phone_number,
               String address, String website, int fee_adult_standard, int fee_child_standard,
               int fee_senior_standard, int fee_adult_MyKad, int fee_child_MyKid,
               int fee_senior_MyKad, int fee_student_standard, int fee_student_MyKad,
               String description, byte[] photo, String coordinate, String door,
               String parking1, String parking2) {
    this.id = id;
    this.category = category;
    this.name = name;
    this.hour = hour;
    this.phone_number = phone_number;
    this.address = address;
    this.website = website;
    this.fee_adult_standard = fee_adult_standard;
    this.fee_child_standard = fee_child_standard;
    this.fee_senior_standard = fee_senior_standard;
    this.fee_adult_MyKad = fee_adult_MyKad;
    this.fee_child_MyKid = fee_child_MyKid;
    this.fee_senior_MyKad = fee_senior_MyKad;
    this.fee_student_standard = fee_student_standard;
    this.fee_student_MyKad = fee_student_MyKad;
    this.description = description;
    this.photo = photo;
    this.coordinate = coordinate;
    this.door = door;
    this.parking1 = parking1;
    this.parking2 = parking2;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getName() {
    return name;
}

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

public String getHour() {
    return hour;
}

public void setHour(String hour) {
    this.hour = hour;
}

public String getPhone_number() {
    return phone_number;
}

public void setPhone_number(String phone_number) {
    this.phone_number = phone_number;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

public int getFee_adult_standard() {
    return fee_adult_standard;
}

public void setFee_adult_standard(int fee_adult_standard) {
    this.fee_adult_standard = fee_adult_standard;
}

public int getFee_child_standard() {
    return fee_child_standard;
}

public void setFee_child_standard(int fee_child_standard) {
    this.fee_child_standard = fee_child_standard;
}

public int getFee_senior_standard() {
    return fee_senior_standard;
}

public void setFee_senior_standard(int fee_senior_standard) {
    this.fee_senior_standard = fee_senior_standard;
}

public int getFee_adult_MyKad() {
    return fee_adult_MyKad;
}

public void setFee_adult_MyKad(int fee_adult_MyKad) {
    this.fee_adult_MyKad = fee_adult_MyKad;
}

public int getFee_child_MyKid() {
    return fee_child_MyKid;
}

public void setFee_child_MyKid(int fee_child_MyKid) {
    this.fee_child_MyKid = fee_child_MyKid;
}

public int getFee_senior_MyKad() {
    return fee_senior_MyKad;
}

public void setFee_senior_MyKad(int fee_senior_MyKad) {
    this.fee_senior_MyKad = fee_senior_MyKad;
}

public int getFee_student_standard() {
    return fee_student_standard;
}

public void setFee_student_standard(int fee_student_standard) {
    this.fee_student_standard = fee_student_standard;
}

public int getFee_student_MyKad() {
    return fee_student_MyKad;
}

public void setFee_student_MyKad(int fee_student_MyKad) {
    this.fee_student_MyKad = fee_student_MyKad;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public byte[] getPhoto() {
    return photo;
}

public void setPhoto(byte[] photo) {
    this.photo = photo;
}

public String getCoordinate() {
    return coordinate;
}

public void setCoordinate(String coordinate) {
    this.coordinate = coordinate;
}

public String getDoor() {
    return door;
}

public void setDoor(String door) {
    this.door = door;
}

public String getParking1() {
    return parking1;
}

public void setParking1(String parking1) {
    this.parking1 = parking1;
}

public String getParking2() {
    return parking2;
}

public void setParking2(String parking2) {
    this.parking2 = parking2;
}
}

ListPOIadapter.java

public class ListPOIadapter extends BaseAdapter {
private Context mContext;
private List<DataPOI> mPOIList;

public ListPOIadapter(Context mContext, List<DataPOI> mPOIList) {
    this.mContext = mContext;
    this.mPOIList = mPOIList;
}

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

@Override
public Object getItem(int i) {
    return mPOIList.get(i);
}

@Override
public long getItemId(int i) {
    return mPOIList.get(i).getId();
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View v = View.inflate(mContext, R.layout.data_layout, null);
    //edit below this
    ImageView myPhoto = (ImageView)v.findViewById(R.id.imageView);
    TextView myName = (TextView)v.findViewById(R.id.name);
    TextView myHour = (TextView)v.findViewById(R.id.operational_hour);
    TextView myContact = (TextView)v.findViewById(R.id.contact_number);
    TextView myWebsite = (TextView)v.findViewById(R.id.website);
    TextView myAddress = (TextView)v.findViewById(R.id.address);

    //myPhoto.setI(mPOIList.get(i).getPhoto());
    myName.setText(mPOIList.get(i).getName());
    myHour.setText(mPOIList.get(i).getHour());
    myContact.setText(mPOIList.get(i).getPhone_number());
    myWebsite.setText(mPOIList.get(i).getWebsite());
    myAddress.setText(mPOIList.get(i).getAddress());
    return v;
}
}

AmusementPark.java

public class AmusementPark extends AppCompatActivity {
private ListView lvPOI;
private ListPOIadapter adapter;
private List<DataPOI> mPOIList;
private AmusementPark_Helper mDBHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_amusement_park);
    lvPOI = (ListView)findViewById(R.id.listview_product);
    mDBHelper = new AmusementPark_Helper(this);

    //Check existis database
    File database = getApplicationContext().getDatabasePath(AmusementPark_Helper.DBNAME);
    if(false == database.exists()) {
        mDBHelper.getReadableDatabase();
        //Copy db
        if(copyDatabase(this)) {
            Toast.makeText(this, "Copy database success", Toast.LENGTH_SHORT).show();
        }

        else {
            Toast.makeText(this, "Copy database error", Toast.LENGTH_SHORT).show();
            return;
        }
    }
    //Get product list in db when db exists
    mPOIList = mDBHelper.getListPOI();

    //Init adapter
    adapter = new ListPOIadapter(this, mPOIList);

    //Set adapter for listview
    lvPOI.setAdapter(adapter);
}

private boolean copyDatabase (Context context) {
    try {
        InputStream inputStream = context.getAssets().open(AmusementPark_Helper.DBNAME);
        String outFileName = AmusementPark_Helper.DBLOCATION + AmusementPark_Helper.DBNAME;
        OutputStream outputStream = new FileOutputStream(outFileName);
        byte[]buff = new byte[1024];
        int length = 0;
        while ((length = inputStream.read(buff)) > 0) {
            outputStream.write(buff, 0, length);
        }
        outputStream.flush();
        outputStream.close();
        Log.v("Amusement Park", "DB copied");
        return true;
    }

    catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}
}

AmusementPark_Helper.java

public class AmusementPark_Helper extends SQLiteOpenHelper {
public static final String DBNAME = "placeofinterest.sqlite";
public static final String DBLOCATION = "/data/data/com.example.lenovo.welcome.ListHelper";
private Context mContext;
private SQLiteDatabase mDatabase;

public AmusementPark_Helper (Context context) {
    super(context, DBNAME, null, 1);
    this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

public void openDatabase() {
    String dbPath = mContext.getDatabasePath(DBNAME).getPath();
    if(mDatabase != null && mDatabase.isOpen()) {
        return;
    }
    mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase() {
    if(mDatabase != null) {
        mDatabase.close();
    }
}

public List<DataPOI> getListPOI() {
    DataPOI placeofinterest = null;
    List<DataPOI> poiList = new ArrayList<>();
    openDatabase();
    Cursor cursor = mDatabase.rawQuery("SELECT * FROM amusement_park", null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        //below is depends on data type of each column
        placeofinterest = new DataPOI(
                cursor.getInt(0), cursor.getString(1), cursor.getString(2),
                cursor.getString(3), cursor.getString(4), cursor.getString(5),
                cursor.getString(6), cursor.getInt(7), cursor.getInt(8),
                cursor.getInt(9), cursor.getInt(10), cursor.getInt(11),
                cursor.getInt(12), cursor.getInt(13), cursor.getInt(14),
                cursor.getString(15), cursor.getBlob(16), cursor.getString(17),
                cursor.getString(18), cursor.getString(19), cursor.getString(20));
        poiList.add(placeofinterest);
        cursor.moveToNext();
    }
    cursor.close();
    closeDatabase();
    return poiList;
}
}

data_layout.xml (created not by using "create new empty activity")

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="210dp"
    android:background="#89cff0">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/imageView"
    android:text="Name of POI"
    android:textColor="#000"
    android:textSize="19sp" />

<TextView
    android:id="@+id/operational_hour"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/name"
    android:layout_toRightOf="@id/imageView"
    android:text="Operational Hours"
    android:textColor="#000"
    android:textSize="15sp" />

<TextView
    android:id="@+id/contact_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/operational_hour"
    android:layout_toRightOf="@id/imageView"
    android:text="Contact Number"
    android:textColor="#000"
    android:textSize="15sp" />

<TextView
    android:id="@+id/website"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/contact_number"
    android:layout_toRightOf="@id/imageView"
    android:clickable="true"
    android:text="Website"
    android:autoLink="web"
    android:textColor="#000"
    android:textSize="15sp" />

<TextView
    android:id="@+id/address"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/website"
    android:layout_toRightOf="@id/imageView"
    android:clickable="true"
    android:text="Address"
    android:textColor="#000"
    android:textSize="15sp" />

<Button
    android:id="@+id/button_description"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/address"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="5dp"
    android:layout_toRightOf="@id/imageView"
    android:layout_weight="1"
    android:background="@drawable/mybutton"
    android:text="Description"
    android:textColor="#000" />

<Button
    android:id="@+id/button_entrance_fee"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/address"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="5dp"
    android:layout_toRightOf="@id/button_description"
    android:layout_weight="1"
    android:background="@drawable/mybutton"
    android:text="Entrance Fee"
    android:textColor="#000" />

<Button
    android:id="@+id/button_nearest_me"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/button_entrance_fee"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="5dp"
    android:layout_toRightOf="@id/imageView"
    android:layout_weight="1"
    android:background="@drawable/mybutton"
    android:text="Nearest Me"
    android:textColor="#000" />

<Button
    android:id="@+id/button_take_me_there"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/button_entrance_fee"
    android:layout_marginBottom="5dp"
    android:layout_marginRight="5dp"
    android:layout_toRightOf="@id/button_nearest_me"
    android:layout_weight="1"
    android:background="@drawable/mybutton"
    android:text="Take Me There"
    android:textColor="#000" />

</RelativeLayout>

So, how to open new activity from a button which as no .java? I tried to create a new activity and copy paste from the data_layout.xml but it still not working. I hope my explanation is quite clear. and please help me. Thank you.

In your AmusementPark.java file, change

if(false == database.exists()) {

to

if(database.exists == false) {

Also verify the paths are correct. See this very similar article: How do I get a button to open another activity in Android Studio?

Code taken from article:

Manifest file

<activity
   android:name="MyOtherActivity"
   android:label="@string/app_name">
</activity>

Activity.java file

Button btn = (Button)findViewById(R.id.open_activity_button);    

btn.setOnClickListener(new View.OnClickListener() {         
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this, MyOtherActivity.class));
    }

});

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