2016-11-17 97 views
5

我正在学习Android Studio中的原型,并且我遇到了一个我似乎无法找到答案的障碍。从活动AlertDialog添加到片段ArrayAdapter

我有一个活动,显示一个自定义ArrayAdapter作为ListView。我可以编辑ListView中的项目,方法是单击它们并键入AlertDialog。还有一个我可以按下的添加按钮,它带来了一个类似的AlertDialog,但是当我保存时没有任何东西被添加到ListView中。如何让AlertDialog文本输入保存为新的ArrayAdapter项目?

我发现的大多数例子都直接在Activity中实例化了ArrayAdapter,而不是像我做过的那样通过Fragment。

MainActivity.java

public class MainActivity extends AppCompatActivity { 

private static final String TAG = "MainActivity"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { //initialize the activity 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); //establish where the layout will come from 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); //creates a toolbar 
    setSupportActionBar(toolbar); 

    if (findViewById(R.id.fragment_container) != null) { 
     if (savedInstanceState != null) { 
      return; 
     } 
     //creates the first fragment dynamically, so it can be replaced 
     Fragment firstFragment = new MainActivityFragment(); 
     firstFragment.setArguments(getIntent().getExtras()); 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.fragment_container, firstFragment).commit(); 
    } 

    //This creates the Floating Action Button 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      createDialog(); 
     } 

     private void createDialog() { 
      ArrayList<User> users = new ArrayList<User>(); 

      // create an AlertDialog that'll come up when the add button is clicked 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); 

      // set title 
      alertDialog.setTitle("Add item"); 

      final EditText input = new EditText(MainActivity.this); //uses the EditText from dialog_set 
      input.setInputType(InputType.TYPE_CLASS_TEXT); //makes the dialog ask for plain text input 
      alertDialog.setView(input); 

      // set up buttons 

      alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        String textInput = input.getText().toString(); //saves user text as a string 
        Log.d(TAG, textInput); // records input as a log 
        CustomUsersAdapter.this.add(textInput); 
       } 
      }); 

      alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 

      // show it 
      alertDialog.show(); 
     } 
    }); 
} 

MainActivityFragment.java

public class MainActivityFragment extends Fragment { 

@BindView(R.id.lvUsers) ListView listView; 

public MainActivityFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_main, container, false); 
    ButterKnife.bind(this, view); 

    ArrayList<User> arrayOfUsers = new ArrayList<User>(); 
    arrayOfUsers.add(new User("Person 1", "Hometown 1")); 
    arrayOfUsers.add(new User("Person 2", "Hometown 2")); 
    arrayOfUsers.add(new User("Person 3", "Hometown 3")); 
    arrayOfUsers.add(new User("Person 4", "Hometown 4")); 
    arrayOfUsers.add(new User("Person 5", "Hometown 5")); 
    // Create the adapter to convert the array to views 
    CustomUsersAdapter adapter = new CustomUsersAdapter(getContext(), arrayOfUsers); 
    // Attach the adapter to a ListView 
    listView.setAdapter(adapter); 

    return view; 
} 

CustomUsersAdapter.java

public class CustomUsersAdapter extends ArrayAdapter<User> { 
private ArrayList<User> users; 

public CustomUsersAdapter(Context context, ArrayList<User> users) { 
    super(context, 0, users); 
    this.users = users; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    // Get the data item for this position 
    User user = getItem(position); 
    // Check if an existing view is being reused, otherwise inflate the view 
    if (convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false); 
    } 
    // Lookup view for data population 
    TextView tvName = (TextView) convertView.findViewById(R.id.tvName); 
    TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown); 
    // Populate the data into the template view using the data object 
    tvName.setText(user.name); 
    tvHome.setText(user.hometown); 

    convertView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      createDialog(position); 
     } 
    }); 

    // Return the completed view to render on screen 
    return convertView; 
} 

protected void add(String textInput) { 
    add(new User(textInput, "Incomplete")); 
} 

private void createDialog(final int position) { 
    // create an AlertDialog that'll come up when text is clicked 
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(
      getContext()); 

    // set title 
    alertDialog.setTitle("Edit item"); 

    final EditText input = new EditText(getContext()); //uses the EditText from dialog_set 
    input.setInputType(InputType.TYPE_CLASS_TEXT); //makes the dialog ask for plain text input 
    alertDialog.setView(input); 

    // set up buttons 

    alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      String textInput = input.getText().toString(); //saves user text as a string 
      users.get(position).name = textInput; 
      notifyDataSetChanged(); 
     } 
    }); 

    alertDialog.setNeutralButton("Complete", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      users.get(position).hometown = "Complete"; 
      notifyDataSetChanged(); 
     } 
    }); 

    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 

    // show it 
    alertDialog.show(); 
} 

ListView的布局(从fragment_main.xml)

<ListView 
    android:id="@+id/lvUsers" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" > 

</ListView> 
+0

'我可以通过单击它们来编辑ArrayAdapter中的项目。不可能。数组适配器不是gui元素,没有视图,因此不可点击。请重新说明。 – greenapps

+0

是否有一个原因,你在活动中的FAB与片段? – BlackHatSamurai

+0

@greenapps,我改了一下措辞 - 现在更清楚了吗? – songbird813

回答

0

有,你有一对夫妇的问题。首先,您正尝试使用静态调用来更新适配器,但不能这样做。您还需要调整您的add方法。它应该看起来像这样:

protected void add(String textInput) { 
    this.users.add(new User(textInput, "Incomplete")); 
} 

但这只是问题的一部分。您需要将对象引用传递到onClickListener。您正试图进行静态调用。这不起作用。我可能会考虑放置浮动操作按钮。如果你这样做,那么你可以很容易地引用传递给适配器:

public class MainActivityFragment extends Fragment { 

private final CustomUsersAdapter adapter; 
@BindView(R.id.lvUsers) ListView listView; 


public MainActivityFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_main, container, false); 
    ButterKnife.bind(this, view); 

    ArrayList<User> arrayOfUsers = new ArrayList<User>(); 
    arrayOfUsers.add(new User("Person 1", "Hometown 1")); 
    arrayOfUsers.add(new User("Person 2", "Hometown 2")); 
    arrayOfUsers.add(new User("Person 3", "Hometown 3")); 
    arrayOfUsers.add(new User("Person 4", "Hometown 4")); 
    arrayOfUsers.add(new User("Person 5", "Hometown 5")); 
    // Create the adapter to convert the array to views 
    adapter = new CustomUsersAdapter(getContext(), arrayOfUsers); 
    // Attach the adapter to a ListView 
    listView.setAdapter(adapter); 

    return view; 
} 

//This creates the Floating Action Button 
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      createDialog(); 
     } 

     private void createDialog() { 
      ArrayList<User> users = new ArrayList<User>(); 

      // create an AlertDialog that'll come up when the add button is clicked 
      AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this); 

      // set title 
      alertDialog.setTitle("Add item"); 

      final EditText input = new EditText(MainActivity.this); //uses the EditText from dialog_set 
      input.setInputType(InputType.TYPE_CLASS_TEXT); //makes the dialog ask for plain text input 
      alertDialog.setView(input); 

      // set up buttons 

      alertDialog.setPositiveButton("Save", new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        String textInput = input.getText().toString(); //saves user text as a string 
        Log.d(TAG, textInput); // records input as a log 
        adapter.add(textInput); 
       } 
      }); 

      alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 

      // show it 
      alertDialog.show(); 
     } 
    }); 

通过做这样的事情,你可以通过参考适配器插入您创建的内部类,然后更新它。希望这可以帮助。