Get a single field from multi-column ListView using SimpleAdapter - android

I have a ListActivity in which i have used a SimpleAdapter to create a list which has 2 fields. The pair of values are stored using a Map, and the list is an ArrayList.
I have an onItemClickListener for this. On selecting a list entry, i get the pair i.e. the 2 values. I need to get only 1 of those values.
For example, if the list item is "John", "123" . I want to store "123" in a string after selecting the entry from the list
Any help?
Here's the code snippet:
ListView lv = getListView();
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData(scanned_name, scanned_addr));
String[] from = { "name", "address" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
Toast.makeText(getApplicationContext(),
parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
});

You have to catch the content of android.R.id.text1 or android.R.id.text2. So, in your onItemClick(...) you have to do:
TextView v = (TextView)findViewById(R.id.text1);
TextView v1 = (TextView)findViewById(R.id.text2);
String content1=v.getText().toString();
String content2=v2.getText().toString();
and you can manage them as you want, even within a Toast:
Toast.makeText(getApplicationContext(),v.getText().toString(),Toast.LENGTH_LONG).show();

Related

android: how to delete a line from sql via ListView?

I managed to show a list from sqllite, now I want that when the user click on the item, the item will be deleted. the problem is that the item's ID number from the sql is different from the listview's ID. so how can I delete the selected item?
I mean this id:
public void onItemClick(AdapterView parent, View view,int position, long id)
is different from the item's ID from the sqllite.
thank you for help
Cursor resultSet = db.rawQuery("Select * from list ORDER BY `ID` DESC",null);
resultSet.moveToFirst();
final ListView listview = (ListView) findViewById(R.id.listView1);
ArrayList<HashMap<String, String>> mylistData = new ArrayList<HashMap<String, String>>();
String[] columnames = new String[] {"C1", "C2", "C3"};
int[] columnsR = new int[] {R.id.column1, R.id.column2, R.id.column3};
int x=0;
while(resultSet.moveToNext()){
HashMap<String,String> map = new HashMap<String, String>();
String d_weight = resultSet.getString(resultSet.getColumnIndex("weight"));
String d_date = resultSet.getString(resultSet.getColumnIndex("date"));
String d_id = resultSet.getString(resultSet.getColumnIndex("ID"));
x=0;
map.put(columnames[x],d_weight);
x++;
map.put(columnames[x],d_date);
x++;
map.put(columnames[x],d_id);
mylistData.add(map);
}
SimpleAdapter arrayAdapter = new SimpleAdapter(this, mylistData, R.layout.row,columnames , columnsR);
listview.setAdapter(arrayAdapter);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
// how to delete? =[
}});
Would it not be easier to make an object that you fill with the data from your sql query for each entry? Then you just populate the listview with a list of these objects, which means that when one of the objects get clicked on the id will be stored inside the object. Then int position would have the same value as the position in your list of objects.
How does that sound?

How to use SimpleCursorAdapter with single_choice and multiple_choice

I am developing a quiz application which contain both multiple answer type and single answer type.the question and answer are store in sqlite database.i use simple cursor adapter and get idea about how to use from here! My question is that how i can switch from simple_list_item_single_choice to simple_list_item_multiple_choice if my answer type changes(single to multiple) and also how can i save the answer choosen.Please give some idea about it.Here is the coding....
db = new DBAdapter(this);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
check this code: for single choice:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_single_choice,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
for multiple:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
simple_list_item_multiple_choice,
db.getAllTitles(),
new String[] { "title" },
new int[] { android.R.id.text1 });
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
depend on your requirement you have to set one of above: i think you aleady kno which question require single ans & which require multiple depend on that you have to also create list item click like
listView.setOnItemClickListener(new OnItemClickListener() {
private String my_sel_items;
public void onItemClick(AdapterView arg0, View arg1, int arg2,
long arg3) {
if(ans == multiple){
my_sel_items = new String("Selected Items");
SparseBooleanArray a = lView.getCheckedItemPositions();
for (int i = 0; i < a.size(); i++) {
if (a.valueAt(i)) {
my_sel_items = my_sel_items + ","
+ (String) listView.getAdapter().getItem(i);
}
}
Log.v("values", my_sel_items);
}else{
// for single it default selected item
}
}
});

how to change image in imageview from adapter android

How can i change image from adapter.
I have created a ListView and in this listview i have added dynamic item which has name and picture. I want to change the pic from that item on some logic.
adapter is created by below code
SimpleAdapter adapter = new SimpleAdapter(this, menuItems , R.layout.list_item, new String[] { NAME, EMPID }, new int[] { R.id.name, R.id.empid });
setListAdapter(adapter);
ListView lv = getListView();
below Code works fine if under OnItemClickListener. but i want image in at the time of rendering first view.
lv.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(XMLUtil.getNodeValue(group, "Gender").equalsIgnoreCase("male")){
view.findViewById(R.id.imageView).setBackgroundResource(R.drawable.male);
}else{
view.findViewById(R.id.imageView).setBackgroundResource(R.drawable.female);
}
}
Set new image in menuItems and call adapter.notifyDataSetChanged() for updating your ListView

Application crashes trying to retrieve view contents from ListView using SimpleAdapter

I am having an issue with trying to retrieve the contents of the view that the the user has clicked within a ListView. My ListView is setup using a SimpleAdapter and is comprised of a "heading" and a "sub-title" for each of the items in the list. These are stored using a HashMap.
Within the activity also, is a spinner, when the user selects an item in the spinner, the ListView is updated. This is working fine and I just thought it necessary to mention what is happening within the activity as well.
What I am trying to do, is retrieve which item the user has selected so I can guide them to a new activity based on their selection (right now just trying to display the view contents with Toast). I want to retrieve the contents of what is stored under the "engExp" key for the item.
Here is my code:
// HASHMAP FOR LISTVIEW
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int x = 0; x < expressionListForModule.getCount(); x++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("engExp", expressionListForModule.getString(0));
map.put("japDef", expressionListForModule.getString(1));
fillMaps.add(map);
expressionListForModule.moveToNext();
}
// SETUP LISTVIEW
SimpleAdapter adapter2 = new SimpleAdapter(this, fillMaps, android.R.layout.simple_list_item_2, new String[] {"engExp","japDef"}, new int[] {android.R.id.text1, android.R.id.text2});
ListView lv = (ListView) findViewById(R.id.expressionList);
lv.setAdapter(adapter2);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_LONG).show();
}
});
Specifically, the item that is causing the application to crash is this:
((TextView) view).getText()
If you need to see any more of my code, please let me know.
Any help is appreciated, thanks in advance.
try
lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView v = (TextView) view.findViewById(android.R.id.text1);
String contentForLink = v.getText().toString();
Toast.makeText(getApplicationContext(), contentForLink, Toast.LENGTH_LONG).show();
}
});

Android: How do I return a selected dataset from SimpleAdapter via onclick?

I am trying to get my head round using SimpleAdapter however I am struggling with one item.
How do I return the array of data for the selected view?
Here is the code I use to generate it.
// Data for List Adapter
List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();
// Make Hashmap of Results Hashmaps
generateData(resourceNames);
//Source information from Hashmap keys (FROM)
String keynames[]={"names","descriptions"};
// Target Views for Data (TO)
int[] targets = new int[] { R.id.textfield_name,R.id.textfield_description};
// Create my Simple Adapter
SimpleAdapter adapter = new SimpleAdapter(this, resourceNames,
R.layout.listrow, keynames,targets
);
setListAdapter(adapter);
}
I then use an onclick listener to get the data using the following code:
setContentView(R.layout.listview);
final ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(arg1.getContext(),
getString(R.string.selected) + " " + position,
Toast.LENGTH_SHORT).show();
// How do I return the entire data that I used
}
});
I can see above how I get the position of the data in the array- but how do I return the actual results data being shown in the view??
Declare List<Map<String, Object>> resourceNames = new ArrayList<Map<String, Object>>();
globally and in ListView's onItemClick() just put line,
Map<String, Object> map = resourceNames.get(position);

Categories

Resources