ListViewPopMenuDBAdapter_android

【ListViewPopMenuDBAdapter_android】非淡泊无以明志,非宁静无以致远。这篇文章主要讲述ListViewPopMenuDBAdapter_android相关的知识,希望能为你提供帮助。


public class MainActivity extends AppCompatActivity {
EditText button_class = null;
EditText button_name = null;
EditText button_number = null;
Button add = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_class = (EditText) findViewById(R.id.input_class);
button_name = (EditText) findViewById(R.id.input_name);
button_number = (EditText) findViewById(R.id.input_number);
add = (Button)findViewById(R.id.button);
ListView listView = (ListView)findViewById(R.id.list);

List< String> lists = new ArrayList< String> ();
final ArrayAdapter< String> adapter = new ArrayAdapter< String> (this, android.R.layout.simple_list_item_1, lists);
listView.setAdapter(adapter);

final DBAdapter dbAdapter = new DBAdapter(MainActivity.this);
Log.i("dapter", "test1");
dbAdapter.open();
Log.i("dapter", "test2");
Student[] students = dbAdapter.getAllData();
for(int i=0; i< students.length; i++){
lists.add(students[i].toString());
}


AdapterView.OnItemLongClickListener itemLongClickListener = new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView< ?> parent, final View view, final int position, final long id) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.pop_menu, popupMenu.getMenu());
popupMenu.show();

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.pop:
adapter.remove(adapter.getItem(position));
String[] strings = view.toString().split(" ");
long new_id = Long.parseLong(strings[0]);
dbAdapter.deleteOneData(new_id);
return true;
default:return false;
}
}
});
return true;
}

};
listView.setOnItemLongClickListener(itemLongClickListener);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String stu_class = button_class.getText().toString();
String stu_name = button_name.getText().toString();
String stu_number = button_number.getText().toString();
String info = stu_class+""+stu_name+""+stu_number;
adapter.add(info);
dbAdapter.insert(new Student(stu_number, stu_name, stu_class));
}
});

}
}

/////////////////////////////////////////////////////////////////////////////////////////////

数据库适配器DBAdapter.java

public class MainActivity extends AppCompatActivity {

EditText button_class = null;
EditText button_name = null;
EditText button_number = null;
Button add = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_class = (EditText) findViewById(R.id.input_class);
button_name = (EditText) findViewById(R.id.input_name);
button_number = (EditText) findViewById(R.id.input_number);
add = (Button)findViewById(R.id.button);
ListView listView = (ListView)findViewById(R.id.list);

List< String> lists = new ArrayList< String> ();
final ArrayAdapter< String> adapter = new ArrayAdapter< String> (this, android.R.layout.simple_list_item_1, lists);
listView.setAdapter(adapter);

final DBAdapter dbAdapter = new DBAdapter(MainActivity.this);
Log.i("dapter", "test1");
dbAdapter.open();
Log.i("dapter", "test2");
Student[] students = dbAdapter.getAllData();
for(int i=0; i< students.length; i++){
lists.add(students[i].toString());
}


AdapterView.OnItemLongClickListener itemLongClickListener = new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView< ?> parent, final View view, final int position, final long id) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
MenuInflater inflater = popupMenu.getMenuInflater();
inflater.inflate(R.menu.pop_menu, popupMenu.getMenu());
popupMenu.show();

popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.pop:
adapter.remove(adapter.getItem(position));
String[] strings = view.toString().split(" ");
long new_id = Long.parseLong(strings[0]);
dbAdapter.deleteOneData(new_id);
return true;
default:return false;
}
}
});
return true;
}

};
listView.setOnItemLongClickListener(itemLongClickListener);

add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String stu_class = button_class.getText().toString();
String stu_name = button_name.getText().toString();
String stu_number = button_number.getText().toString();
String info = stu_class+""+stu_name+""+stu_number;
adapter.add(info);
dbAdapter.insert(new Student(stu_number, stu_name, stu_class));
}
});

}
}
///////////////////////////////////////////////////////////////////////////////////////
布局文件activity_main.xml

< ?xml version="1.0" encoding="utf-8"?>
< android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
< LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
< LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
< TextView android:id="@+id/stu_class"
android:layout_marginLeft="5dp"
android:text="calss: "
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
< EditText android:id="@+id/input_class"
android:textSize="18dp"
android:layout_width="300dp"
android:layout_height="wrap_content" />
< /LinearLayout>

< LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
< TextView android:id="@+id/stu_name"
android:layout_marginLeft="5dp"
android:text="name: "
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
< EditText android:id="@+id/input_name"
android:textSize="18dp"
android:layout_width="300dp"
android:layout_height="wrap_content" />
< /LinearLayout>

< LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
< TextView android:id="@+id/stu_number"
android:layout_marginLeft="5dp"
android:text="number: "
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
< EditText android:id="@+id/input_number"
android:textSize="18dp"
android:layout_width="285dp"
android:layout_height="wrap_content" />
< /LinearLayout>

< Button
android:id="@+id/button"
android:layout_width="365dp"
android:layout_height="55dp"
android:text="add info"
android:layout_marginLeft="10dp"
/>
< TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp"
android:text="classnamenumber"
/>
< ListView android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
< /ListView>
< /LinearLayout>

< /android.support.constraint.ConstraintLayout>
































































































































































































































    推荐阅读