simple_items_list_2レイアウトを使用して2アイテムリストを表示するSimpleAdapterを備えたListActivityがあります。HashMapのArrayListはアイテムを保持します。
リストには、異なるからのUDPパケットとして受信したデータを保持する必要があります。したがって、これらのパケットが受信される別のスレッドがあります。そこから、ハンドラーを使用して、受信したデータを送信し、アイテムをリストに追加します。
これでパケットを正しく受信し、リストも生成されます。ただし、たとえばアイテムBを選択すると、アイテムAが選択されることがあります。
コードスニペットは次のとおりです。
OnCreate()で、
lv = getListView();
list = new ArrayList<HashMap<String, String>>();
String[] from = { "name", "address" };
int[] to = { android.R.id.text1, android.R.id.text2 };
adapter = new SimpleAdapter(getApplicationContext(), list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
スレッドからの内容を含むメッセージを受け取った後のハンドラーのコード:
list.add(putData(scanned_name, scanned_addr));
adapter.notifyDataSetChanged();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
TextView name_tv = (TextView) findViewById(android.R.id.text1);
TextView addr_tv = (TextView) findViewById(android.R.id.text2);
selectedName = name_tv.getText().toString();
selectedAddr = addr_tv.getText().toString();
データを配置するHashMap関数:
private HashMap<String, String> putData(String name, String address) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("address", address);
return item;
}
何か助けはありますか?