문제

I have a EditText box which is used to input names. Once the names are inputed and the add button is clicked I need the name to be saved into a Array that can be used in the next Activity and to also clear the EditText box. I think I have this working but not to sure if it is correct you can see the code below.

My actuall question is a little different when the add button is clicked I also need it to show the name that was just added next to the add button with its own remove button and if more are added they need to appear below like a kind of list view. If the remove button is clicked it needs to then remove the name and remove button from screen and the name out of the array.

Code has been updated I am trying to populate the ListView from a Adapter but with no sucsess so far

enter image description here

I am thinking something like adding a TextView and Button within the onClick method. And when the button Remove is clicked I would need to remove the TextView string from the playerList. I think I would also have to remove the View so it can no longer be seen.

Can any help me out on the code side of this can't seem to figure it out.

public class AddRemove extends Activity {

ArrayList<String> playerList = new ArrayList<String>();
String playerlist[];
ListView listview;



@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addremove);
ListView listView = (ListView)findViewById(R.id.namelistview);
listview.setAdapter(new myAdapter(getApplicationContext(), R.layout.listview_content, list));

Button confirm = (Button) findViewById(R.id.add);
confirm.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText playername = (EditText) findViewById(R.id.userinput);
String name = playername.getText().toString();
playerList.add(name);
playername.setText("");

}});

Button play = (Button) findViewById(R.id.playnow);
play.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent( demo.AddRemove.this, demo.PasswActivity.class);
Bundle extras = new Bundle();
extras.putSerializable( "com.example.playerList", playerList );
i.putExtras( extras );
startActivity( i );

}});
}

class myAdapter extends ArrayAdapter{

List<String> users;
public myAdapter(Context context, int textViewResourceId, List<String> list) {
super(context, textViewResourceId);
// assign list
users = list;
// TODO Auto-generated constructor stub
}

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return super.getView(position, convertView, parent);

}

}
}

listview_content XML

<TextView
    android:id="@+id/playername"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<Button
    android:id="@+id/remove"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Remove" />
도움이 되었습니까?

해결책

You should be using a ListView to display your list of people. Within this List, you would have a layout with 2 components in each row. These would include one TextView and one Button, the name of the person and the button to remove the component. When the button is clicked, remove the item from the Adapter and notify the ListView that it has changed.

When you are configuring your ListView your adapter will need to have a custom Adapter that will be used to configure each row of your ListView.

Update

Sample Code:

http://android.vexedlogic.com/2011/04/02/android-lists-listactivity-and-listview-ii-%E2%80%93-custom-adapter-and-list-item-view/

다른 팁

First of all, you need to initialize each TextView with a reference to the context. So, in your onClick, given that it is in ViewOnClick.java:

public class ViewOnClick extends Activity {
    LinearLayout.LayoutParams layoutParams;
    LinearLayout ll;
    static int i;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button b = (Button)findViewById(R.id.Button01);
        ll = (LinearLayout)findViewById(R.id.ll);
        layoutParams = new LinearLayout.LayoutParams
        (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        b.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                TextView view = new TextView(ViewOnClick.this);             
                view.setText(++i+" view");
                ll.addView(view, layoutParams); 

            }});
    }
}

Second, given that you have a call to setContentView in onCreate which also adds your linearlayout to the view, you do not need to call setContentView(linearLayout) each time the button is clicked.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top