5

In my main activity I have defined two views and a menu:

Views: 1. Custom view game 2. Button btn

Menu: 1. Open item for opening a file

The menu layout is defined in a different activity

Basically, when the main activity starts, it draws the custom view that doesn't have anything, and the button. Then I load the file using the open in the menu. It brings up a list of files in the /sdcard dir and I pick the file from the list. Then the onListItemClick() parses the file and updates the data structure in the custom view. Then I press back to return to the main activity. Now I'd expect to see the redraw after pressing the button but it doesn't seems to do anything. I've implemented the OnClickListener() for the button and call the game.invalidate(). But it doesn't seem to redraw the custom view immediately upon button click. I have to exit the app( back button) then reopen the app to see the redraw. I've read other posts regarding the invalidate() not redrawing the view immediately. It only pushes it to a queue and Android will take action once it's free to do so. I guess thing I don't understand is that what in my app is holding Android from taking action.

Snippets of code

//Main Activity
public class someActivity extends Activity {

someView myView;

@Override
public void onCreate(Bundle savedInstanceState)
{
     super.onCreate(savedInstanceState);
     LinearLayout layout = new LinearLayout(this);
     layout.setOrientation(LinearLayout.VERTICAL);
     Button btn1 = new Button(this);
     btn1.setOnClickListener(forward);
     myView = new someView(this);
     layout.addView(myView);
     layout.addView(btn1);
     setContentView(layout);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
    case R.id.open:
        Intent myIntent = new Intent();
        myIntent.setClass(this, DirectoryBrowser.class);
        startActivityForResult(myIntent,0);
        return true;
    case R.id.save:
        return true;
    default:
        return super.onOptionsItemSelected(item);

    }
}

private OnClickListener forward = new OnClickListener()
{
    public void onClick(View v)
    {
        myView.invalidate();

    }
};

//DirectoryBrowser Activity
public class DirectoryBrowser extends ListActivity 
{
private List<String> items = null;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.open);
    getFiles(new File("/sdcard").listFiles());
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id){
    int selectedRow = (int)id;
    if(selectedRow == 0){
        getFiles(new File("/sdcard").listFiles());
    }else{
        File file = new File(items.get(selectedRow));
        if(file.isDirectory()){
            getFiles(file.listFiles());
        }else{
                try
                {
                                         //Parse file and update myView.data
                                    }
                catch (FileNotFoundException e) 
                {
                    Log.e("ERROR","File Not Found");
                }
        }
    }
}

//Custom View someView
//...

@Override
public void onDraw(Canvas canvas) 
{
         //Redraw view here

}
kvzrock
  • 437
  • 1
  • 4
  • 10

2 Answers2

7

I've seen 2 activities, so I guess postInvalidate() may is what you are looking for.

http://developer.android.com/reference/android/view/View.html#invalidate%28%29 http://developer.android.com/reference/android/view/View.html#postInvalidate%28%29

Edit: Why isn't view.invalidate immediately redrawing the screen in my android game here is an intresting thread, have you seen it?

Community
  • 1
  • 1
sschrass
  • 7,014
  • 6
  • 43
  • 62
  • I'm calling the invalidate() in the same activity where the custom view is located. So I probably don't need the postInvalidate(). Corret me if I'm wrong. – kvzrock Mar 18 '12 at 18:25
  • posted a link from StackOverflow. the repaint() method looks intresting. – sschrass Mar 19 '12 at 07:36
0

Instead of myView.invalidate() try v.invalidate() because I guess your button view will be passed as the parameter to the onclick() method .

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
arun
  • 463
  • 4
  • 4