大家好,我是 android 的新手,当用户长按列表视图中的项目时,我有一个任务会输出一个选项。你能帮我解决这个问题吗?提前致谢。我在互联网上获取此代码并尝试对其进行调整,但我不知道将方法放在哪里 onlongclick。请帮帮我
public class MainActivity extends ListActivity{
private File mCurrentNode = null;
private File mLastNode = null;
private File mRootNode = null;
private ArrayList<File> mFiles = new ArrayList<File>();
private CustomAdapter mAdapter = null;
private String fname;
private Context context;
private ArrayList<File> objname;
File f;
String filename;
Button df;
private Bundle savedInstanceState;
ListView parent=null;
int position=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File Audio = new File(Environment.getExternalStorageDirectory() + "/Audio");
File Ebook = new File(Environment.getExternalStorageDirectory() + "/Ebook");
File Images = new File(Environment.getExternalStorageDirectory() + "/Images");
File Video = new File(Environment.getExternalStorageDirectory() + "/Video");
Audio.mkdirs();
Ebook.mkdirs();
Images.mkdirs();
Video.mkdirs();
if(!Audio.exists())
{
if(Audio.mkdir())
{
//directory is created;
}
}
if(!Ebook.exists())
{
if(Audio.mkdir())
{
//directory is created;
}
}
if(!Images.exists())
{
if(Audio.mkdir())
{
//directory is created;
}
}
if(!Video.exists())
{
if(Audio.mkdir())
{
//directory is created;
}
}
mAdapter = new CustomAdapter(this, R.layout.list_row, mFiles);
setListAdapter(mAdapter);
if (savedInstanceState != null) {
mRootNode = (File)savedInstanceState.getSerializable("root_node");
mLastNode = (File)savedInstanceState.getSerializable("last_node");
mCurrentNode = (File)savedInstanceState.getSerializable("current_node");
}
refreshFileList();
}
/* public void onLongClick(ListView parent, View v, int position, long id){
f = (File) parent.getItemAtPosition(position);
//Browser
if (position == 1) {
if (mCurrentNode.compareTo(mRootNode)!=0) {
mCurrentNode = f.getParentFile();
refreshFileList();
Toast.makeText(this, "This is postion 1 "+f.getName()+"!", Toast.LENGTH_SHORT).show();
// out();
}
} else if (f.isDirectory()) {
//f.delete();
mCurrentNode = f;
refreshFileList();
Toast.makeText(this, "This is postion 2 "+f.getName()+"!", Toast.LENGTH_SHORT).show();
//out();
} else {
Toast.makeText(this, "You selected: "+f.getName()+"!", Toast.LENGTH_SHORT).show();
//out();
}
//});
}*/
//end of bundle
private void refreshFileList() {
if (mRootNode == null) mRootNode = new File(Environment.getExternalStorageDirectory().toString());
if (mCurrentNode == null) mCurrentNode = mRootNode;
mLastNode = mCurrentNode;
File[] files = mCurrentNode.listFiles();
mFiles.clear();
mFiles.add(mRootNode);
mFiles.add(mLastNode);
if (files!=null) {
for (int i = 0; i< files.length; i++) mFiles.add(files[i]);
}
mAdapter.notifyDataSetChanged();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putSerializable("root_node", mRootNode);
outState.putSerializable("current_node", mCurrentNode);
outState.putSerializable("last_node", mLastNode);
super.onSaveInstanceState(outState);
}
/**
* Listview on click handler.
*/
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
f = (File) parent.getItemAtPosition(position);
//Browser
ListView lst=null;
lst.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
if (position == 1) {
if (mCurrentNode.compareTo(mRootNode)!=0) {
mCurrentNode = f.getParentFile();
refreshFileList();
// Toast.makeText(this, "This is postion 1 "+f.getName()+"!", Toast.LENGTH_SHORT).show();
// out();
}
} else if (f.isDirectory()) {
//f.delete();
mCurrentNode = f;
refreshFileList();
// Toast.makeText(this, "This is postion 2 "+f.getName()+"!", Toast.LENGTH_SHORT).show();
out();
} else {
//Toast.makeText(this, "You selected: "+f.getName()+"!", Toast.LENGTH_SHORT).show();
//out();
}
return false;
}
});
}
//@Override
public void out(){
Toast.makeText(this, "You selected: "+f.getName()+"!", Toast.LENGTH_SHORT).show();
}
}