Hi,
I have a hashmap with key, value pairs example :-
(msisdn,value)
43664xxxxxxx,2
43665xxxxxxx,3
now I want to display this information in a ListView but I don't know
how to feed the data to an ArrayAdapter.
HERE IS MY ADAPTER, note I want to replace myList with data from the
Hashmap a concatentated key+value.
1. Pass the above hashmap as an adapter, replacing myList with the
hashmap
adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList);
setListAdapter(adapter);
Note this is what I have without using the hashmap
THANKS IN ADVANCE
package listmodified.org;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;
public class listmodified extends ListActivity implements
OnGestureListener {
public ArrayList<String> myList = new
ArrayList<String>(Arrays.asList(items));
private TextView selection; // MAIN.xml
public ArrayAdapter<String> adapter; // my adapter
public OnItemLongClickListener itemDelListener;
private GestureDetector gestureScanner;
public int longClickedItem = 0; //check if longClick is selected or
not
private String itemSelected; // for delete function
private static final byte UPDATE_LIST = 100;
public AdapterView<?> parent; //used by OnLitemLongClickListener
public int position;
//tie items to an array list called myList
public static String[] 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
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
OnItemLongClickListener itemDelListener = new
OnItemLongClickListener(){
//@Override
public boolean onItemLongClick(AdapterView<?> parent, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
itemSelected=parent.getItemAtPosition(position).toString();
adapter.remove(itemSelected);
Toast.makeText(listmodified.this, "position is:" +
position,
Toast.LENGTH_SHORT).show();
myList.remove(this); //remove the current object ,
position throws
an exception
adapter.notifyDataSetChanged();
return false;
}};
setContentView(R.layout.main);
//DEFINE MY OWN VIEW TIE TO ARRAYLIST myList WHICH CONTAINS STRINGS
adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,myList);
setListAdapter(adapter);
//A VIEW OF THE LIST NECESSARY FOR DELETION
selection=(TextView)findViewById(R.id.selection);
// PART OF LONG CLICK SELECTED CODE
// CALLS IMPLEMENTED METHODS - detect gestures checking my list items
gestureScanner = new GestureDetector(this);
getListView().setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureScanner.onTouchEvent(event);
}
});
//UPDATE VIEW DELETE WHEN ONLONG CLICK IS PRESSED
getListView().setOnItemLongClickListener(itemDelListener);
}
//LIST ITEM PRESS CHECKING
public void onListItemClick(ListView parent, View v,
int position, long id){
selection.setText(myList.get(position));
//check to see if LONGCLICK IS PRESSED
if (longClickedItem != -1) {
Toast.makeText(listmodified.this, "A short click detected",
Toast.LENGTH_SHORT).show();
}
longClickedItem=0;
}
//IMPLEMENTED BY GESTURE
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float
velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
//CHECKS ONLONGPRESS EVENTS SET LONG PRESS TO -1,
//COOL I CAN USE THIS TO SEE IF A LONG CLICK WAS SELECTED LATER ON
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(listmodified.this, "A long click detected",
Toast.LENGTH_SHORT).show();
if (e.getAction()==MotionEvent.ACTION_DOWN)
{
longClickedItem = -1;
}
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float
distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
private Handler updateListHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATE_LIST:
int position = msg.arg1;
myList.remove(position);
adapter.notifyDataSetChanged();
Toast.makeText(listmodified.this, "OnSingleTapUp",
Toast.LENGTH_SHORT).show();
break;
}
;
};
};
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en