2012-12-05 32 views
1

我遇到了麻煩與開發,這是我與Eclipse JUNO的第一個Android應用程序。 我想在ListActivity中使用公共靜態變量填充Listview,並正確指定(使用調試器進行檢查)。 問題是,listview向我顯示正確的行數,但空白數據。Android:與空白數據的ListView

他是我的代碼:

Grilla.java

import android.os.Bundle; 
import android.app.ListActivity; 
import android.view.View; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 

public class Grilla extends ListActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     try 
     { 
      SimpleAdapter adapter = new SimpleAdapter(Grilla.this, VariablesPublicas.listado_grilla, R.layout.mylistrow, 
        VariablesPublicas.ColumnTags, new int[] {R.id.column1, R.id.column2}); 
      setListAdapter(adapter); 

      //getListView().setTextFilterEnabled(true); 
     } 
     catch(Exception e) { 
      System.out.println(e.toString()); 
     } 
    } 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
      super.onListItemClick(l, v, position, id); 
      Object o = l.getItemAtPosition(position); 
      VariablesPublicas.itemSeleccionado = o.toString(); 
      finish(); 

    } 

} 

mylistrow.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="fill_parent"> 

    <TextView 
     android:id="@+id/column1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="1" 
     android:visibility="invisible" /> 

    <TextView 
     android:id="@+id/column2" 
     android:layout_width="fill_parent" 
     android:layout_height="34dp" 
     android:text="2" /> 

</LinearLayout> 

activity_grilla.xml(Grilla.java)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ListView 
     android:id="@+id/listView_grilla" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     tools:listitem="@layout/mylistrow" > 

    </ListView> 

</LinearLayout> 

主要應用:

public class Main extends Activity { 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final Button btn_buscarobra = (Button) findViewById(R.id.btn_buscarobra); 

     btn_buscarobra.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
       servicioweb WS = new servicioweb(); 
       try { 
        String obras = WS.ObtenerObras(""); 
        org.w3c.dom.Document doc = WS.XMLfromString(obras); 

        NodeList nodes = doc.getElementsByTagName("entry"); 

        //fill in the list items from the XML document 
        VariablesPublicas.listado_grilla = new ArrayList<HashMap<String, String>>(); 

        for (int i = 0; i < nodes.getLength(); i++) { 
         Element e = (Element)nodes.item(i); 
         HashMap<String, String> map = new HashMap<String, String>(); 
         map.put("id_obra", servicioweb.getValue(e, "IdObra")); 
         map.put("obra", servicioweb.getValue(e, "Obra")); 
         //VariablesPublicas.listado_grilla.(new String[] {servicioweb.getValue(e, "IdObra"), 
         //  }); 
         VariablesPublicas.listado_grilla.add(map); 

        } 
        VariablesPublicas.ColumnTags = new String[] {"#", "OBRA"}; 
        Intent listaObras = new Intent(Main.this, Grilla.class); 
        startActivityForResult(listaObras,0); 

       } catch (Exception e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

當我在列表視圖中單擊一個項目時,它返回正確的值。

謝謝!

貢薩洛。

+1

你有你的第一個TextView的設置爲不可見;也許那個文本足夠長,可以將第二個文本排除在外,所以你什麼都看不到?如果你刪除了'android:visibility =「invisible」'這一行,你會看到任何文本嗎? – dokkaebi

+0

第一個「列」包含ID(這就是爲什麼我設置爲不可見),如果我設置爲可見,結果相同。 – Gonzalo

回答

0

您的密鑰不匹配。

VariablesPublicas.ColumnTags中的鍵應與地圖中的鍵相對應。

二者必選其一:

map.put("id_obra", servicioweb.getValue(e, "IdObra")); 
map.put("obra", servicioweb.getValue(e, "Obra")); 
... 
VariablesPublicas.ColumnTags = new String[] {"id_obra", "obra"}; 

或:

map.put("#", servicioweb.getValue(e, "IdObra")); 
map.put("OBRA", servicioweb.getValue(e, "Obra")); 
... 
VariablesPublicas.ColumnTags = new String[] {"#", "OBRA"}; 
+0

完美!這是解決方案!謝謝!!!!! – Gonzalo