Greetings

We need to create a filter, where the input is an array of strings, in the list we tick the necessary strings, then we return the array with the selected strings. I tried to implement it through another activation, but there were problems with returning the array to the first form. How can I fix this? Or, ideally, get rid of the second activation ...

Thank.

public class filter extends Activity implements OnClickListener { final String LOG_TAG = "myLogs"; String[] itemOwner; ListView lvMain; String[] names; String[] returnList; int i, j; /** Called when the activity is first created. */ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.filter); itemOwner = new String[getIntent().getExtras().getInt("itemCount")]; //хапаем из первого активити массив itemOwner = getIntent().getExtras().getStringArray("itemOwner"); lvMain = (ListView) findViewById(R.id.lvMain); // устанавливаем режим выбора пунктов списка lvMain.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // Создаем адаптер, используя массив из файла ресурсов ArrayAdapter < String > adapter = new ArrayAdapter < String > (this, android.R.layout.simple_list_item_multiple_choice, itemOwner); lvMain.setAdapter(adapter); Button btnChecked = (Button) findViewById(R.id.btnChecked); btnChecked.setOnClickListener(this); names = itemOwner; } public void onClick(View arg0) { // пишем в лог выделенные элементы SparseBooleanArray sbArray = lvMain.getCheckedItemPositions(); returnList = new String[sbArray.size()]; for (int i = 0; i < sbArray.size(); i++) { int key = sbArray.keyAt(i); if (sbArray.get(key)) returnList[i] = names[key]; } Intent intent = new Intent(this, GraphPrintActivity.class); //пытаемся возвратить выбранные элементы intent.putExtra("returnList", returnList); // в ключ returnList пихаем наш массив intent.putExtra("returnListCount", returnList.length); startActivity(intent); } } 
  • So you want to say that before that you started startActivityForResult (...)? In the key we push our array? First create an itemOwner array, and then take it from Extras? - rasmisha
  • one
    instead of another activation, you can call a custom dialog with a list, it will be more beautiful - LackOfKnowledge
  • @alvin Dialogue prettier ?? - rasmisha
  • aesthetically beautiful - instead of creating a whole activity - you have a dialogue - LackOfKnowledge
  • In general, I do not agree, sometimes the dialogue looks worse than the activation - rasmisha

2 answers 2

The simplest option: instead of String[] returnList; write public static String[] returnList; and from the second activation you will have access to this list through filter.returnList = ...;

  • so did) thanks! - baralgin1003

So you need a ListView in Android: Customizing lists

UPD In the general approach, imkho, with the second is bad from the very beginning. All the same, I would replace the adapter and simply update the existing activations for a multiple selection, and then process the list of selected ones.