我试图用一个在其中打开EditText的AlertDialog输入来填充ListView。我的问题是每次我使用EditText在AlertDialog中提供一些东西,然后点击AlertDialog中的我的添加按钮,它用相同的String填充我的所有20个数组。但我希望他只从EditText填充字符串1时间,我可以进一步使用这一项。
这是我的代码
MainActivity.java
public class MainActivity extends Activity {
private static final int DIALOG_ALERT = 10;
ListView list;
Button addBtn;
EditText input;
String[] items;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
addBtn = (Button) findViewById(R.id.addPlanBtn);
input = new EditText(this);
items = new String[20];
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
list.setAdapter(adapter);
addBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Train plan name");
alert.setMessage("enter a name:");
alert.setView(input);
alert.setButton("add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString();
for(int i=0; i < items.length; i++){
items[i] = value;
}
}
});
alert.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/addPlanBtn"
>
</ListView>
<Button
android:id="@+id/addPlanBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="add a new Plan" />
</RelativeLayout>
感谢您的帮助。
答案 0 :(得分:1)
点击监听器中的for循环正在为列表中所有数组成员设置字符串:
for(int i=0; i < items.length; i++){
items[i] = value;
}
要更改有问题的字符串,您需要索引数组的特定成员,例如:
items[0] = value;
这只会改变第一项。在您的情况下,您提取的对话框不依赖于特定的列表项,因此您必须想出一种方法来跟踪正在编辑的列表项。
答案 1 :(得分:0)
得到它,
这是工作代码。
MainActivity.java
public class MainActivity extends Activity {
private static final int DIALOG_ALERT = 10;
ListView list;
Button addBtn;
EditText input;
String[] items;
MyListAdapter adapter;
AlertDialog alert;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
addBtn = (Button) findViewById(R.id.addPlanBtn);
input = new EditText(this);
items = new String[1];
alert = new AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Train plan name");
alert.setMessage("enter a name:");
alert.setView(input);
alert.setButton("add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String value = input.getText().toString();
items[items.length - 1] = value;
adapter = new MyListAdapter(MainActivity.this, items);
list.setAdapter(adapter);
String[] temp = new String[items.length +1];
for(int i = 0; i< items.length; i++)
temp[i] = items[i];
items = temp;
alert.dismiss();
adapter.notifyDataSetChanged();
}
});
addBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
alert.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyListAdapter
public class MyListAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
static class ViewHolder {
public TextView text;
}
public MyListAdapter(Context context, String[] values) {
super(context, R.layout.row_item, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row_item, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.tvItem);
textView.setText(values[position]);
return rowView;
}
}
row_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/tvItem"
android:layout_width="fill_parent"
android:layout_height="50px"
android:textSize="40px" >
</TextView>
</RelativeLayout>
activity_main.xml
<RelativeLayout 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"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/addPlanBtn"
>
</ListView>
<Button
android:id="@+id/addPlanBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="add a new Plan" />
</RelativeLayout>
谢谢大家,他们有时间帮助我。