简体   繁体   English

如何将EditText中的值添加到自定义ListView中?

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

I've been struggling with adding entries to a custom ListView. 我一直在努力向自定义ListView添加条目。 Here is the code so far on the fragment where the ListView is going to be. 到目前为止,这是ListView所在片段的代码。

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. 捆绑包是EditText值的来源。 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(); 把ArrayList的值= new ArrayList(); before the onActivityCreated(). 在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. 也许这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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