Android ArrayAdapter for ListView method .notifyDataSetChanged() issue - android

I am trying to update ListView after another activity finish()'es with result. Initialization (works correctly):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
objects = databaseHelper.selectObjects();
objectsListView = (ListView) findViewById(R.id.LIST_VIEW_OBJECTS);
objectArrayAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, objects);
objectsListView.setAdapter(objectArrayAdapter);
}
Problem occurs only when Im trying update ListView:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if(requestCode == SECOND_ACTIVITY_REQUEST) {
if (resultCode == RESULT_OK) {
objects = databaseHelper.selectObjects();
objectArrayAdapter.notifyDataSetChanged();
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.TOAST_ERROR_RESULT_CANCELED), Toast.LENGTH_LONG).show();
}
}
}
But this code works just fine:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if(requestCode == SECOND_ACTIVITY_REQUEST) {
if (resultCode == RESULT_OK) {
objects.add(newObject);
objectArrayAdapter.notifyDataSetChanged();
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.TOAST_ERROR_RESULT_CANCELED), Toast.LENGTH_LONG).show();
}
}
}
This code also works fine:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
if(requestCode == SECOND_ACTIVITY_REQUEST) {
if (resultCode == RESULT_OK) {
objects = databaseHelper.selectObjects();
objectArrayAdapter = new ArrayAdapter<Object>(this, android.R.layout.simple_list_item_1, objects);
objectsListView.setAdapter(objectArrayAdapter);
}
if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), getResources().getString(R.string.TOAST_ERROR_RESULT_CANCELED), Toast.LENGTH_LONG).show();
}
}
}
My question is: why first method doesn't work as expected? Second and third seems to be a work round isn't it?

In your first example, you are giving the information to questionnaires variable, which I don't see being related to your listview. onNotifyDataSetChanged() has the same data as before from what I can tell.
The second one works because you are directly changing the objects variable which is connected to your adapter.
EDIT:
"For an ArrayAdapter, notifyDataSetChanged only works if you use the add(), insert(), remove(), and clear() on the Adapter."
notifyDataSetChanged example

Related

ArrayList is null after executing onActivityResult

I have Activity > Fragment > RecyclerView .
I am getting data from server in a ArrayList to inflate recyclerView in onCreate() of fragment.
In recyclerView there is Image with which I open camera and click photo.
But when I return after successful image capture, I get my ArrayList null
onCreate in ShipmentFragment :
ArrayList<ShipmentDetails> shipmentList;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
shipmentList = getShipmentLists();
}
in activity's onActivityResult :
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAMERA_IMAGE_CAPTURE_REQUEST_CODE:
if (resultCode == RESULT_OK) {
ShipmentFragment shipmentFragment = new ShipmentFragment();
shipmentFragment.entryToList();
} else {
Toast.makeText(OrderActivity.this, "Error capturing image", Toast.LENGTH_SHORT).show();
}
break;
}
entryToList() in ShipmentFragment:
public void entryToList() {
// Here I get shipmentList null
int size = shipmentList.size();
}

Execute functions in order in Android

I'm trying to make program with functions.
Code is so simple, but I'm struggling with basic grammar in java.
OnClickListener voiceListener = new OnClickListener() {
public void onClick(View v) {
FA();//I want FB is operated after FA and onActivityResult are finished..
FB();
}
};
private void FA(){...startActivityForResult(intent,check);
}
private void FB(){}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == check && resultCode == RESULT_OK){
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
//
The problem is, when I click the button, FA and FB are operating together and seems like it FA, FB
What I wanted to see was, after FA is performed( intent is passed), the program gets into onActivityResult and results are applied to FB.
Where should I touch for doing this? What am I going to do?
I'm expecting to seeing opinions! :)
Execute the operations of FB() method inside the onActivityResult method, just like this
OnClickListener voiceListener = new OnClickListener() {
public void onClick(View v) {
FA();//I want FB is operated after FA and onActivityResult are finished..
//Don't call FB over here
}
};
//FA method defined here which startActivityForResult
private void FA(){...startActivityForResult(intent,check);
}
//FB method defined here
private void FB(){}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == check && resultCode == RESULT_OK){
results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
//Call FB over here
FB();
}
super.onActivityResult(requestCode, resultCode, data);
}
}
I hope this helps. Still got doubts? Comment below.

onActivityResult not being called in custom Fragment or anywhere else attempted

There have been many successful answers to this question, but there's still something I'm not understanding or that I'm doing incorrectly. One answer is implementing onActivityResult in the host Activity, but I guess I don't know which one that is or I'm following it wrong.
And no, I'm not calling getActivity.startActivityForResult(), just startActivityForResult().
Depending on a selection made in FirstActivity, one or more other selections are possible, created using Fragments. One Fragment is ButtonFragment, which when selected, starts OptionsActivity, like so:
public class ButtonFragment extends Fragment
{
.....
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(resultCode != Activity.RESULT_CANCELED)
{
if(requestCode == OPTIONS_REQUEST_CODE)
{
Bundle extras = data.getExtras();
if (extras != null) {
String selection = (String) extras.get("optionsSelection");
data.setText(selection);
}
}
}
}
private void openOptionsActivity()
{
Intent intent = new Intent(getActivity(), OptionsActivity.class);
intent.putExtra("optionsArray", options);
startActivityForResult(intent, OPTIONS_REQUEST_CODE);
}
}
This fragment is being added in BaseActivity:
public class BaseActivity extends FragmentActivity
{
.....
fm.beginTransaction().add(
R.id.FragmentContainer, frag).commitAllowingStateLoss();
fm.executePendingTransactions();
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
finish();
}
}
Inside OptionsActivity:
public class OptionsActivity extends BaseSpinnerActivity
{
.....
private class OptionsAdapter extends BaseSpinnerAdapter
{
public OptionsAdapter(Context context, Object[] values)
{
super(context, values);
}
protected void fillRow(ViewHolder holder, int position)
{
String option = options[position];
if(option != null) {
holder.text.setText(option);
}
}
protected void onRowSelect(View v)
{
Intent returnIntent = new Intent();
String optionSelection = (String)v.getTag();
returnIntent.putExtra("optionSelected", optionSelection);
setResult(RESULT_OK, returnIntent);
finish();
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
finish();
}
}
The same thing is done inside BaseSpinnerActivity, which extends from ListActivity. (I know, should use FragmentActivity, but I haven't had a chance to convert it yet.)
None of these onActivityResult()'s are being called.
What am I missing or doing wrong? Any help is appreciated!

Listview and adapter for dummies

I seen through all previous question about this topic but cannot get my head around it.It is tested on a Samsung Galaxy S4. This is from my activity_main.xml.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/llListe"
android:orientation="vertical"
>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lstListe"
android:layout_gravity="center"
android:background="#bcffb9" />
</LinearLayout>
This is the MainActivity.java. I have tested the result from the speechRecognizer and it return the spoken word. The problem is to add this string to a ListView. I'm quit new at this so please forgive. WHAT AM I DOING WRONG?
public class MainActivity extends Activity implements View.OnClickListener {
protected static final int REQUEST_OK = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnSpeek).setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,"en-US");
try {
startActivityForResult(intent,REQUEST_OK);
} catch (Exception e) {
Toast.makeText(this, "Unable to initialize speech engine.", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ListView lv;
ArrayList<String> inputString = new ArrayList<String>();
ArrayAdapter<String> adapter;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
inputString = (data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS));
//Toast.makeText(this,"You said "+inputString.get(0), Toast.LENGTH_SHORT).show();
lv = (ListView) findViewById(R.id.lstListe);
adapter = new ArrayAdapter(this,R.id.lstListe,inputString);
lv.setAdapter(adapter);
}
}
}
I think you should put this line super.onActivityResult(requestCode, resultCode, data); in the else condition of the onActivityResult method, so that you're sure your code is executed when the request code is REQUEST_OK.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ListView lv;
ArrayList<String> inputString = new ArrayList<String>();
ArrayAdapter<String> adapter;
if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
inputString = (data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS));
//Toast.makeText(this,"You said "+inputString.get(0), Toast.LENGTH_SHORT).show();
lv = (ListView) findViewById(R.id.lstListe);
adapter = new ArrayAdapter(this,R.id.lstListe,inputString);
lv.setAdapter(adapter);
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}

How to Call onActivityResult in adapter listview?

i'm learning how to change Single item with resultActivity in in my custom Adapter listview. how it's can work ?
this my code when startActivityForResult in custom adapter listview
holder.isi_layout.setOnClickListener(new android.view.View.OnClickListener(){
public void onClick(View v)
{
Intent i = null;
i = new Intent(activity, DetailLaporanActivity.class);
Bundle b = new Bundle();
b.putString("position", Integer.toString(position));
i.putExtras(b);
activity.startActivityForResult(i, mRequestCode);
activity.overridePendingTransition (R.anim.push_left_in, R.anim.push_left_out);
}
});
and this OnResultActivity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==mRequestCode) {
if(resultCode == RESULT_OK){
String position = data.getStringExtra("position");
String status_favorite = data.getStringExtra("status_favorite");
String jumlah_favorite = data.getStringExtra("jumlah_favorite");
String jumlah_komentar = data.getStringExtra("jumlah_komentar");
}
Toast.makeText(getApplicationContext(), "This code Success get Result", Toast.LENGTH_LONG).show();
}
}
When i put OnResultActivity in Adapter, code is error, RESULT_OK get notice Cannot be resolved to a variable,
but if i put in MainActivity , this not error but not get value result, i check with Toast.makeText(getApplicationContext(), "This code Success get Result", Toast.LENGTH_LONG).show(); but no toast,....
anybody help me ? how it's work ?
sorry, for my english...
You added unresolved RESULT_OK so you should set it in DetailLaporanActivity.class like that.
public class DetailLaporanActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(RESULT_OK);
}
}
and than you can use RESULT_OK in your onActivityResult method.
That's because RESULT_OK is a constant of the Activity class. So, you need to qualify it in your Adapter class as so:
Activity.RESULT_OK

Categories

Resources