4

我有一个带有键值对示例的哈希图:-

(msisdn,值)

43664xxxxxxx,2 43665xxxxxxx,3

现在我想在 ListView 中显示此信息,但我不知道如何将数据从 Hashmap 提供给 ArrayAdapter ?

这是我的适配器,注意我想用来自 Hashmap 的数据替换 myList 一个连接的键+值。

  1. 将上面的 hashmap 传递给适配器,将 myList 替换为 hashmap

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

谢谢

没有 HASHMAP

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()),假设myHashMapHashMap<String, Something>

于 2012-04-03T19:54:17.460 回答