Android的程序,怎样同时实现动态添加ListView条目和长按条目弹出菜单选择是否删除功能?
我分别找了两个教程,然后把两端代码合成一段,点击button没有任何反应,求解答,或者给其他方法也可以,万分感谢!publicclassPage1extendsActiv...
我分别找了两个教程,然后把两端代码合成一段,点击button没有任何反应,求解答,或者给其他方法也可以,万分感谢!
public class Page1 extends Activity implements OnClickListener,
OnItemClickListener {
private ListView lvDynamic;
private ViewAdapter viewAdapter;
private int selectedIndex = -1;
private ArrayAdapter adapter;
private List<Object> textIdList = new ArrayList<Object>();
private class ViewAdapter extends BaseAdapter {
private Context context;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(inflater);
LinearLayout linearLayout = null;
if (textIdList.get(position) instanceof String) {
linearLayout = (LinearLayout) layoutInflater.inflate(
R.layout.text, null);
TextView textView = ((TextView) linearLayout
.findViewById(R.id.textview));
textView.setText(String.valueOf(textIdList.get(position)));
}
return linearLayout;
}
public ViewAdapter(Context context) {
this.context = context;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
return textIdList.size();
}
@Override
public Object getItem(int position) {
return textIdList.get(position);
}
public void addText(String text) {
Log.v("add", "");
textIdList.add(text);
notifyDataSetChanged();
}
public void remove(int index) {
if (index < 0)
return;
textIdList.remove(index);
notifyDataSetChanged();
}
}
@Override
public void onClick(View view) {
Log.v("点击了", "Ok");
viewAdapter.addText("新建日志");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
new Intent(Page1.this,Log.class);
Log.v("当前", "" + view + position + selectedIndex);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo acmiRef = (AdapterContextMenuInfo) item
.getMenuInfo();
int removeIndex = acmiRef.position;
textIdList.remove(removeIndex);
adapter.notifyDataSetChanged();
Log.v("!选中了:", "" + item.getItemId() + "位置:" + acmiRef.position);
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 1, 1, "删除");
menu.add(0, 2, 1, "不删除");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
lvDynamic = (ListView) findViewById(R.id.lvDynamic);
Button btnAddText = (Button) findViewById(R.id.btnAddLog);
btnAddText.setOnClickListener(this);
viewAdapter = new ViewAdapter(this);
lvDynamic.setAdapter(viewAdapter);
lvDynamic.setOnItemClickListener(this);
lvDynamic.setAdapter(adapter);
this.registerForContextMenu(lvDynamic);
}
} 展开
public class Page1 extends Activity implements OnClickListener,
OnItemClickListener {
private ListView lvDynamic;
private ViewAdapter viewAdapter;
private int selectedIndex = -1;
private ArrayAdapter adapter;
private List<Object> textIdList = new ArrayList<Object>();
private class ViewAdapter extends BaseAdapter {
private Context context;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(inflater);
LinearLayout linearLayout = null;
if (textIdList.get(position) instanceof String) {
linearLayout = (LinearLayout) layoutInflater.inflate(
R.layout.text, null);
TextView textView = ((TextView) linearLayout
.findViewById(R.id.textview));
textView.setText(String.valueOf(textIdList.get(position)));
}
return linearLayout;
}
public ViewAdapter(Context context) {
this.context = context;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getCount() {
return textIdList.size();
}
@Override
public Object getItem(int position) {
return textIdList.get(position);
}
public void addText(String text) {
Log.v("add", "");
textIdList.add(text);
notifyDataSetChanged();
}
public void remove(int index) {
if (index < 0)
return;
textIdList.remove(index);
notifyDataSetChanged();
}
}
@Override
public void onClick(View view) {
Log.v("点击了", "Ok");
viewAdapter.addText("新建日志");
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
new Intent(Page1.this,Log.class);
Log.v("当前", "" + view + position + selectedIndex);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo acmiRef = (AdapterContextMenuInfo) item
.getMenuInfo();
int removeIndex = acmiRef.position;
textIdList.remove(removeIndex);
adapter.notifyDataSetChanged();
Log.v("!选中了:", "" + item.getItemId() + "位置:" + acmiRef.position);
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.add(0, 1, 1, "删除");
menu.add(0, 2, 1, "不删除");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
lvDynamic = (ListView) findViewById(R.id.lvDynamic);
Button btnAddText = (Button) findViewById(R.id.btnAddLog);
btnAddText.setOnClickListener(this);
viewAdapter = new ViewAdapter(this);
lvDynamic.setAdapter(viewAdapter);
lvDynamic.setOnItemClickListener(this);
lvDynamic.setAdapter(adapter);
this.registerForContextMenu(lvDynamic);
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询