0

I am making an android application that uses a listview. I want to get the index number of an item once an item has been pressed(single-click). I've gone through several tutorials, but none seemed to help. How can i get the index number and pass it to a String. I do then want to delete it, but i'll manage that part my self. I only need the index number and pass it to a string. The code where i'll get the index number is straigt after the onCreate method. Please help and thanks in advance! This is the code that i am using:

public class NotesActivity extends ListActivity implements OnClickListener {
/** Called when the activity is first created. */
List<String> myList = new ArrayList<String>();
EditText AddItemToListViewEditText;
Button AddItemToListView, AddItemToListViewButton, CancelButton, DeleteButton;
LinearLayout AddItemToListViewLinearLayout, DeleteItemFromListViewLinearLayout;
public String DeleteIndexNumber;
  static final String[] COUNTRIES = new String[] {
      "Matte på A1 med Ole", "Engelsk på klasserommet", "Film på A1 etter friminuttet"
      };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notes);
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, COUNTRIES));
    setListAdapter((ListAdapter) new ArrayAdapter<String>(this, R.layout.list_item, myList));

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);

    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(), "Note: " + ((TextView) view).getText(),
            Toast.LENGTH_SHORT).show();
        //This is where i need the index number to be passed to the string "DeleteIndexNumber"
        DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
        DeleteItemFromListViewLinearLayout.setVisibility(View.VISIBLE);
  }
    });
  }
@Override
public boolean onCreateOptionsMenu(Menu meny) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.listviewmenubuttons, meny);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.AddItemToListView:
        AddItemToListViewButton = (Button)findViewById(R.id.AddItemToListViewButton);
        CancelButton = (Button)findViewById(R.id.CancelButton);
        DeleteButton = (Button)findViewById(R.id.DeleteButton);
        CancelButton.setOnClickListener(this);
        DeleteButton.setOnClickListener(this);
        AddItemToListViewLinearLayout = (LinearLayout)findViewById(R.id.AddItemToListViewLinearLayout);
        AddItemToListViewButton.setOnClickListener(this);
        AddItemToListViewLinearLayout.setVisibility(View.VISIBLE);
        break;
        }
    return true;
}
public void onClick(View src) {
    switch(src.getId()) {
    case R.id.AddItemToListViewButton:
        AddItemToListViewEditText = (EditText)findViewById(R.id.AddItemToListViewEditText);
        myList.add(AddItemToListViewEditText.getText().toString());
        ((ArrayAdapter)getListView().getAdapter()).notifyDataSetChanged(); 
        AddItemToListViewEditText.setText("");
        AddItemToListViewEditText.clearFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        AddItemToListViewLinearLayout.setVisibility(View.GONE);
        break;
    case R.id.CancelButton:
        DeleteItemFromListViewLinearLayout = (LinearLayout)findViewById(R.id.DeleteItemFromListViewLinearLayout);
        DeleteItemFromListViewLinearLayout.setVisibility(View.INVISIBLE);
        break;
    case R.id.DeleteButton:
        break;
    }
}
}
1
  • 1
    in our listview onItemClick integer variable position is for clicked item position of listview. use this for get clicked item position of listview. Commented Feb 16, 2012 at 17:42

3 Answers 3

1

notice the parameters for the onItemClick method

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}

position will give you the index you desire..

Sign up to request clarification or add additional context in comments.

Comments

0

Isn't it position parameter in onItemClick ?

Comments

0

The position parameter in onItemClick will give you the index of the clicked Item in the list.

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.