Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使文本在android ListView中可见_Android_Xml - Fatal编程技术网

如何使文本在android ListView中可见

如何使文本在android ListView中可见,android,xml,Android,Xml,我正在处理listview,我想让文本在xml文件中的textview中可见。我在列表中添加的列表项正确显示(可见),但没有显示文本。我也希望文本可见,我需要帮助 Xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"

我正在处理listview,我想让文本在xml文件中的textview中可见。我在列表中添加的列表项正确显示(可见),但没有显示文本。我也希望文本可见,我需要帮助

Xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />



    <TextView
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:text="Name:"
        android:visibility="visible"
        android:textSize="17sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/gender"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:text="Gender:"
        android:textColor="#FF00FF"
        android:textSize="17sp"
        android:textStyle="bold"
        android:visibility="visible" />

</LinearLayout>

Java代码: 将数据放入适配器

if (success == 1) {


                    products = json.getJSONArray(TAG_PRODUCTS);


                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);


                        String id = c.getString(TAG_PID);
                        String name = c.getString(TAG_NAME);
                        String gender = c.getString(TAG_GENDER);


                        HashMap<String, String> map = new HashMap<String, String>();


                        map.put(TAG_ID, id);
                        map.put(TAG_NAME, name);
                        map.put(TAG_GENDER, gender);


                        productsList.add(map);
                    }






                public void run() {

                    ListAdapter adapter = new SimpleAdapter(
                            AllPersonActivity.this, productsList,
                            R.layout.list_item, new String[] { TAG_ID,
                                    TAG_NAME, TAG_GENDER},
                            new int[] { R.id.pid, R.id.name, R.id.gender });
                    // updating listview
                    setListAdapter(adapter);
                }
            });
if(成功==1){
products=json.getJSONArray(TAG_products);
对于(int i=0;i
不确定您对哪个
文本视图有问题,但第一个设置

android:visibility="gone"

这将阻止它出现。

我想这是因为线性布局的背景色和内容的字体颜色相同。请尝试以下代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:orientation="vertical" >


<TextView
    android:id="@+id/id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />



<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="6dip"
    android:paddingLeft="6dip"
    android:text="Name:"
    android:visibility="visible"
    android:textColor="@android:color/white"
    android:textSize="17sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/gender"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:paddingLeft="6dip"
    android:paddingTop="6dip"
    android:text="Gender:"
    android:textColor="#FF00FF"
    android:textSize="17sp"
    android:textStyle="bold"
    android:visibility="visible" />


回答我自己的问题只是为了帮助像我这样的人面对问题。我已经找到了解决方案,虽然它不是一个好的解决方案,但对我来说很有效,将其更改为不同的
视图可以以不同的方式显示文本或其他内容。问题是我的代码(适配器)正在替换
xml
TextView
上解析的字符串,所以我在列表中的每个项目上方放置了另一个
TextView

这是我所做的更改


可以使用列表视图中填写的数据附加文本

即, YourTextView.setText(“名称:”+listitem)


像这样。是最简单的方法。

发布相关代码。您可以使用
append
方法将文本附加到textview。请发布适配器代码。我对接下来的两个textview有问题第一个可见性设置为Goe不会在那里显示,但为什么接下来的两个不可见,即使我已经将属性设置为visible?嗯,尝试替换包装内容的所有三个fill\u父项,并为每个TextViews添加一个权重=1。如果我在上述代码中使用了“ListAdapter”,请在将数据放入productsList时将其追加。我不确定。试试看。让我知道它是否有效。祝贺你。你提出的想法也奏效了,所以我找到了第二个解决方案。我使用hasmap
(map.put(TAG_NAME,“NAME:”+NAME);
,然后将其放入
productsList.add(map);
,然后放入适配器。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:visibility="gone" />


    <!--This is to show the label "Name" above the listitem (whatever name) come from java code parsed, json  -->

    <TextView
        android:id="@+id/txtshowname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />


<!-- This TextView will contain the listitems loaded from server -->

     <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="6dip"
        android:paddingLeft="6dip"
        android:visibility="visible"
        android:textColor="@android:color/white"
        android:textSize="17sp"
        android:textStyle="bold" />



 <!--This is to show the label "Gender" above the listitem (whatever gender) come from java code parsed, json  -->
    <TextView
        android:id="@+id/txtshowGender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Gender"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="6dip"
        android:paddingTop="6dip"
        android:textColor="#FF00FF"
        android:textSize="17sp"
        android:textStyle="bold"
        android:visibility="visible" />

</LinearLayout>
map.put(TAG_ID, id);
                    map.put(TAG_NAME, "Name" + name);
                    map.put(TAG_GENDER, "Gender" + gender);


                    productsList.add(map);