我正在尝试使用嵌入式数据库创建基本评级应用程序。不幸的是,每当我尝试将onClickListener编程到我的reportSomeoneButton时,应用程序崩溃或者事件没有被正确调用,按钮什么都不做。
预期的操作是,有人点击reportSomeoneButton,这会创建一个警告对话框,询问用户输入。用户输入注释,然后将信息存储在数据库中并填充下表。
我现在一直在努力奋斗好几个小时。教程没有达到我正在寻找的东西,有关stackoverflow的其他问题有助于学习不同的东西,但我似乎无法解决这个问题。我对使用Android Studio进行开发非常陌生,所以任何形式的帮助都会受到高度赞赏,无论是完美的解决方案,还是仅仅通过我的研究指向我正确的方向,我们都将不胜感激!
无论如何,这就是我所拥有的:
main activity.Java
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Calendar;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "Welcome!";
//private Reports currentReport;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view)
{
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText2);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
activity_main.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"
android:minWidth="0dp"
tools:context="com.example.feral.myapplication.MainActivity">
<EditText
android:id="@+id/editText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:ems="10"
android:hint="@string/edit_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="20dp"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintBaseline_toBaselineOf="@+id/editText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/editText2"
android:layout_marginLeft="20dp"
android:layout_marginRight="16dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/honkhonklogo" />
</android.support.constraint.ConstraintLayout>
display message activity.Java
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Calendar;
public class DisplayMessageActivity extends AppCompatActivity
{
public String currentDate;
private RatingDBHelper dbAssistant;
private ListView listHandler;
private ArrayAdapter<String> arrayHandler;
private static final String TAG = "MainActivity";
private TextView getCurrentDate;
private DatePickerDialog.OnDateSetListener dateListener;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = findViewById(R.id.textView);
textView.setText("Welcome, " + message + "!");
getCurrentDate = (TextView) findViewById(R.id.textView);
getCurrentDate.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
//int date = calendar.get(Calendar.DAY_OF_WEEK);
DatePickerDialog dialog = new DatePickerDialog(
DisplayMessageActivity.this,
android.R.style.Theme_DeviceDefault_Dialog_MinWidth,
dateListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0xFFFF6666));
dialog.show();
}
});
dateListener = new DatePickerDialog.OnDateSetListener()
{
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day)
{
month = month + 1;
Log.d(TAG, "onDateSet: mm/dd/yyyy: " + month + "/" + day + "/" + year);
String submittedDate = month + "/" + day + "/" + year;
if (!(submittedDate != null && !submittedDate.isEmpty()))
{
currentDate = submittedDate;
}
else
{
currentDate = submittedDate;
}
getCurrentDate.setText(submittedDate);
}
};
dbAssistant = new RatingDBHelper(this);
listHandler = (ListView) findViewById(R.id.reportedRatings);
refreshList();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.reportSomeoneButton:
final EditText reportInput = new EditText(this);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Report a Driver")
.setMessage("License Plate #: ")
.setView(reportInput)
.setPositiveButton("Submit", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int i)
{
String licensePlate = String.valueOf(reportInput.getText());
SQLiteDatabase db = dbAssistant.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ReportDataSource.ReportSubmission.COL_TASK_TITLE, licensePlate + "\n" + currentDate);
db.insertWithOnConflict(ReportDataSource.ReportSubmission.TABLE,
null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
refreshList();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void refreshList()
{
ArrayList<String> reportList = new ArrayList<>();
SQLiteDatabase db = dbAssistant.getReadableDatabase();
Cursor cursor = db.query(ReportDataSource.ReportSubmission.TABLE,
new String[] {ReportDataSource.ReportSubmission._ID, ReportDataSource.ReportSubmission.COL_TASK_TITLE},
null, null, null, null,null);
while (cursor.moveToNext())
{
int idx = cursor.getColumnIndex(ReportDataSource.ReportSubmission.COL_TASK_TITLE);
reportList.add(cursor.getString(idx));
}
if(arrayHandler == null)
{
arrayHandler = new ArrayAdapter<>(this,
R.layout.activity_display_message,
R.id.textView,
reportList);
listHandler.setAdapter(arrayHandler);
}
else
{
arrayHandler.clear();
arrayHandler.addAll(reportList);
arrayHandler.notifyDataSetChanged();
}
cursor.close();
db.close();
}
}
activity_display_message.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.example.feral.myapplication.DisplayMessageActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/reportSomeoneButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="@string/reportSomeone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ratingBar" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="86dp"
android:layout_height="55dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:cropToPadding="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:srcCompat="@drawable/blankprofile" />
<RatingBar
android:id="@+id/ratingBar"
style="@style/Widget.AppCompat.RatingBar"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:isIndicator="false"
android:stepSize="1"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView3" />
<ListView
android:id="@+id/reportedRatings"
android:layout_width="0dp"
android:layout_height="339dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/reportSomeoneButton" />
</android.support.constraint.ConstraintLayout>
我也有DBHelper,数据库源代码文件,但我不相信它们与这个问题有关。
再次,谢谢大家的帮助!
首先,在OnCreate()中初始化reportsSomeButton。而不是在OnOptionsItemSelected(..)内部调用,在onCreate()中尝试这个。
reportSomeoneButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Report a Driver")
.setMessage("License Plate #: ")
.setView(reportInput)
.setPositiveButton("Submit", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int i)
{
String licensePlate = String.valueOf(reportInput.getText());
SQLiteDatabase db = dbAssistant.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ReportDataSource.ReportSubmission.COL_TASK_TITLE, licensePlate + "\n" + currentDate);
db.insertWithOnConflict(ReportDataSource.ReportSubmission.TABLE,
null, values, SQLiteDatabase.CONFLICT_REPLACE);
db.close();
refreshList();
}
})
.setNegativeButton("Cancel", null)
.create();
dialog.show();
}
});
希望这可以帮到你。