0

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
    }
}
4

1 回答 1

1

当您在此处更新文本视图的内容时,您并未更新支持列表视图适配器的数据。这意味着当您的活动重新出现时(在第二个之后startActivityForResult),它会使用旧数据重新绘制自己。

您应该更新支持适配器的数据,而不是直接更新视图。像这样的东西;您必须修改它以适合您的代码。

 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);
}

在你的适配器中,你会有这样的东西:

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

public void setQuantities(List<Integer> quantities) {
    mQuantities = quantities;
    notifyDataSetChanged();
}
于 2013-11-04T21:39:30.540 回答