在我的列表视图中已有20个项目以及列表视图,其中有一个" ADD"按钮。当我点击按钮然后打开一个弹出对话框..在弹出对话框中有edittext和保存并关闭按钮..当我写一些edittext并点击保存按钮然后它不工作....我&# 39; ll使用自定义列表视图,左侧是一个图像,中间是文本.....帮帮我
Button add = (Button)findViewById(R.id.btn_Add);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.popup);
dialog.setTitle("Add Category");
Button close = (Button)dialog.findViewById(R.id.btn_cls);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
Button save = (Button)dialog.findViewById(R.id.btn_sv);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText edit = (EditText)dialog.findViewById(R.id.editText1);
list.add(edit.getText().toString());
edit.setText("");
adapter.notifyDataSetChanged();
}
});
dialog.show();
}
});
答案 0 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
<强> activity_main.xml中强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/black"
android:orientation="vertical">
<EditText
android:id="@+id/search"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/bbbb"
android:ems="10"
android:textColor="@color/White"
android:hint="Search"
android:padding="5dp"
android:textSize="20sp">
<requestFocus />
</EditText>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:dividerHeight="5dp"
android:cacheColorHint="@null"
android:listSelector="@null"
android:clipToPadding="false"
android:drawSelectorOnTop="true" />
<Button
android:id="@+id/btn_Add"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/list_selector"
android:text="ADD Category"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
<强> listview_item.xml 强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_border"
android:gravity="center"
android:padding="5dp">
<ImageView
android:id="@+id/flag"
android:background="@drawable/sd"
android:layout_width="60dp"
android:layout_height="60dp"
android:adjustViewBounds="true"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp">
<TextView
android:id="@+id/country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="21sp"
android:textStyle="bold" />
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/round"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<强> popup.xml 强>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<Button
android:id="@+id/btn_cls"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:text="Close" />
<Button
android:id="@+id/btn_sv"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/list_selector"
android:text="Save" />
</LinearLayout>
</LinearLayout>
<强> MainActivity.java 强>
public class MainActivity extends Activity {
private ListView list;
private ListViewAdapter adapter;
private EditText editsearch;
private Button add;
private String[] animal = new String[] {"COW","LION","TIGER","ELEPHANT","MONKEY","DONKEY"};
private ArrayList<String> listData;
private Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listview);
add = (Button)findViewById(R.id.btn_Add);
listData = new ArrayList<String>();
for (int i=0;i<animal.length;i++){
listData.add(animal[i]);
}
Collections.sort(listData);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addAnimal();
}
});
adapter = new ListViewAdapter(this,listData);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,listData.get(position),Toast.LENGTH_SHORT).show();
// if(listData.get(position).equals("COW"));
// intent = new Intent(getBaseContext(),COW.class);
//
// else if(listData.get(position).equals("LION"))
// intent = new Intent(getBaseContext(),LION.class);
//
// else if(listData.get(position).equals("TIGER"))
// intent = new Intent(getBaseContext(),TIGER.class);
//
// else if(listData.get(position).equals("MONKEY"))
// intent = new Intent(getBaseContext(),MOKEY.class);
//
// else if(listData.get(position).equals("DONKEY"))
// intent = new Intent(getBaseContext(),DONKEY.class);
}
});
editsearch = (EditText) findViewById(R.id.search);
editsearch.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
String text = editsearch.getText().toString();
adapter.filter(text);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
});
}
public void addAnimal(){
dialog = new Dialog(this);
dialog.setContentView(R.layout.popup);
dialog.setTitle("Add Animal");
final EditText edit = (EditText)dialog.findViewById(R.id.editText1);
Button close = (Button)dialog.findViewById(R.id.btn_cls);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button save = (Button)dialog.findViewById(R.id.btn_sv);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(edit.getText().toString().trim().length()>0){
listData.add(edit.getText().toString());
Collections.sort(listData);
adapter = new ListViewAdapter(MainActivity.this,listData);
list.setAdapter(adapter);
dialog.dismiss();
}else{
edit.setError("Value Required");
}
}
});
dialog.show();
}
class ListViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> data;
private ArrayList<String> filterData;
public ListViewAdapter(Context context,ArrayList<String> data) {
this.context = context;
this.data = data;
filterData =new ArrayList<String>();
filterData.addAll(this.data);
}
public class ViewHolder {
TextView country;
}
@Override
public int getCount() {
return filterData.size();
}
@Override
public Object getItem(int position) {
return filterData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
holder.country = (TextView) view.findViewById(R.id.country);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.country.setText(filterData.get(position));
return view;
}
public void filter(String charText) {
filterData.clear();
if (charText.length() == 0) {
filterData.addAll(data);
}else{
for (String animal : data){
if (animal.toLowerCase().contains(charText.toLowerCase())){
filterData.add(animal);
}
}
}
notifyDataSetChanged();
}
}
}
答案 1 :(得分:1)
我会像那样使用..
public class list extends Activity {
private ListView list;
private Button add ;
private Dialog dialog;
private ListViewAdapter adapter;
private ArrayList<String> listData;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_item);
list = (ListView)findViewById(R.id.listview1);
add = (Button)findViewById(R.id.btn_Addd);
listData = new ArrayList<String>();
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AddSMS();
}
});
list.setAdapter(adapter);
}
public void AddSMS(){
dialog = new Dialog(this);
dialog.setContentView(R.layout.popup1);
dialog.setTitle("Add Your SMS");
final EditText et = (EditText)dialog.findViewById(R.id.editText11);
Button close = (Button)dialog.findViewById(R.id.btn_clss);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button save = (Button)dialog.findViewById(R.id.btn_svv);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (et.getText().toString().trim().length()>0){
listData.add(et.getText().toString());
adapter = new ListViewAdapter(list.this, listData);
list.setAdapter(adapter);
dialog.dismiss();
}else {
et.setError("Value Required");
}
}
});
dialog.show();
}
class ListViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<String> data;
private ArrayList<String> filterData;
public ListViewAdapter(Context context,ArrayList<String> data) {
this.context = context;
this.data = data;
filterData =new ArrayList<String>();
filterData.addAll(this.data);
}
public class ViewHolder {
TextView txttt;
}
@Override
public int getCount() {
return filterData.size();
}
@Override
public Object getItem(int position) {
return filterData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.list_double, null);
holder.txttt = (TextView) view.findViewById(R.id.txttt);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.txttt.setText(filterData.get(position));
return view;
}
public void filter(String charText) {
filterData.clear();
if (charText.length() == 0) {
filterData.addAll(data);
}else{
for (String Detail : data){
if (Detail.toLowerCase().contains(charText.toLowerCase())){
filterData.add(Detail);
}
}
}
notifyDataSetChanged();
}
}
}
答案 2 :(得分:0)
而不是添加到列表中添加适配器中的项目,然后调用notifyDataSetChanged