gpt4 book ai didi

android - 如何在 ArrayAdapter 中使用 HashMap 中的键和值?

转载 作者:太空宇宙 更新时间:2023-11-03 13:02:02 25 4
gpt4 key购买 nike

我有一个带有键值对示例的 HashMap :-

(msisdn,值)

43664xxxxxxx,243665xxxxxxx,3

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

这是我的适配器,注意我想用来自 HashMap 一个连接的键+值。

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


    适配器=新
    ArrayAdapter(这个,android.R.layout.simple_list_item_1,myList);
    设置列表适配器(适配器);

谢谢

没有 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;

}
;
};
};


}

最佳答案

I don't know how to feed the data to an ArrayAdapter from the Hashmap ?

使用 new ArrayList<String>(myHashMap.keySet()) ,假设 myHashMapHashMap<String, Something> .

关于android - 如何在 ArrayAdapter 中使用 HashMap 中的键和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10000108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com