我在开发时遇到了麻烦,这是我第一个使用 Eclipse JUNO 的 android 应用程序。我想用正确分配的公共静态变量填充 ListActivity 中的 Listview(使用调试器进行检查)。问题是列表视图向我显示了正确的行数,但数据为空白。
他是我的代码:
格里拉.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();
}
}
});
}
当我单击列表视图中的项目时,它会返回正确的值。
谢谢!
贡萨洛。