package com.Dean;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ListViewActivity extends ListActivity implements OnItemClickListener,OnItemLongClickListener{
ViewAdapter va;
int selectedIndex=0;
ListViewActivity thisView;
protected int titleIndex;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
va=new ViewAdapter();
AddDisplayData();
thisView=this;
ListView lv=this.getListView();
lv.setAdapter(va);
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
lv.setSelection(0);
lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
@Override
public void onCreateContextMenu(ContextMenu menu, View arg1,ContextMenuInfo arg2) {
menu.setHeaderTitle(va.ListItemText.get(titleIndex));
menu.add(0, 0, 0, "test1");
menu.add(0, 1, 1, "test2");
menu.add(0, 2, 2, "test3");
menu.add(0, 3, 3, "test4");
}
});
}
void AddDisplayData()
{
va.AddDisplayData("/data/dd/shitouji");
va.AddDisplayData("/data/dd/JBIG");
va.AddDisplayData("/data/gr");
va.AddDisplayData("/data/jiefang");
va.AddDisplayData("/data/");
va.AddDisplayData("/data/testEMB");
va.AddDisplayData("/data/dd/sdji");
}
private class ViewAdapter extends BaseAdapter
{
ArrayList<String> ListItemText=new ArrayList<String>();
public int getCount()
{
return ListItemText.size();
}
public Object getItem(int position)
{
return ListItemText.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
TextView t=new TextView(thisView);
t.setText((String)ListItemText.get(position));
return t;
}
void AddDisplayData(String t)
{
ListItemText.add(t);
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id)
{
Toast t=Toast.makeText(this, "This is onItemClick", Toast.LENGTH_LONG);
t.show();
}
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position,
long id) {
titleIndex=position;
return false;//此处需要注意,如果改成true,不会再次触发ListView的setOnCreateContextMenuListener事件
}
}
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页