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

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?

Related

set textview onload and not on itemclick in a listview

I have a listview populated from a table with a simple adapter. Now, I can change the value of a textview if I click the list item, like this:
ArrayList<HashMap<String, String>> userList = controller.getOld();
// If users exists in SQLite DB
if (userList.size() != 0) {
// Set the User Array list in ListView
ListAdapter adapter = new SimpleAdapter(OldTips.this, userList, R.layout.old_tips, new String[] {
"userId", "tara","ora_meci" ,"echipa_acasa","echipa_deplasare","cota","pronostic","explicatii","rezultat"}, new int[] { R.id.userId, R.id.tara , R.id.ora_meci, R.id.echipa_acasa, R.id.echipa_deplasare, R.id.cota,R.id.pronostic,R.id.explicatii,R.id.rezultat});
final ListView myList = (ListView) findViewById(android.R.id.list);
myList.setAdapter(adapter);
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// Object obj = myList.getAdapter().getItem(position);
// String value= obj.toString();
TextView textView = (TextView) view.findViewById(R.id.echipa_acasa);
// String nume_campanie = textView.getText().toString();
textView.setText("value of header text");
}
});
Question: is it possible to change the textview value onload or something like this and not onclick of the item from the 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);

Get a single field from multi-column ListView using SimpleAdapter

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();

Specific textview from listview using OnClick?

Hi I have a question. Wondering how do I get certain values from the OnItemClick method. I can get position but seen as I have more then 1 textview in this position is it possible to get the first textview?
Heres my code
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
for(int i=0; i<poi.length; i++){
map = new HashMap<String, String>();
map.put("first", poi[i][0]);
map.put("second",poi[i][1]);
mylist.add(map);
}
SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] {"first", "second"}, new int[] {R.id.FIRST, R.id.SECOND});
list.setAdapter(mSchedule);
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Log.i("owen", Integer.toString(position));
//my code to get "first" string on click
}
Thanks
If you know the resource identifier for the desired textview, you can take the view passed into the onitemClick() method and find that child:
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
TextView myText = (TextView) v.findViewById(R.id.my_textview);
Log.i("owen", myText.getText().toString());
}
The position relates to the row position in the AdapterView list.

Categories

Resources