我正在做一个应用程序来帮助我在学校。但是,我有一个问题:当我想要listView的位置(int位置)时,它总是返回第一个位置,即使我选择第三个位置,例如。
public class TestsActivity extends ListActivity implements AdapterView.OnItemClickListener, DialogInterface.OnClickListener {
private List<Map<String, Object>> tests;
private AlertDialog dialogDelete;
private int selectedTest;
private Application dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tests_activity);
dao = new Application(this);
android.app.ActionBar actionBar = getActionBar();
actionBar.setTitle("My tests");
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
String[] from = {"subject", "content"};
int[] to = {R.id.subject, R.id.content};
SimpleAdapter adapter = new SimpleAdapter(this, listTests(), R.layout.test_list_activity, from, to);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
registerForContextMenu(getListView());
this.dialogDelete = createDialogDelete();
}
private List<Map<String, Object>> listTests() {
tests = new ArrayList<Map<String, Object>>();
List<Test> TestList = dao.listTests();
for (Test test : TestList) {
Map<String, Object> item = new HashMap<String, Object>();
Long id = test.getId();
String subject = test.getSubject();
item.put("id", test.getId());
item.put("subject", subject);
test.add(item);
}
return tests;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Map<String, Object> map = tests.get(position);
String subject = (String) map.get("subject");
String msg = "Test of" + subject;
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
selectedTest = position;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.tests_context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
Intent intent;
String id = String.valueOf(tests.get(selectedTest).get("id"));
switch (item.getItemId()) {
case R.id.remove:
dialogDelete.show();
break;
case R.id.edit:
intent = new Intent(this, NewTestActivity.class);
intent.putExtra(Constantes.TEST_ID, id);
startActivity(intent);
return true;
}
return super.onContextItemSelected(item);
}
@Override
public void onClick(DialogInterface dialog, int item) {
Intent intent;
String id = String.valueOf(tests.get(selectedTest).get("id"));
switch (item) {
case DialogInterface.BUTTON_POSITIVE:
tests.remove(selectedTest);
dao.removeTest(id);
getListView().invalidateViews();
break;
case DialogInterface.BUTTON_NEGATIVE:
dialogDelete.dismiss();
break;
}
}
private AlertDialog createDialogDelete() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.confirm_delete_test);
builder.setPositiveButton(getString(R.string.yes), (android.content.DialogInterface.OnClickListener) this);
builder.setNegativeButton(getString(R.string.no), (android.content.DialogInterface.OnClickListener) this);
return builder.create();
}
@Override
protected void onDestroy() {
dao.close();
super.onDestroy();
}
}
我该如何解决?
答案 0 :(得分:1)
for (Test test : TestList) {
Map<String, Object> item = new HashMap<String, Object>();
Long id = test.getId();
String subject = prova.getSubject();
item.put("id", prova.getId());
item.put("subject", subject);
tests.add(item);
}
尝试从
更改test.add(item);
到
tests.add(item);
答案 1 :(得分:0)
你有没有在你的Activity中调用listTests()?它的私有方法是什么?
你应该在某个地方调用listTests()。
答案 2 :(得分:0)
也许你应该使用自定义BaseAdapter 它可以帮助你清楚地创建listview的每一个细节
private class Simple extends BaseAdapter {
@Override
public int getCount() {
return 0;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
return view;
}
}
答案 3 :(得分:0)
根据您的回答,以下是我的建议。
1)在 listTests ()中,更改为:
test.add(item);
TO:
tests.add(item);
原因是对象 tests 是包含对象测试的ArrayList。
2)由于代码取决于List<Test> TestList = dao.listTests();
的列表dao,我想避免意外删除列表项。所以
在protected void onDestroy()
中,删除:
dao.close();