2

I am new to Android. Now I am developing a project. My project has 2 edittext, 1 button, and a custom listview. When I click the ADD button, I want to display my edittext data in a custom listview.

MainActivity.java

public class MainActivity extends AppCompatActivity {

Button btn;
EditText edt1,edt2;
ListView itemsListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn=(Button) findViewById(R.id.add_id);
    edt1=(EditText) findViewById(R.id.credit_id);
    edt2=(EditText) findViewById(R.id.gpa_id);
    itemsListView  = (ListView)findViewById(R.id.list_view_items);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CustomListAdapter adapter = new 
CustomListAdapter(MainActivity.this, generateItemsList());
            itemsListView.setAdapter(adapter);
            edt1.setText("");
            edt2.setText("");
        }
    });
}
private ArrayList<Item> generateItemsList() {

    String itemName=edt1.getText().toString();
    String itemDescription=edt2.getText().toString();
    ArrayList<Item> list= new ArrayList<>();
    Item item=new Item(itemName,itemDescription);
    item.getItemName();
    item.getItemDescription();
    list.add(item);
    return list;
}
}

CustomListAdapter.java

public class CustomListAdapter extends BaseAdapter {

private Context context;
private ArrayList<Item> items;
public CustomListAdapter(Context context, ArrayList<Item> items) {
    this.context = context;
    this.items = items;
}
@Override
public int getCount() {
    return items.size();
}
@Override
public Object getItem(int position) {
    return items.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder;
    if (convertView == null) {
        convertView = 
LayoutInflater.from(context).inflate(R.layout.layout_list_view_row_items, 
parent, false);
        viewHolder = new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    Item currentItem = (Item) getItem(position);
    viewHolder.itemName.setText(currentItem.getItemName());
    viewHolder.itemDescription.setText(currentItem.getItemDescription());
    return convertView;
}
private class ViewHolder {
    TextView itemName;
    TextView itemDescription;
    public ViewHolder(View view) {
        itemName = (TextView)view.findViewById(R.id.text_view_item_name);
        itemDescription =(TextView)view.findViewById(R.id.text_view_item_description);
    }
}
}
5
  • What is the correct way? @Arbaz.in Commented May 28, 2019 at 10:13
  • Is Item a custom object that you created? Commented May 28, 2019 at 10:24
  • @Shihab007 post Item model class Commented May 28, 2019 at 11:09
  • @Shihab007 remove the for loop. Commented May 28, 2019 at 11:14
  • @Shihab007 your looping concept is wrong Commented May 28, 2019 at 11:40

1 Answer 1

2

Corrected your method , please check

private ArrayList<Item> generateItemsList(){
    String itemName=edt1.getText().toString();
    String itemDescription=edt2.getText().toString();
    ArrayList<Item> list=new ArrayList<String>();

    Item item=new Item();
    item.setName(itemName);
    item.setDescription(itemDescription);
    list.add(item);

    return list;
}

this method will return the current item name and item description in a list.

Declare a global arraylist to save multiple entries Like this

public class MainActivity extends AppCompatActivity {

    Button btn;
    EditText edt1, edt2;
    ListView itemsListView;
    ArrayList<Item> list = new ArrayList<>();

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

        btn = (Button) findViewById(R.id.add_id);
        edt1 = (EditText) findViewById(R.id.credit_id);
        edt2 = (EditText) findViewById(R.id.gpa_id);
        itemsListView = (ListView) findViewById(R.id.list_view_items);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String itemName = edt1.getText().toString();
                String itemDescription = edt2.getText().toString();

                Item item = new Item(itemName, itemDescription);
                item.getItemName();
                item.getItemDescription();
                list.add(item);
                CustomListAdapter adapter = new
                        CustomListAdapter(MainActivity.this, list);
                itemsListView.setAdapter(adapter);
                edt1.setText("");
                edt2.setText("");
            }
        });
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks.. actually i want to display my data in custom listview.Your method not work in listview. @Quick learner
can u tell me what is the problem ?
Data is not add listview. Just update data same place..exp: 1st i input 5 and 6 click ADD button display 5,6 in below listview...2nd i input 10,30 it is not add listview..just 5 replaces 10 and 6 replaced 30.. @Quick learner
check my answer now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.