Question

I have a list view which contains a TextView for each list item. I need to take a picture onItemClick of the listView. onActivityResult of the ACTION_IMAGE_CAPTURE intent and updating the listview , I start another activity for result. The problem I am facing is that all the textviews in my list view are getting reset when I come back to the activity onActivityResult from the second activity. Can you please help me with this. This is my onItemClick

 public void onItemClick(AdapterView<?> arg0, View v, int index, long arg3) {
                selectedIndex = index; //selectedIndex is a class level variable
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = createImageFile();
                if (f != null) {
                    imageFileName = f.getAbsolutePath();
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                }
                startActivityForResult(takePictureIntent, 1);
            }
        }

This is my onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1 && resultCode == RESULT_OK) {
        View v = listView.getChildAt(selectedIndex
                - listView.getFirstVisiblePosition());
        TextView textView = (TextView) v.findViewById(R.id.textView1);
        int quantity = Integer
                .parseInt(textView.getText().toString().trim().length() > 0 ? quantityTV
                        .getText().toString() : getString(R.string._0));
        quantity++;
        textView.setText(String.valueOf(quantity));
        listViewAdapter.notifyDataSetChanged();
        Intent intent = new Intent();
        intent.setClass(getApplicationContext(), NotificationActivity.class);
        intent.putExtra("Value1", "0");
        startActivityForResult(intent, 100);
    }
    else if (requestCode == 100) {
       // do nothing
    }
}
Was it helpful?

Solution

While you're updating the content of the text view here, you're not updating the data that backs the adapter for your list view. This means when your activity comes back into view (after the second startActivityForResult) it's redrawing itself with the old data.

Instead of updating the view directly, you should update the data that backs the adapter. Something like this; you'll have to modify it to suit your code.

 if (requestCode == 1 && resultCode == RESULT_OK) {
    List<Integer> adapterData = listViewAdapter.getQuantities();
    int quantity = adapterData.get(selectedIndex) + 1;
    adapterData.set(selectedIndex, quantity);
    listViewAdapter.setQuantities(adapterData);
    Intent intent = new Intent();
    intent.setClass(getApplicationContext(), NotificationActivity.class);
    intent.putExtra("Value1", "0");
    startActivityForResult(intent, 100);
}

And in your adapter, you'd have something like this:

public List<Integer> getQuantities() {
    return mQuantities;
}

public void setQuantities(List<Integer> quantities) {
    mQuantities = quantities;
    notifyDataSetChanged();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top