【问题标题】:How to hide a TextView from Specific listview item如何从特定的列表视图项中隐藏 TextView
【发布时间】:2022-01-07 15:20:47
【问题描述】:

我是 android 新手,我试图从 JSON 的列表视图中隐藏或显示我的项目中的 Textview。

如果日期存在,想法是显示和自定义具有自定义背景和样式的文本视图。

如果日期不存在,则隐藏该项目中的文本视图。

我知道这是一个非常愚蠢的问题,但我似乎无法找到正确的答案。

se here the example

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


    【解决方案1】:

    首先,您需要将您的适配器更改为另一个,例如 ArrayAdapter。在 getView 方法中,您可以描述根据数据显示所需内容的逻辑。下面我根据您的数据类型展示了一个示例。

    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;
        }
    }
    

    在 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);
        }
    }
    

    结果

    PS.我建议你使用代替 ListView -> RecyclerView 也代替 HashMap> 最好使用一些带有字段的模型,并使用 GSON 将来自服务器的数据转换为该模型。

    【讨论】:

    • WOORKkkSSSSS!!!谢谢!!! ?????
    猜你喜欢
    • 2019-12-31
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2015-01-15
    • 1970-01-01
    • 2011-06-01
    • 1970-01-01
    相关资源
    最近更新 更多