【发布时间】:2022-01-07 15:20:47
【问题描述】:
我是 android 新手,我试图从 JSON 的列表视图中隐藏或显示我的项目中的 Textview。
如果日期存在,想法是显示和自定义具有自定义背景和样式的文本视图。
如果日期不存在,则隐藏该项目中的文本视图。
我知道这是一个非常愚蠢的问题,但我似乎无法找到正确的答案。
PrimerActivity.JAVA
public void showJSON(String response) {
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Search_data_anuncios.JSON_ARRAY);
for (int i = 0; i < result.length(); i++) {
JSONObject jo = result.getJSONObject(i);
String titulo = jo.getString(Search_data_anuncios.KEY_titulo_anuncio_cliente);
String descripcion = jo.getString(Search_data_anuncios.KEY_descripcion_anuncio_cliente);
String fecha_creacion = jo.getString(Search_data_anuncios.KEY_fecha_creacion_anuncio_cliente);
String categoria_anuncio_cliente = jo.getString(Search_data_anuncios.KEY_categoria_anuncio_cliente);
Log.i(TAG_activity, "AQUI - " + categoria_anuncio_cliente);
//change_img_item(categoria_anuncio_cliente, i);
final HashMap<String, String> oferta = new HashMap<>();
oferta.put(Search_data_anuncios.KEY_titulo_anuncio_cliente, titulo);
oferta.put(Search_data_anuncios.KEY_descripcion_anuncio_cliente, descripcion);
oferta.put(Search_data_anuncios.KEY_fecha_creacion_anuncio_cliente, fecha_creacion);
oferta.put(Search_data_anuncios.KEY_categoria_anuncio_cliente, categoria_anuncio_cliente);
list.add(oferta);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(
PrimerActivity.this, list, R.layout.activity_anuncio_item_listview,
new String[]{Search_data_anuncios.KEY_titulo_anuncio_cliente, Search_data_anuncios.KEY_descripcion_anuncio_cliente, Search_data_anuncios.KEY_fecha_creacion_anuncio_cliente, Search_data_anuncios.KEY_categoria_anuncio_cliente},
new int[]{R.id.tv_titulo, R.id.tv_descripcion, R.id.tvclass});
anunt.setAdapter(adapter);}
}
ACTIVITY_PRIMER.XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<ListView
android:id="@+id/listView_anunt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="2dp"
android:layout_marginBottom="57dp"
android:dividerHeight="10dp">
</ListView>
</RelativeLayout>
activity_anuncio_item_listview.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:outlineAmbientShadowColor="@color/black"
android:paddingBottom="0dp">
<LinearLayout
android:layout_width="111dp"
android:layout_height="83dp">
<ImageView
android:id="@+id/item_listview_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="-20dp"
android:src="@drawable/recuest2" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_titulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="txt tit"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Light.SearchResult.Title" />
<TextView
android:id="@+id/tv_descripcion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="txt des" />
<TextView
android:id="@+id/tvclass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="txt dat"
android:textAppearance="@style/TextAppearance.AppCompat" />
</LinearLayout>
</LinearLayout>
【问题讨论】:
标签: android android-studio android-listview android-listadapter