2

これは私が働いている大学のために書いている最初のAndroidアプリです。これは基本的に、サイトのxmlからデータを読み取り、それをリストに出力するシンクライアントです。しかし、私は奇妙な問題に遭遇し、いくつかの助けを使うことができました。EventsListActivityは、ファイルからxmlを読み取り、それをHashMapsの配列に配置し、並べ替えて、SimpleAdapterに配置します。DirectoryListActivityにこれとまったく同じコードを使用していますが、問題なく機能します。ただし、EventsListActivityは、1行の空白のリストのみを表示します。その行をクリックすると正しいデータが表示され、xmlファイルには1行しかないため、問題はありません。ただし、行のTextViewは何も表示していません。

コードをステップスルーしましたが、xmlが正しく読み取られて保存されています。SimpleAdapterのバインディングに問題があるようです。どんなヒントでも大歓迎です。私のコードで私ができるより良いことを見つけたら、遠慮なく私にポインタを教えてください。必要な情報は次のとおりです。

public class KCCEventsListActivity extends ListActivity {
    public static final ArrayList<HashMap<String, String>> eventsList = new ArrayList<HashMap<String, String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.customlist);
        SimpleAdapter adapter = new SimpleAdapter(KCCEventsListActivity.this, eventsList, R.layout.customrow, new String[] {"summary", "date"}, new int[] {R.id.tvHeading, R.id.tvSubheading});

        if (eventsList.isEmpty()) {
            getEvents();
        }

        KCCEventsListActivity.this.setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView lv, View v, int position, long id) {
        super.onListItemClick(lv, v, position, id);

        HashMap<String, String> event = eventsList.get(position);
        Toast.makeText(KCCEventsListActivity.this, event.get("date"), Toast.LENGTH_LONG).show();
        //Call AddtoCalendar Intent
    }

    protected void getEvents() {
        try {
            URL url = new URL(getString(R.string.KccEventsAddress));
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("EventItem");

            for (int i=0; i < nodeList.getLength(); i++) {
                Node node = nodeList.item(i);
                Element element = (Element)node;

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

                NodeList titleList = element.getElementsByTagName("Summary");
                Element titleElement = (Element)titleList.item(0);
                titleList = titleElement.getChildNodes();
                entry.put("summary", titleList.item(0).getNodeValue());

                NodeList dateList = element.getElementsByTagName("StartTime");
                Element dateElement = (Element)dateList.item(0);
                dateList = dateElement.getChildNodes();
                entry.put("date", dateList.item(0).getNodeValue());

                NodeList orderList = element.getElementsByTagName("Seconds");
                Element orderElement = (Element)orderList.item(0);
                orderList = orderElement.getChildNodes();
                entry.put("order", orderList.item(0).getNodeValue());

                eventsList.add(entry);
            }

            Comparator<HashMap<String, String>> comparator = new Comparator<HashMap<String, String>>() {

                public int compare(HashMap<String, String> object1, HashMap<String, String> object2) 
                {       
                        return object1.get("order").compareToIgnoreCase(object2.get("order"));
                }
            };      
            Collections.sort(eventsList, comparator);

        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
}

CUSTOMLIST.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView android:id="@id/android:list"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"></ListView>
  <TextView android:id="@id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:text="No data" />
</LinearLayout>

CUSTOMROW.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:id="@+id/tvHeading" android:layout_width="wrap_content" android:layout_height="26dip" android:layout_alignParentTop="true" android:textSize="20sp" />
<TextView android:id="@+id/tvSubheading" android:layout_width="wrap_content" android:layout_height="22dip" android:layout_below="@id/tvHeading" android:textSize="16sp" />
</RelativeLayout>
4

3 に答える 3

1

これらの行を置き換えてみてください。

SimpleAdapter adapter = new SimpleAdapter(KCCEventsListActivity.this, eventsList, R.layout.customrow, new String[] {"summary", "date"}, new int[] {R.id.tvHeading, R.id.tvSubheading});
KCCEventsListActivity.this.setListAdapter(adapter);

これらの行について:

SimpleAdapter adapter = new SimpleAdapter(this, eventsList, R.layout.customrow, new String[] {"summary", "date"}, new int[] {R.id.tvHeading, R.id.tvSubheading});
setListAdapter(adapter)
于 2011-06-20T21:27:39.273 に答える
1

コードを何度か調べた後、最終的に Web サービスを書き直して、別のスタイルの xml を出力しました。これにより、コードが完全に機能しました。何らかの理由で、以前に使用された xml ファイルが原因で、表示が正しく機能しませんでした。なぜこのようなことが起こったのかについて、もっと情報があればいいのにと思います。答えてくれてありがとう。

于 2011-06-22T15:51:48.707 に答える
1

アダプタを作成する前に HashMap にデータを入力するか、呼び出すかを試しましたか

adapter.notifyDataSetChanged()

バッキング データが更新されたときに、アダプターに ping を実行する必要があります。

于 2011-06-20T22:33:03.873 に答える