当前位置: 首页 > 知识库问答 >
问题:

xml文件上的“创建”按钮(不是“新建活动”)并打开“新建活动”

农均
2023-03-14

我是android studio的新手,目前正在创建一个应用程序,它将从SQLite的现有数据库中检索数据。基于我发现的是创建一个布局文件(而不是通过创建新的空活动)来设计如何显示数据。但在布局中,我想要按钮打开新的活动,但不知何故它没有,我没有找到任何解决方案,到目前为止。而影像也没有出现。这是代码:

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;
}
}

游乐场.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;
    }
}
}
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;
}
}
<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>

共有1个答案

夏立果
2023-03-14

在您的AmusementPark.java文件中,更改

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

if(database.exists == false) {

还要验证路径是否正确。看到这篇非常相似的文章:我如何在Android Studio获得一个打开另一个活动的按钮?

<activity
   android:name="MyOtherActivity"
   android:label="@string/app_name">
</activity>
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));
    }
 类似资料:
  • 我是全新的,我唯一的编程经验是PLC; s和HMI的,所以我很抱歉不能弄清楚什么可能是一个简单的任务。但是我找不到任何CURRENT视频或工作示例,说明如何简单地将图像放置在主活动上,并在按下后打开另一个活动。我正在使用最新版本的Android Studio11.0,并试图学习静态编程语言。我已经附上了我到目前为止的项目代码,正如你所看到的,我刚刚开始这个项目。提前感谢您的任何帮助。 Java文件

  • 当我在onSaveInstanceState方法中将数据成功保存到Bundle时,我无法找出为什么onCreate方法中的savedInstanceState总是为null。当我在AVD上运行我的程序并单击“上一步”按钮(销毁活动),然后通过单击其图标再次创建程序时,保存的状态始终为空。下面是一个测试这个问题的简单程序。 如果可能的话,告诉我如何解决这个问题。

  • 单击通知、服务创建新活动和活动创建新服务? 我的应用MainActivity“OnCreate”创建一个“粘性”的服务。 在我的服务中,当有一些通知时,我会发布多个通知。除非用户进入应用程序并阅读通知,否则通知将在那里。在onStartCommand中检查新通知。 按下通知将发送打开MainActivity的意图。 有服务、主要活动和通知。 一段时间后,MainActivity被系统杀死。 单击通

  • 我合并了一个水平幻灯片导航组件(这需要使类扩展片段)。滑动部分工作正常。这里我有相应的<code>onClick() 到目前为止,我的代码还不多,所以我不会用相关的布局部分来混淆我的问题。任何帮助都是非常感谢的。 片段#1的Java代码 public class TasksFragment扩展Fragment { @ Override public View oncreate View(layou

  • 嗨,有人能有一个关于如何使用Fab做一个新活动的建议,比如如果你点击Fab按钮,它将显示一个侧过渡说你好世界?实际上我正在创建一个聊天应用程序,我想用我的联系人取代“你好世界”。 我得到了一个关于我提到的浮动操作按钮的参考链接或源代码。我所做的是删除列表视图,这样屏幕上只有fab按钮,http://www.myandroidsolutions.com/2015/01/01/android-floa

  • 我的应用程序有一个放射群。它有2个单选按钮。 选择单选按钮2 转到电话设置并更改语言。 返回应用程序活动。 再次创建活动。但是选择了radiobutton2。 实际上,当我创建活动时,它应该保留radiobutton1作为默认选择。但是在改变语言并进入我的活动之后,即使运行了onCreate(),它仍然保持以前的状态。 不知何故,状态没有被清除,当我再次创建活动时,我无法将radiobutton1