InfoAdatpter
package com.example.listviewtest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class InfoAdapterextends ArrayAdapter {
private int resourcesId;
public InfoAdapter(Context context, int resourcesId, List items)
{
super(context, resourcesId, items);
this.resourcesId = resourcesId;
}
@Override
public ViewgetView(int position, View convertView, ViewGroup parent) {
StuInfo stuInfo = (StuInfo) getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourcesId, null);
TextView textView1 = (TextView) view.findViewById(R.id.list_item1);
TextView textView2 = (TextView) view.findViewById(R.id.list_item2);
TextView textView3 = (TextView) view.findViewById(R.id.list_item3);
textView1.setText(stuInfo.getClassNo());
textView2.setText(stuInfo.getsNo());
textView3.setText(stuInfo.getName());
return view;
}
}
stuInfo
package com.example.listviewtest;
public class StuInfo {
StringclassNo;
StringsNo;
Stringname;
public StuInfo(String cno, String sno, String name)
{
classNo = cno;
sNo = sno;
this.name = name;
}
public StringgetClassNo() {
return classNo;
}
public StringgetsNo() {
return sNo;
}
public StringgetName() {
return name;
}
}
MainActivity
package com.example.listviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivityextends AppCompatActivityimplements Button.OnClickListener{
Buttonaddbtn;
EditTextclassNo, sno, name;
ListViewlistView;
ListdataList;
InfoAdapterinfoAdapter;
int context_menu = Menu.FIRST;
int item_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addbtn = (Button) findViewById(R.id.Add);
addbtn.setOnClickListener(this);
classNo = (EditText) findViewById(R.id.classNo);
sno = (EditText) findViewById(R.id.studentNo);
name = (EditText) findViewById(R.id.name);
listView = (ListView) findViewById(R.id.listview);
dataList =new ArrayList();
StuInfo stu =new StuInfo("信息2","2016013018","qinbo");
dataList.add(stu);
infoAdapter =new InfoAdapter(MainActivity.this, R.layout.list_item, dataList);
listView.setAdapter(infoAdapter);
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
item_id = info.position;
menu.add(0,context_menu,0,"删除");
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.Add){
if(!classNo.getText().toString().equals("") && !sno.getText().toString().equals("") && !name.getText().toString().equals("")){
StuInfo tmp =new StuInfo(classNo.getText().toString(), sno.getText().toString(), name.getText().toString());
dataList.add(tmp);
infoAdapter.notifyDataSetChanged();
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getItemId()==context_menu){
dataList.remove(item_id);
infoAdapter.notifyDataSetChanged();
return true;
}
else
return false;
}
}
layout.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:rowCount="4"
android:columnCount="2"
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="班级: "/>
android:id="@+id/classNo"
android:layout_width="180dp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号: " />
android:id="@+id/studentNo"
android:inputType="number"
android:layout_width="180dp"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"/>
android:id="@+id/name"
android:inputType="text"
android:layout_width="180dp"/>
android:id="@+id/Add"
android:layout_columnSpan="2"
android:text="添加"/>
android:layout_columnSpan="2"
android:text="班级 学号 姓名"/>
android:id="@+id/listview"
android:layout_columnSpan="2">