我有一个AppCompatActivity。在那里我有一个列表视图。我想在选择一个项目时发生这样的事情(另一个活动将启动,但是现在我只想进入我可以调用该意图的方法)。
我不知道该怎么做。我看到我可以使用我的AppCompatActivity实现AbsListView.OnItemClickListener,它给了我一个方法
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
但是当我运行我的代码并选择一个项目时,它不会进入该方法。 那么有人能告诉我我是怎么做到的吗?
以下是我活动的全部代码。
public class RouteList extends AppCompatActivity implements AbsListView.MultiChoiceModeListener, AbsListView.OnItemClickListener {
ArrayList<RouteItem> itemsList;
ArrayList<String> routeFiles;
ArrayList<RouteItem> selected;
RouteListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_list);
itemsList = new ArrayList<RouteItem>();
selected = new ArrayList<RouteItem>();
adapter = new RouteListAdapter(this, R.layout.route_list_item);
ListView listView = (ListView) findViewById(R.id.routeList);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
listView.setMultiChoiceModeListener(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Routes");
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
getRouteFiles();
RouteLocationsTask task = new RouteLocationsTask(adapter, routeFiles, getApplicationContext());
task.execute();
}
public void getRouteFiles()
{
String path = Environment.getExternalStorageDirectory()+"/LocationTracker/Routes/";
// Read all files sorted into the values-array
routeFiles = new ArrayList();
File dir = new File(path);
String[] list = dir.list();
if (list != null) {
for (String file : list) {
if (!file.startsWith(".")) {
routeFiles.add(file);
}
}
}
Collections.sort(routeFiles);
}
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
int x = adapter.getCount();
RouteItem routeItem = adapter.getItem(position);
if(checked)
selected.add(adapter.getItem(position));
else
selected.remove(adapter.getItem(position));
mode.setTitle(String.valueOf(selected.size()));
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
getMenuInflater().inflate(R.menu.listview_cab_menu, menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()){
case R.id.delete:
itemsList = adapter.getItemList();
itemsList.removeAll(selected);
adapter.notifyDataSetChanged();
deleteSelectedFiles();
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
selected.clear();
}
public void deleteSelectedFiles()
{
for(RouteItem routeItem : selected)
{
File file = new File(routeItem.getFileName());
file.delete();
}
}
//@Override
public void onClick(View v) {
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int y = 0;
}
}
答案 0 :(得分:2)
您需要在ListView上调用setOnItemClickListener
方法才能在onItemClick
中接收回调。只需将此行添加到Activity
onCreate
方法中。
listView.setOnItemClickListener(this);