我收到此错误 - 无法解析方法' onListItemClick(android.widget.ListView,android.view.View,int,long)' 我已经尝试使用onClickListener方法,但这不起作用。 请建议。
这是代码 -
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
private Toolbar mToolbar;
private FragmentDrawer drawerFragment;
ListView listview;
String[] pdflist;
File[] imagelist;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar);
drawerFragment.setDrawerListener(this);
File images = Environment.getExternalStorageDirectory();
imagelist = images.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return ((name.endsWith(".pdf")));
}
});
pdflist = new String[imagelist.length];
for (int i = 0; i < imagelist.length; i++) {
pdflist[i] = imagelist[i].getName();
}
listview.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, pdflist));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String path = imagelist[(int) id].getAbsolutePath();
openPdfIntent(path);
}
private void openPdfIntent(String path) {
try {
final Intent intent = new Intent(MainActivity.this, Second.class);
intent.putExtra(PdfViewerActivity.EXTRA_PDFFILENAME, path);
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
请改用:
yourListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
答案 1 :(得分:0)
将此行替换为
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener {
到
public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener, OnItemClickListener {
并在onCreate()
方法中添加以下行:
listview = (ListView) findViewById(R.id.listView1);
listview.setOnItemClickListener(this);