Issue
i´m new to android and im tryin to hide or show a Textview in my items from a listview from JSON.
The idea is to show and to custom a textview with a custom background and style if the date exists.
if date doesn't exist, then hide the textview in that item.
i know tha my be a realy stypid question, but i can't seem to find the right answer anyware.
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>
Solution
First, you need to change your adapter to another, for example ArrayAdapter. Inside getView method you can describe the logic for displaying what you need depending on the data. Below I showed an example, based on your datatype.
public class TestAdapter extends ArrayAdapter<HashMap<String, String>> {
public TestAdapter(Context context, ArrayList<HashMap<String, String>> data) {
super(context, 0, data);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_anuncio_item_listview, parent, false);
}
/*getting the model one by one*/
HashMap<String, String> item = getItem(position);
/*we get some view*/
TextView textDescription = (TextView) convertView.findViewById(R.id.tv_descripcion);
/*we get some value from model*/
String testValue = item.get("tet_key");
/*check if empty then hide the view*/
if (testValue == null) {
textDescription.setVisibility(View.GONE);
} else {
textDescription.setText(testValue);
textDescription.setVisibility(View.VISIBLE);
}
return convertView;
}
}
inside MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> model1 = new HashMap<>();
HashMap<String, String> model2 = new HashMap<>();
model2.put("tet_key", "test");
list.add(model1);
list.add(model2);
ListView listView = findViewById(R.id.listView_anunt);
TestAdapter adapter = new TestAdapter(this, list);
listView.setAdapter(adapter);
}
}
result
PS.I advise you to use instead of ListView -> RecyclerView, and also instead of HashMap<> it is better to use some model with fields and convert the data from the server to this model using GSON.
Answered By - Yura
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.