0

I'm quite a beginner on Android studio and I'm trying to make an application. I have one question : I have a Custom ListView and I want to add items to this ListView when I click on a button (open a dialogbox with editText). But I don't succeed! For the moment I succeeded in adding items to a simple List view, but I want now to add items to my custom ListView :

This is my custom ListView (i was inspired by the Internet) :

public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private  String[] web;
private final Integer[] imageId;
public CustomList(Activity context,
                  String[] web, Integer[] imageId) {
    super(context, R.layout.list_single, web);
    this.context = context;
    this.web = web;
    this.imageId = imageId;
 }
@Override
public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View rowView= inflater.inflate(R.layout.list_single, null, true);
    TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
    txtTitle.setText(web[position]);
    imageView.setImageResource(imageId[position]);
    return rowView;
 }

}

This is now my current code which works but just add Items to a "normal" ListView

public class MenuActivity extends Activity {
ListView listplayer=null;
ImageButton addButton = null;
/** Items entered by the user is stored in this ArrayList variable */
ArrayList<String> list = new ArrayList<String>();

/** Declaring an ArrayAdapter to set items to ListView */
ArrayAdapter<String> adapter;



@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);


    listplayer=(ListView) findViewById(R.id.list);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);



    addButton = (ImageButton) findViewById(R.id.buttonaddplayer);
    addButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Create custom dialog object
            final Dialog dialog = new Dialog(MenuActivity.this);
            dialog.setTitle(getResources().getString(R.string.addperson));
            // Include dialog.xml file
            dialog.setContentView(R.layout.dialoglayout);
            // Set dialog title
           // dialog.setTitle("Custom Dialog");

            // set values for custom dialog components - text, image and button
            final EditText text = (EditText) dialog.findViewById(R.id.editTextdialog);


            dialog.show();

            ImageButton validButton = (ImageButton) dialog.findViewById(R.id.imageButtonvalid);
            // if decline button is clicked, close the custom dialog
            validButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    list.add(text.getText().toString());
                    text.setText("");
                    adapter.notifyDataSetChanged();


                    dialog.dismiss();
                }
            });
            listplayer.setAdapter(adapter);





        }
    });

}

Thank you for helping me. In fact I want to do an application where you can add player, but with smth "beautiful" and not the normals List view of android studio

Wazery
  • 15,394
  • 19
  • 63
  • 95
guillaumegg10
  • 53
  • 1
  • 2
  • 10

1 Answers1

0

read this on adapters

check here

figure out where your 'data' is.

onButtonClick() -> insert new item to the 'head' of your adapter's data, calling 'notifyDataSetChanged()' to update the view

Community
  • 1
  • 1
Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43