我有一个 android 应用程序,我从数据库中加载。在下面的链接中,我问为什么我的代码给了我一个错误,因为带有查询数据的表没有正确加载。
根据 A.Cedano 的回答更改我的代码后,程序没有做出应有的响应。我的代码如下:
@Override
public void onDateChanged(DatePicker view, int year, int month, int day){
String username = i.getStringExtra("us_usuario");
String password = i.getStringExtra("us_clave");
int Año = calendario.getYear();
int Mes = calendario.getMonth()+1;
int Dia = calendario.getDayOfMonth();
String fecha = Año+"-"+Mes+"-"+Dia;
Response.Listener<String> responseListener = new Response.Listener<String>(){
@Override
public void onResponse(String response){
ArrayList<String> arrayRespuesta = new ArrayList<>();
try {
JSONArray jsonArray = new JSONArray(response);
String hora_inicio, fecha, nombre, apellidos, prestacion;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
hora_inicio = jsonObject.getString("hora_inicio");
fecha = jsonObject.getString("fecha");
nombre = jsonObject.getString("nombre");
apellidos = jsonObject.getString("apellidos");
prestacion = jsonObject.getString("prestacion");
int[] comprobarFecha = separarFecha(fecha);
if(comprobarFecha[0] == calendario.getYear() && comprobarFecha[1] == calendario.getMonth()+1 && comprobarFecha[2] == calendario.getDayOfMonth()){
arrayRespuesta.add(hora_inicio);
arrayRespuesta.add(nombre);
arrayRespuesta.add(apellidos);
arrayRespuesta.add(prestacion);
}
}
}catch(JSONException e){
tablePaciente.removeAllViews();
Toast t = Toast.makeText(getApplicationContext(), "No hay pacientes para esta fecha", Toast.LENGTH_LONG);
t.show();
}catch(ArrayIndexOutOfBoundsException e){
tablePaciente.removeAllViews();
AlertDialog.Builder builder = new AlertDialog.Builder(Usuario.this);
builder.setMessage("Error de excepción en el array "+e.getLocalizedMessage())
.setNegativeButton("Retry", null)
.create().show();
}
if(!arrayRespuesta.isEmpty()){
tablePaciente.removeAllViews();
TablaPacientes tabla = new TablaPacientes(Usuario.this, tablePaciente);
tabla.agregarCabecera(R.array.tabla_pacientes);
agregarFila(arrayRespuesta);
}else{
tablePaciente.removeAllViews();
Toast t = Toast.makeText(getApplicationContext(), "No hay pacientes para esta fecha", Toast.LENGTH_LONG);
t.show();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(username, password, fecha, responseListener);
RequestQueue queue = Volley.newRequestQueue(Usuario.this);
queue.add(registerRequest);
}
addRow 方法如下,在同一个类中进一步向下(为此,我使用了以下问题中的信息:在 Android 中动态创建列):
public void agregarFila(ArrayList<String> elementos){
int num_cols = 4;
TableRow.LayoutParams layoutCelda;
tablePaciente = (TableLayout) findViewById(R.id.tablePaciente);
TableRow fila = new TableRow(this);
for(int i=0; i<elementos.size(); i++){
TextView texto = new TextView(this);
texto.setText(String.valueOf(elementos.get(i)));
texto.setGravity(Gravity.START);
layoutCelda = new TableRow.LayoutParams(obtenerAnchoPixelesTexto(texto.getText().toString()), TableRow.LayoutParams.WRAP_CONTENT);
texto.setLayoutParams(layoutCelda);
fila.addView(texto);
for (int j=0; j<num_cols; j++){
TableRow tableRow = new TableRow(this);
tablePaciente.addView(tableRow);
}
}
tablePaciente.addView(fila);
}
json给出的结果如下:
[
{
"us_usuario": "DONATE",
"us_clave": "DONATE",
"hora_inicio": "14:00",
"hora_fin": "14:15",
"fecha": "2017-10-29",
"nombre": "JACINTO",
"apellidos": "JARAMILLO ROMERO",
"prestacion": "FOTOCOAGULACION DR"
},
{
"us_usuario": "DONATE",
"us_clave": "DONATE",
"hora_inicio": "18:20",
"hora_fin": "18:35",
"fecha": "2017-10-29",
"nombre": "JUAN MANUEL",
"apellidos": "LLORENTE RODRIGO",
"prestacion": "REVISION OFTALMOLOGICA"
}
]
但是,它只显示第一个查询,即当我选择 10 月 29 日的日期时,它只显示 JACINTO 数据,如下图所示:
知道如何让它向我展示所有查询吗?
(编辑自@A.Cedano 的回答)
创建 RecyclerViewAdapterUser 类时,RecyclerView 类无法识别我。
我的 Gradle 文件的代码如下(我的最低版本是 15,最高版本是 26)
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.tigestion.guillermo.prueba"
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.1'
}
我是否必须导入一些特殊的东西或做一些事情才能让 RecyclerView 识别我?
好的,解决了 RecyclerView 错误,通过 Build->Clean Project 然后 Build->Rebuild Project 解决。
Android 允许实现
RecyclerView
这种类型的操作。它的使用打开了一个充满可能性的世界,现在解释起来会很长,但你以后可以深入研究它。我将根据这种方法向您提出建议,而不会过多地关注您如何获取数据的细节。
步骤 0:需要配置
RecyclerView
它是 V7 支持库的一部分。gradle
要使其正常工作,您需要在app文件中添加此依赖项。您不必添加其他dependencies
部分,但查找该部分并放入以下行:compile 'com.android.support:recyclerview-v7:21.0.+'
。有关更多兼容性详细信息,您可以参考此 Android 链接。
步骤1:
我们将调用您要在其中显示表格的活动
TablaActivity
。我们将创建一个
activity_tabla
包含以下内容的布局RecyclerView
:第2步
我们创建一个包含表格不同列的布局。在此示例中,我们将有一个包含两列的布局。我们会打电话给你
table_layout
。这是一个线性布局内的两个 textView:第 3 步:
让我们创建一个数据模型。
数据模型是一种强烈推荐的做法。通常,数据模型代表我们应用程序的完整实体,例如,数据库表。
在任何情况下,处理数据模型的便利性都令人印象深刻,例如使用 Gson 库,您可以将 JSON 对象
Usuarios
直接放入数据模型中。对于他们,
RecyclerView
我们还将看到它们的重要性以及它们为代码带来的清晰度。让我们想象一个简单的数据模型......然后你可以适应它。
该模型将具有您想要的所有方法,因此您可以将其用于与用户相关的所有内容。
第4步:
我们将创建一个适配器,作为它
RecyclerView
和我们的数据模型之间的中介:第 5 步:
最后!现在好东西来了。
请注意,以上所有内容都是我们将在我们的应用程序中修复的。现在我们将获得如此努力的好处。
这将是我在步骤 1
TablaActivity
中提到的主要活动的模型。虽然我们可以从应用程序的任何其他部分实现此代码,我们需要通过将它们呈现在表格中来处理它。Usuarios
结果是一个用户表……点击其中任何一个都会在烤面包机中显示他们的 ID。
可以修改此代码,例如在单击特定用户时显示特定用户的更多详细信息,显示 a
CardView
,从表中添加或删除用户等。这种获取和使用数据的方式的可能性是巨大的。
链接:
关于这个主题有很多有趣的教程。这里有一些: