简体   繁体   中英

How do I add values from EditText to a custom ListView?

I've been struggling with adding entries to a custom ListView. Here is the code so far on the fragment where the ListView is going to be.

public class f2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_1, container, false);
 }
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    Bundle pending = getArguments();
    String Number = pending.getString("Num");
    String Message = pending.getString("Mess");
    ArrayList<String> values = new ArrayList<String>();
    values.add(Number);

    ListView yourListView = (ListView) getActivity().findViewById(R.id.listnew);

    MySimpleArrayAdapter customAdapter = new MySimpleArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, values);
    yourListView.setAdapter(customAdapter);
    customAdapter.notifyDataSetChanged();
    super.onActivityCreated(savedInstanceState);
}
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
      Bundle pending;
      private final Context context;
      private final ArrayList<String> values;

      public MySimpleArrayAdapter(Context context, int simpleListItem1, ArrayList<String> values) {
        super(context, R.layout.rowlayout);
        this.context = context;
        this.values = values;
        this.pending = getArguments();
      }

      @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.rowlayout, parent, false);
        TextView textViewNum = (TextView) rowView.findViewById(R.id.label);
        textViewNum.setText(values.get(position));
        return rowView;
      }
    } 

}

The Bundle is where the values from the EditText are coming from. Oh yeah and the bundle has already been checked to with toast messages and other ways to make sure that the arguments are coming through. If someone could help, it would be greatly appreciated.

The problem with the code that is shown here doesn't show anything on the activity even when there are Strings in the bundle.

Here's how you can do it:

First thing;

Put ArrayList values = new ArrayList(); before the onActivityCreated().

Second:

If you are using intent, do this :

Intent intentBundle = getActivity().getIntent();
        String question_cat = intentBundle.getStringExtra("question_cat");
        String Number = intentBundle.getStringExtra("Num");
        String Message = intentBundle.getStringExtra("Mess");
        Log.i("Result : ", Number );
        values.add(Number);

If using Bundle:

     Bundle extras = getIntent().getExtras();
     if (extras != null) 
     {
     String Number = extras.getString("Num");
     String Message = extras.getString("Mess");
     values.add(Number);
     }

Maybe this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM