因此,我已经使用Android的适配器和适配器完成了此操作,并且工作正常。自定义警报对话框包含一个编辑文本,当用户单击“确定”时,该名称将被添加到列表视图中。
但是现在,我正在使用模型和自定义适配器。该怎么做?
这是我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
EditText textsearch;
ListView lv;
AlertDialog.Builder builder;
ArrayList<Names> namesArrayList = new ArrayList<>(); //arraylist for names
ArrayList<Names> findnames = new ArrayList<>(); //array list for search filter
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textsearch = (EditText) findViewById(R.id.textsearch);
lv = (ListView) findViewById(R.id.listview);
adapter = new Adapter(this, namesArrayList);
lv.setAdapter(adapter);
//
builder = new AlertDialog.Builder(this);
lv.setOnItemClickListener(this);
this.textsearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//using regular expression
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// findnames.clear();
//
// //using regular expression
// String s1 = s.toString();
// Pattern pattern = Pattern.compile(s1);
//
// for (int i=0; i<list.size(); i++){
// Matcher matcher = pattern.matcher(list.get(i));
// if(matcher.find()){
// findnames.add(list.get(i));
// }//end if
// }
// //update the adapter
// findadapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.addmenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_add:
showDialog(); //calling the showDialog() method
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void showDialog(){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.custom_dialog, null);
dialogBuilder.setView(dialogView);
final EditText textName = (EditText) dialogView.findViewById(R.id.editName);
dialogBuilder.setTitle("New Item");
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(!textName.getText().toString().equals("")){
// code here to add item
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Item added!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Fields can not be empty!", Toast.LENGTH_SHORT).show();
}
}
});
dialogBuilder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog build = dialogBuilder.create();
build.show();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
this.namesArrayList.remove(position);
this.adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Name deleted!", Toast.LENGTH_LONG).show();
}
}
这是我的模特。 Names.java
public class Names {
String name;
public Names(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Adapter.java
public class Adapter extends BaseAdapter {
Context context;
//data container
ArrayList<Names> list;
LayoutInflater inflater;
//constructor
public Adapter(Context context, ArrayList<Names> list) {
this.context = context;
this.list = list;
this.inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_layout, parent, false);
holder.name = (TextView) convertView.findViewById(R.id.txtname);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//inflate
holder.name.setText(list.get(position).getName());
return convertView;
}
static class ViewHolder{
TextView name;
}
}
答案 0 :(得分:2)
尝试这样的事情:
if(!textName.getText().toString().equals("")) {
Names name = new Names(textName.getText().toString());
adapter.list.add(name);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Item added!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Fields can not be empty!", Toast.LENGTH_SHORT).show();
}
本质上,我要做的只是制作一个Names
模型的新实例并将其添加到适配器中的项目列表中