android sqlite按钮无法点击

时间:2017-08-23 20:06:57

标签: android sqlite android-button toast android-toast

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

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>

3 个答案:

答案 0 :(得分:2)

问题是你的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>

答案 1 :(得分:0)

看起来您没有正确地将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
         }
     });

答案 2 :(得分:0)

或者你在xml中执行“&lt; Button&gt;”:

机器人:的onClick = “方法名()”

E.g。的onClick()