onListItemClick() show different-2 activities - android

I have responceid (String responceid = Activity2.getData()) for every row ...I want when I click on any position of list item it show its corresponding responceid ...so how to bind responseid for every row ....responceid takes from database
#Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String responceid = Activity2.getData();
Object o = (String) (Notepadv3.this).getListAdapter().getItem(position);
Toast.makeText(this, "this row responce id is= " + " " + , Toast.LENGTH_LONG).show();
}

If you have used an adapter to fill the listView then use getItemId() to retrieve the database id.
Salil

Related

Try to delete selected item from listview and DB but my code will delete the previous item rather than the selected item

Updated code for refreshing the listview is not working.
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
cd.open();
cd.deleteRow(id+1);
strings.remove(position);
lv_arr = (String[]) strings.toArray(new String[strings.size()]);
arrayAdapter.notifyDataSetChanged();
return false;
}
You are passing the id or position of selected item from listview, So you should know that the listview items are initialize with 0 position and it may be possible you have store items with id 1 and then increase it by 1.
So In this kind of situation either delete it by increase id by 1 or create one object for selected item and then pass it to remove.
For first easy situation check below answer.
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
Log.v("long clicked", "pos: " + position);
cd.open();
//long i=id;
// i--;
cd.deleteRow(id + 1);
strings.remove(position)
lv_arr = (String[]) strings.toArray(new String[strings.size()]);
arrayAdapter.notifyDataSetChanged();
// CView.setVisibility(ListView.VISIBLE);
// Intent intent=new Intent(SqlView.this,SqlView.class);
// startActivity(intent);
// ret();
Toast.makeText(getApplicationContext(),"You have deleted a workout!", Toast.LENGTH_LONG).show();
return false;
}

onListItemClick for dynamically changing list item

protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
int selection = position;
switch (selection)
{
case 0:
{
String toast=" 1 clicked";
DisplayToast(toast);
}
break;
case 1:
{
String toast=" 2 clicked";
DisplayToast(toast);
}
break;
}
}
This is the code I am using for my OnListItemClick() in List Activity.
My Problem is my List item is populated dynamically and hence I don't know how many items would be there in the list.
I cannot use switch statement in that case.So. How can I distinguish which item was clicked in a dynamically changing list.
Set an onItemClickListener like this:
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
Toast.makeText(MainActivity.this, adapter.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
}
});
So you can get the text of your currently selected item by getItemAtPosition(position).toString().
Or if you make your own ArrayAdapter, you can implement the getItem(position) method, which can return with anything about your adapter item.
This will get the Text from the selected textview and display it in a Toast.
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
String toast= ((TextView)v).getText();
DisplayToast(toast);
}
Following is the code
String toast = " " + ++position + " clicked";
DisplayToast(toast);

returning something other than the actual label in a list view

If I have a listview which is populated with a string array say:
Mazda
Renault
Holden
Audi
and when I want to click on an item, rather than returning string "Mazda" I'd rather return an associated number e.g. Mazda Returns 0, Renault returns 1 etc.
Is this possible and would I see a significant performance increase on querying an
SQLite DB using just ints rather than strings?
Thanks,
M
in your listview there are adapter are use, so in this method there are position variable also already exist there.if you print it in logcat it will get every time position of your list-item.you need to handle it in array easily
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}
You could do something like the following:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] MY_CARS = new STRING[] { Mazda, Renault, Holden, Audi };
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, MY_CARS));
ListView lv = (ListView)findViewById(R.id.MyListView);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Item at Position "+pos+" was clicked. This corresponds to "+MY_CARS[pos],
Toast.LENGTH_SHORT).show();
}
});
}
This should return the position of the item in the list that was clicked, which should be the index of the array.
I am not sure whether you will get a significant increase in db performance though.

Get Text from ListView Item that was created from DB

i create a ListView with content from a Database.
I want to get the Text from one Item with:
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String item = l.getItemAtPosition(position).toString();
}
But i get some text: "android.database.sqlite.SQLiteCurser#45765"
Can anyone please help me?
What exactly you retrieved from the database. If it's only set of Strings you can try the below code.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show(); }
this should work...
On onItemClick :
String text = parent.getItemAtPosition(position).toString();

how to get the index and textview of the selected item in listview?

i have a listview from webservice and i want to get the index and textview of the selected item .my list view is
company symbol
android an
iphone ip
blackderry bb
. .
. .
. .
so tell me how to get the index and textview of symbol in the listview from webservice.i used two methods
firstmethod:
protected void onListItemClick(ListView l, View v, int position,long id)
{
super.onListItemClick( l, v, position, id);
ID = id;
int selectedPosition = l.getSelectedItemPosition();
index=String.valueOf(selectedPosition);
Log.e("index value","index"+index);
TextView txt=(TextView)findViewById(R.id.symboltext);
TextView temp=(TextView)v;
txt.setText(temp.getText());
Log.e("text",txt.toString());
}
in above code the logcat shows index is -1 and could not shows the txt.
Second method:
listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView, int position, long id)
{
String text = ((TextView)childView.getText()).toString();
//The above text variable has the text value of selected item
// position will reflect the index of selected item
}
public void onNothingSelected(AdapterView parentView)
{
}
});
in above method the listview didnt get onclick action. so please tell me how to get the index and textview of the selected item in listview.
Best Regards
Thanks in advance
Try this code. And Check your logcat. It should get you the desired result.
protected void onListItemClick(ListView l, View v, int position,long id)
{
super.onListItemClick( l, v, position, id);
ID = id;
Log.e("index value","index"+position);
TextView txt = (TextView) v.findViewById(R.id.symboltext);
Log.e("text",txt.toString());
}
you can use a hashmap to put your string to a listview
this link will help you to understand how hashmap works and how the strings are saved and retrived from it
I was trying something like this, on a Custom ListView:
My scenario was:
To fetch three Objects from Server with JSONArrayRequest named as:
ID (tvLvDocID)
Title (tvLvDocTitle)
Sub Title (tvLvDocSub Title)
ID is actually the ID of the column of item in MySQL database.
What I did was:
listViewDoc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int myPosition = position;
String id_doc_from_lv = listViewDoc.getItemAtPosition(myPosition).toString();
Toast.makeText(getApplicationContext(), "ID is : " + id_doc_from_lv, Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
But My problem was that it was returning Title of the Custom ListView.
And I wanted only and only the ID, which I was also showing in ListView above Title. (Its another story I will later apply INVISIBLE property to that ID).
As I wanted only ID. So I did toke help from this answer:
When I add these lines in my code:
String tv_ID_Str= ((TextView)view.findViewById(R.id.tvLvDocID)).getText().toString();
And call this tv_ID_Str as:
Toast.makeText(getApplicationContext(), "ID is : " + tv_ID_Str, Toast.LENGTH_LONG).show();
Aha! Now I can successfully show tvLvDocID and I can retrieve all the information from database on this ID.
Final code looks like this:
listViewDoc.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int myPosition = position;
String id_doc_from_lv = listViewDoc.getItemAtPosition(myPosition).toString();
// selected item
String tv_ID_Str = ((TextView)view.findViewById(R.id.tvLvDocID)).getText().toString();
Toast.makeText(getApplicationContext(), "ID is : " + tv_ID_Str, Toast.LENGTH_LONG).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
Purpose of my answer is people like me, who spend lot of time to do this. I hope this will help someone.

Categories

Resources