2016-11-17 6 views
5

से फ्रैगमेंट ऐरेएडाप्टर में जोड़ना मैं एंड्रॉइड स्टूडियो में प्रोटोटाइप सीख रहा हूं, और मैं एक स्नैग तक पहुंच गया हूं जिसके लिए मुझे कोई जवाब नहीं मिल रहा है।गतिविधि AlertDialog

मेरे पास एक ऐसी गतिविधि है जो एक कस्टम व्यूएडाप्टर को सूची दृश्य के रूप में प्रदर्शित करती है। मैं ListView में आइटम को उन पर क्लिक करके और परिणामस्वरूप AlertDialog में टाइप करके संपादित कर सकता हूं। एक ऐड बटन भी है जिसे मैं दबा सकता हूं, और यह एक समान अलर्टडिअलॉग लाता है, लेकिन जब मैं सहेजता हूं तो सूची दृश्य में कुछ भी जोड़ा नहीं जाता है। मैं एक नया ArrayAdapter आइटम के रूप में सहेजने के लिए AlertDialog पाठ इनपुट कैसे प्राप्त करूं?

मैंने पाया है कि अधिकांश उदाहरणों ने सीधे फ्रैगमेंट के माध्यम से गतिविधि में सीधे ऐरेएडाप्टर को तुरंत चालू कर दिया है।

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

क्या कोई कारण है कि आपके पास गतिविधि बनाम खंड में एफएबी है? – BlackHatSamurai

+0

@ग्रीनैप्स, मैंने थोड़ा सा शब्द बदल दिया - क्या अब यह स्पष्ट है? – 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(); 
     } 
    }); 

कुछ इस तरह करने से, आप आंतरिक श्रेणी आपके द्वारा बनाए गए में एडाप्टर के लिए संदर्भ पारित कर सकते हैं, और फिर इसे अद्यतन करें। उम्मीद है की यह मदद करेगा।

संबंधित मुद्दे