gpt4 book ai didi

android sqlite按钮不可点击

转载 作者:行者123 更新时间:2023-11-29 19:09:35 26 4
gpt4 key购买 nike

问候 android 开发人员,我正在学习 android 数据库 sql 并且遇到了问题。我创建了这段代码,构建已完成,但是当我运行模拟器时,按钮不可点击
我尝试创建 toast 以确保按钮可点击,但它也不起作用,想知道代码有什么问题吗?需要帮助谢谢

package com.faddi.sql;  

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import com.faddi.model.DataHelper;

public class MainActivity extends AppCompatActivity implements OnClickListener, OnItemLongClickListener{

ListView listView;
SimpleCursorAdapter adapter;
@SuppressWarnings("deprecation")

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

listView = (ListView) findViewById(R.id.listView1);
listView. setOnItemLongClickListener(this);
findViewById(R.id.tambahButton).setOnClickListener(this);
findViewById(R.id.refreshButton).setOnClickListener(this);


DataHelper dh = new DataHelper(this);
Cursor c = dh.getAll();
String[] from = new String[] { "judul","isi" };
int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
try{
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, from, to);
}catch (Exception ex){}
listView.setAdapter(adapter);

}

protected void onResume() {
adapter.notifyDataSetChanged();
super.onResume();
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.tambahButton:
Toast.makeText(getBaseContext(),"testing",Toast.LENGTH_SHORT).show();
//startActivity(new Intent(this,SecondActivity.class));
break;
case R.id.refreshButton:
DataHelper dh = new DataHelper(this);
Cursor c = dh.getAll();
String[] from = new String[] { "judul","isi" };
int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
try{
adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, c, from, to);
}catch (Exception ex){}
listView.setAdapter(adapter);
break;
default:
break;
}
}

@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
final int id = (int) adapter.getItemId(arg2);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah id="+id+" akan dihapus").setCancelable(true).setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
hapusData(id);
}
}).setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
return false;
}

private void hapusData(long id){
DataHelper dh = new DataHelper(this);
try{
dh.deleteById((int)id);
}catch (Exception ex){
Toast.makeText(this, "Error: "+ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
}

Android Button

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.faddi.sql.MainActivity">

<LinearLayout
android:layout_width="368dp"
android:layout_height="70dp"
android:orientation="horizontal"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">

<Button
android:id="@+id/tambahButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tambah" />

<Button
android:id="@+id/refreshButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Refresh" />
</LinearLayout>

<LinearLayout
android:layout_width="368dp"
android:layout_height="300dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="133dp">

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-51dp" />
</LinearLayout>

</android.support.constraint.ConstraintLayout>

最佳答案

问题是你的 ListView 在按钮之上,你正在使用

tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="133dp"

因此在您的编辑器中,它看起来在中间,但在运行时它会出现在屏幕顶部。

因此,为您的 LinearLayout 按住按钮设置一些 id,并设置 app:layout_constraintTop_toBottomOf 该 id。

像这样改变你的布局,

<LinearLayout
android:id="@+id/topView"
>

<Button
/>

<Button
/>
</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@+id/topView"
android:orientation="vertical">

<ListView
/>
</LinearLayout>

关于android sqlite按钮不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45848459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com