android sqlite 按钮不可点击
android sqlite button unclickable
问候 android 开发人员,我正在学习 android 数据库 sql
并且遇到了问题。我创建了这段代码,构建完成但是当我 运行 模拟器时按钮是 unclickable.
我尝试创建 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();
}
}
}
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>
您似乎没有正确地将 onClick 分配给按钮。
Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes after user presses button
}
});
或者在你 xml 在“< Button >”中做:
android:onClick="methodName()"
例如onClick()
问候 android 开发人员,我正在学习 android 数据库 sql
并且遇到了问题。我创建了这段代码,构建完成但是当我 运行 模拟器时按钮是 unclickable.
我尝试创建 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();
}
}
}
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>
您似乎没有正确地将 onClick 分配给按钮。
Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes after user presses button
}
});
或者在你 xml 在“< Button >”中做:
android:onClick="methodName()"
例如onClick()