4

キーと値のペアの例を含むハッシュマップがあります:-

(msisdn、value)

43664xxxxxxx,2 43665xxxxxxx,3

この情報をListViewに表示したいのですが、ハッシュマップからArrayAdapterにデータをフィードする方法がわかりません。

これが私のアダプターです。myListをハッシュマップからの連結されたキー+値のデータに置き換えたいことに注意してください。

  1. 上記のハッシュマップをアダプタに渡し、myListをハッシュマップに置き換えます

    adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,myList); setListAdapter(adapter);

ありがとう

ハッシュマップなし

packagelistmodified.org;

importjava.util.Arrays;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;

importandroid.app.ListActivity;
importandroid.os.Bundle;
importandroid.os.Handler;
importandroid.os.Message;
importandroid.view.View;
importandroid.widget.AdapterView;
importandroid.widget.ArrayAdapter;
importandroid.widget.ListView;
importandroid.widget.TextView;
importandroid.widget.AdapterView.OnItemLongClickListener;
importandroid.view.GestureDetector.OnGestureListener;
importandroid.view.GestureDetector;
importandroid.view.MotionEvent;
importandroid.widget.Toast;




publicclasslistmodifiedextendsListActivityimplements
OnGestureListener{
publicArrayList<String>myList=new
ArrayList<String>(Arrays.asList(items));
privateTextViewselection;//MAIN.xml
publicArrayAdapter<String>adapter;//myadapter
publicOnItemLongClickListeneritemDelListener;
privateGestureDetectorgestureScanner;
publicintlongClickedItem=0;//checkiflongClickisselectedor
not
privateStringitemSelected;//fordeletefunction
privatestaticfinalbyteUPDATE_LIST=100;
publicAdapterView<?>parent;//usedbyOnLitemLongClickListener
publicintposition;

//tieitemstoanarraylistcalledmyList
publicstaticString[]items={"lorem","ipsum","dolor",
"sit","amet",
"consectetuer","adipiscing","elit","morbi","vel",
"ligula","vitae","arcu","aliquet","mollis",
"etiam","vel","erat","placerat","ante",
"porttitor","sodales","pellentesque","augue","purus"};

@Override
publicvoidonCreate(Bundleicicle){
super.onCreate(icicle);

OnItemLongClickListeneritemDelListener=new
OnItemLongClickListener(){

//@Override
publicbooleanonItemLongClick(AdapterView<?>parent,Viewarg1,
intposition,longarg3){
//TODOAuto-generatedmethodstub
itemSelected=parent.getItemAtPosition(position).toString();
adapter.remove(itemSelected);
Toast.makeText(listmodified.this,"positionis:"+position,
Toast.LENGTH_SHORT).show();
myList.remove(this);//removethecurrentobject,positionthrows
anexception
adapter.notifyDataSetChanged();


returnfalse;
}};

setContentView(R.layout.main);


//DEFINEMYOWNVIEWTIETOARRAYLISTmyListWHICHCONTAINSSTRINGS
adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList);
setListAdapter(adapter);

//AVIEWOFTHELISTNECESSARYFORDELETION

selection=(TextView)findViewById(R.id.selection);


//PARTOFLONGCLICKSELECTEDCODE
//CALLSIMPLEMENTEDMETHODS-detectgesturescheckingmylistitems
gestureScanner=newGestureDetector(this);
getListView().setOnTouchListener(newView.OnTouchListener(){
@Override
publicbooleanonTouch(Viewv,MotionEventevent){
returngestureScanner.onTouchEvent(event);
}
});

//UPDATEVIEWDELETEWHENONLONGCLICKISPRESSED
getListView().setOnItemLongClickListener(itemDelListener);

}


//LISTITEMPRESSCHECKING
publicvoidonListItemClick(ListViewparent,Viewv,
intposition,longid){
selection.setText(myList.get(position));
//checktoseeifLONGCLICKISPRESSED
if(longClickedItem!=-1){
Toast.makeText(listmodified.this,"Ashortclickdetected",
Toast.LENGTH_SHORT).show();

}
longClickedItem=0;
}

//IMPLEMENTEDBYGESTURE
@Override
publicbooleanonDown(MotionEventarg0){
//TODOAuto-generatedmethodstub
returnfalse;
}


@Override
publicbooleanonFling(MotionEvente1,MotionEvente2,float
velocityX,
floatvelocityY){
//TODOAuto-generatedmethodstub
returnfalse;
}


//CHECKSONLONGPRESSEVENTSSETLONGPRESSTO-1,
//COOLICANUSETHISTOSEEIFALONGCLICKWASSELECTEDLATERON

@Override
publicvoidonLongPress(MotionEvente){
//TODOAuto-generatedmethodstub
Toast.makeText(listmodified.this,"Alongclickdetected",
Toast.LENGTH_SHORT).show();


if(e.getAction()==MotionEvent.ACTION_DOWN)
{

longClickedItem=-1;

}

}


@Override
publicbooleanonScroll(MotionEvente1,MotionEvente2,float
distanceX,
floatdistanceY){
//TODOAuto-generatedmethodstub
returnfalse;
}


@Override
publicvoidonShowPress(MotionEvente){
//TODOAuto-generatedmethodstub

}


@Override
publicbooleanonSingleTapUp(MotionEvente){
//TODOAuto-generatedmethodstub
returnfalse;
}
privateHandlerupdateListHandler=newHandler(){
@Override
publicvoidhandleMessage(Messagemsg){
switch(msg.what){
caseUPDATE_LIST:
intposition=msg.arg1;
myList.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(listmodified.this,"OnSingleTapUp",
Toast.LENGTH_SHORT).show();
break;

}
;
};
};


}
4

1 に答える 1

6

Hashmap から ArrayAdapter にデータをフィードする方法がわかりません。

new ArrayList<String>(myHashMap.keySet())であると仮定しmyHashMapて、 を使用しHashMap<String, Something>ます。

于 2012-04-03T19:54:17.460 に答える