获取android中的listview项索引

时间:2012-02-16 17:35:08

标签: android listview indexing

我正在制作一个使用listview的android应用程序。我希望在按下一个项目后单击一个项目的索引号(单击)。我已经完成了几个教程,但似乎都没有帮助。如何获取索引号并将其传递给String。然后我想删除它,但我将自己管理那部分。我只需要索引号并将其传递给字符串。在onCreate方法之后,我将获得索引号的代码是straigt。请提前帮助和感谢!这是我正在使用的代码:

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;
    }
}
}

3 个答案:

答案 0 :(得分:1)

注意onItemClick方法的参数

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

位置会给你你想要的指数..

答案 1 :(得分:0)

它不是onItemClick中的位置参数吗?

答案 2 :(得分:0)

onItemClick中的position参数将为您提供列表中单击项目的索引。