Reputation: 21
im trying to fill a ListView with input from a AlertDialog which open an EditText in it. My problem is every time i give something in the AlertDialog with the EditText and click on my add button from the AlertDialog it fills all the 20 sets of my Array with the same String.But i want the he fill the String from the EditText only 1 time and i can work futher with this 1 item.
here is my code
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>
Thank you for any help.
Upvotes: 0
Views: 4974
Reputation: 21
got it,
here is the working code.
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>
Thank you all, who got the time to help me.
Upvotes: 0
Reputation: 651
This for loop in your on click listener is setting the string for all of the members of your array that underlies your list:
for(int i=0; i < items.length; i++){
items[i] = value;
}
To change just the string in question you'll want to index a particular member of the array, e.g.:
items[0] = value;
That will change only the first item. In your case, the dialog that you pull up isn't tied to a specific list item, so you'll have to come up with a way of keeping track of which list item is being edited.
Upvotes: 1