2013-09-25 2 views
1

Я работаю над списком, я хочу, чтобы текст был видимым, который предоставляется ему в текстовом виде в XML-файле. Элементы списка, которые я добавил в списке, отображаются правильно (видимо), но не текст. Я хочу, чтобы текст, видимый, тоже нуждался в помощи для этого.Как сделать текст видимым в андроиде ListView

enter image description here

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); 
       } 
      }); 
+3

Отправьте соответствующий код. –

+0

вы можете добавить текст в текстовое окно, используя метод «append» – Raghunandan

+0

, пожалуйста, отправьте код адаптера. – fasteque

ответ

0

Вы можете добавить текст с данными, которые заполнены в виде списка ,

ie, YourTextView.setText ("Name:" + listitem);

такой. является самым простым способом.

+0

Что в случае с «ListAdapter», которое я использовал в приведенном выше коде? –

+1

добавьте его, когда вы поместите данные в список товаров. я не уверен. просто попробуйте. дайте мне знать, если это сработает. – arjunkn

+0

поздравления. идея, которую вы дали, также сработала, поэтому я нашел второе решение. Я сделал то, что использовал hasmap '(map.put (TAG_NAME,« Name: »+ name);» и после этого я поместил его в 'productsList.add (map);', а затем в адаптер. –

0

Не уверен, который TextView вы испытываете проблемы, но первый один устанавливает

android:visibility="gone" 

, который не позволит ему появиться.

+0

У меня проблема со следующими двумя TextViews, первая видимость настроена на исчезновение, не покажет ее там, но почему следующие два невидимы, даже если я установил свойство в видимое? –

+0

Mmh попробуйте заменить все три fill_parent для wrap_contents и добавить вес = 1 для каждого textView – ssantos

0

ИМХО это связано с тем же цветом фона LinearLayout и цветом шрифта содержимого. Попробуйте следующий код

<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" /> 

0

Отвечая на мой собственный вопрос только, чтобы помочь тем проблемы лицо, как я. Я нашел решение, хотя оно не было хорошим, но работало для меня и меняло его на разные Views, может помочь по-разному показать текст или что-то еще. Проблема заключалась в том, что мой код (адаптер) заменял разборную строку на xmlTextViews, поэтому я положил еще один TextView над каждым элементом, который пришел из списка.

Вот XML я сделал изменения

<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); 
Смежные вопросы