1

How I can get the selected item data in onItemLongClick ?

  listView.setOnItemLongClickListener(new OnItemLongClickListener() {

                @Override
                public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    //              
                    return false;
                }
            });
1
  • Did you got solution @Mickey Tin ? Commented Jan 7, 2014 at 4:16

3 Answers 3

9
final ListView lv = (ListView) findViewById(R.id.ListView01);

lv.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> myAdapter, View myView, int pos, long mylng) {
    String selectedFromList =(lv.getItemAtPosition(pos).toString());
    // this is your selected item
  }                 
});
Sign up to request clarification or add additional context in comments.

2 Comments

selectedFromList = android.database.sqlite.SQLiteCursor@4052cf70
if you are using array to form a list then using list item selected position you can get value from array
2
 listView.setOnItemLongClickListener(new OnItemLongClickListener() 
 {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) 
    {
         String item = ArryList.get(arg2); 
         Toast.makeText(getBaseContext(), item,
         Toast.LENGTH_LONG).show();          
         return false;
     }
 });

2 Comments

What is pos here ? I think here arg2 is used instead of pos.
cannot convert from Object to String
0

If you parse set of data from xml, json, database, etc it's better to create custom model i.e. your model:

public class YourModel
{
    private int id = -1;
    private String title = "";

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }
}

In activity:

private YourModel yourModel;

@Override
public void onCreate(Bundle savedInstanceState)
{
    lstView.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                    long id)
        {
            detailsModel = ((YourAdapter) lstView.getAdapter()).getItem(position);
            Toast.makeText(getApplicationContext, yourModel.getTitle(),
                Toast.LENGTH_LONG).show();
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.