0

I'm attempting to expand my current source code to include additional rows of data (it works fine using just the textview: empty) however when I attempt to add empty2 to allow for more data to be displayed - I'm running into the error:

"Empty2 Cannot Be Resolved Or Is Not A Field"

Even though I've created a textview called empty2

Any idea why this might be happening?

todo_row.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="5sp"
    android:textSize="25sp" >
    <TextView
        android:id="@+id/android:empty2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />

<TextView
        android:id="@+id/android:empty3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />

</TextView>



package com.parse.demo;

import java.util.List;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

public class ToDoListActivity extends ListActivity {
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;

    public static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private List<ParseObject> todos;
    private Dialog progressDialog;

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        // Override this method to do custom remote calls
        protected Void doInBackground(Void... params) {
            // Gets the current list of todos in sorted order
            ParseQuery query = new ParseQuery("TestObject");
            query.orderByDescending("_created_at");

            try {
                todos = query.find();
            } catch (ParseException e) {

            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            ToDoListActivity.this.progressDialog = ProgressDialog.show(ToDoListActivity.this, "",
                    "Loading...", true);
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {

            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void result) {
            // Put the list of todos into the list view
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(ToDoListActivity.this,
                    R.layout.todo_row);
            for (ParseObject todo : todos) {
                adapter.add((String) todo.get("DataI"));
                adapter.add((String) todo.get("DataO"));
            }
            setListAdapter(adapter);
            ToDoListActivity.this.progressDialog.dismiss();
            TextView empty = (TextView) findViewById(android.R.id.empty);
            TextView empty2 = (TextView) findViewById(android.R.id.empty2);
            empty.setVisibility(View.VISIBLE);
            empty2.setVisibility(View.VISIBLE);
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView empty = (TextView) findViewById(android.R.id.empty);
        empty.setVisibility(View.INVISIBLE);
        TextView empty2 = (TextView) findViewById(android.R.id.empty2);
        empty2.setVisibility(View.INVISIBLE);

        new RemoteDataTask().execute();
        registerForContextMenu(getListView());
    }

    private void createTodo() {
        Intent i = new Intent(this, CreateTodo.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent == null) {
            return;
        }
        final Bundle extras = intent.getExtras();

        switch (requestCode) {
        case ACTIVITY_CREATE:
            new RemoteDataTask() {
                protected Void doInBackground(Void... params) {
                    String DataI = extras.getString("DataI");
                    String DataO = extras.getString("DataO");
                    ParseObject todo = new ParseObject("Todo");
                    todo.put("DataI", DataI);
                    todo.put("DataO", DataO);
                    try {
                        todo.save();
                    } catch (ParseException e) {
                    }

                    super.doInBackground();
                    return null;
                }
            }.execute();
            break;
        case ACTIVITY_EDIT:
            // Edit the remote object
            final ParseObject todo;
            todo = todos.get(extras.getInt("position"));
            todo.put("DataI", extras.getString("DataI"));
            todo.put("DataO", extras.getString("DataO"));

            new RemoteDataTask() {
                protected Void doInBackground(Void... params) {
                    try {
                        todo.save();
                    } catch (ParseException e) {
                    }
                    super.doInBackground();
                    return null;
                }
            }.execute();
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return result;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

            // Delete the remote object
            final ParseObject todo = todos.get(info.position);


            new RemoteDataTask() {
                protected Void doInBackground(Void... params) {
                    try {
                        todo.delete();
                    } catch (ParseException e) {
                    }
                    super.doInBackground();
                    return null;
                }
            }.execute();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case INSERT_ID:
            createTodo();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, CreateTodo.class);

        i.putExtra("DataI", todos.get(position).getString("DataI").toString());
        i.putExtra("DataO", todos.get(position).getString("DataO").toString());
        i.putExtra("position", position);


        startActivityForResult(i, ACTIVITY_EDIT);
    }

}

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ListView
        android:id="@+id/android:list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/android:empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />

<TextView
        android:id="@+id/android:empty3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
<TextView
        android:id="@+id/android:empty5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty" />
</LinearLayout>

SOURCE AFTER ANSWER EDITS:

import java.util.List;

import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class ToDoListActivity extends ListActivity {


    TextView empty;
    TextView empty2;
    private static final int ACTIVITY_CREATE = 0;
    private static final int ACTIVITY_EDIT = 1;

    public static final int INSERT_ID = Menu.FIRST;
    private static final int DELETE_ID = Menu.FIRST + 1;

    private List<ParseObject> todos;
    private Dialog progressDialog;

    private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
        // Override this method to do custom remote calls
        public void setVisibility() {
               empty.setVisibility(View.VISIBLE);
               empty2.setVisibility(View.VISIBLE);  

        }
        protected void doInBackground(Void... params) {
            // Gets the current list of todos in sorted order
            ParseQuery query = new ParseQuery("TestObject");
            query.orderByDescending("_created_at");

            try {
                todos = query.find();
            } catch (ParseException e) {

                return;

            }




            runOnUiThread(new Runnable() {
                   public void run() {


            //      
                   }});
        }

        @Override
        protected void onPreExecute() {
            ToDoListActivity.this.progressDialog = ProgressDialog.show(ToDoListActivity.this, "",
                    "Loading...", true);
            super.onPreExecute();
        }

        @Override
        protected void onProgressUpdate(Void... values) {

            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Void result) {
            // Put the list of todos into the list view
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(ToDoListActivity.this,
                    R.layout.todo_row);
            for (ParseObject todo : todos) {
                adapter.add((String) todo.get("DataI"));
                adapter.add((String) todo.get("DataO"));
                adapter.add((String) todo.get("DataRSSI"));
                adapter.add((String) todo.get("DataSSID"));
                adapter.add((String) todo.get("DataTIME"));
                adapter.add((String) todo.get("DataRESTRICTED"));
            }
            setListAdapter(adapter);
            ToDoListActivity.this.progressDialog.dismiss();
        //  TextView empty = (TextView) findViewById(android.R.id.empty);
    //      empty.setVisibility(View.VISIBLE);

        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_new);

            empty = (TextView) findViewById(android.R.id.empty);
            empty.setVisibility(View.INVISIBLE);
        //    empty2 = (TextView) findViewById(android.R.id.empty2);
         //   empty2.setVisibility(View.INVISIBLE);

        new RemoteDataTask().execute();
        registerForContextMenu(getListView());
    }

    private void createTodo() {
        Intent i = new Intent(this, CreateTodo.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (intent == null) {
            return;
        }
        final Bundle extras = intent.getExtras();

        switch (requestCode) {
        case ACTIVITY_CREATE:
            new RemoteDataTask() {
                protected void doInBackground(Void... params) {
                    String DataI = extras.getString("DataI");
                    String DataO = extras.getString("DataO");
                    String DataRSSI = extras.getString("DataRSSI");
                    String DataSSID = extras.getString("DataSSID");
                    String DataTIME = extras.getString("DataTIME");
                    String DataRESTRICTED = extras.getString("DataRESTRICTED");
                    ParseObject todo = new ParseObject("Todo");
                    todo.put("DataI", DataI);
                    todo.put("DataO", DataO);
                    todo.put("DataRSSI", DataRSSI);
                    todo.put("DataSSID", DataSSID);
                    todo.put("DataTIME", DataTIME);
                    todo.put("DataRESTRICTED", DataRESTRICTED);
                    try { todo.save(); } catch (ParseException e) {
                    }

                    super.doInBackground();
                    return;
                }
            }.execute();
            break;
        case ACTIVITY_EDIT:
            // Edit the remote object
            final ParseObject todo;
            todo = todos.get(extras.getInt("position"));
            todo.put("DataI", extras.getString("DataI"));
            todo.put("DataO", extras.getString("DataO"));
            todo.put("DataRSSI", extras.getString("DataRSSI"));
            todo.put("DataSSID", extras.getString("DataSSID"));
            todo.put("DataTIME", extras.getString("DataTIME"));
            todo.put("DataRESTRICTED", extras.getString("DataRESTRICTED"));

            new RemoteDataTask() {
                protected void doInBackground(Void... params) {
                    try {
                        todo.save();
                    } catch (ParseException e) {
                    }
                    super.doInBackground();
                    return;
                }
            }.execute();
            break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, 0, R.string.menu_insert);
        return result;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

            // Delete the remote object
            final ParseObject todo = todos.get(info.position);


            new RemoteDataTask() {
                protected void doInBackground(Void... params) {
                    try {
                        todo.delete();
                    } catch (ParseException e) {
                    }
                    super.doInBackground();
                    return;
                }
            }.execute();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case INSERT_ID:
            createTodo();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, CreateTodo.class);

        i.putExtra("DataI", todos.get(position).getString("DataI").toString());
        i.putExtra("DataO", todos.get(position).getString("DataO").toString());
        i.putExtra("DataRSSI", todos.get(position).getString("DataRSSI").toString());
        i.putExtra("DataSSID", todos.get(position).getString("DataSSID").toString());
        i.putExtra("DataTIME", todos.get(position).getString("DataTIME").toString());
        i.putExtra("DataRESTRICTED", todos.get(position).getString("DataRESTRICTED").toString());
        i.putExtra("position", position);


        startActivityForResult(i, ACTIVITY_EDIT);
    }

}

PROBLEMS

Description Resource    Path    Location    Type
Void methods cannot return a value  ToDoListActivity.java   /NFC Linking Manager/src/com/nfc/linkingmanager line 56 Java Problem
The return type is incompatible with AsyncTask<Void,Void,Void>.doInBackground(Void[])   ToDoListActivity.java   /NFC Linking Manager/src/com/nfc/linkingmanager line 47 Java Problem
3
  • You cannot call TextView empty2 = (TextView) findViewById(android.R.id.empty2); within an AsyncTask. Instead you should onPostExecute(...) invoke a method in the calling Activity to change View visibility or whatever else you wish you do. Commented Apr 17, 2013 at 15:18
  • How can this be implemented? (Im quite new to Android Development - this is my first app) Commented Apr 17, 2013 at 15:21
  • Doesnt the line: empty2.setVisibility(View.VISIBLE); in the onPostExecute(...) already handle this? Commented Apr 17, 2013 at 15:30

1 Answer 1

1

Since you are updating the UI, you will need to use the runOnUiThread(...) method within the AsyncTask.

runOnUiThread(new Runnable() {
   public void run() {
      setVisibility();
   }
});

Where you would need to define the setVisibility() method in the ToDoListActivity Activity. eg.

setVisibility() {
   empty.setVisibility(View.VISIBLE);
   empty2.setVisibility(View.VISIBLE);
}

Also, it is best to declare the UI objects in the global scope and instantiate then in the onCreate(...) method. eg.

public class ToDoListActivity extends ListActivity {
   ...
   TextView empty;
   TextView empty2;
   ...
   @Override
   public void onCreate(Bundle savedInstanceState) {
    ...
    empty = (TextView) findViewById(android.R.id.empty);
    empty.setVisibility(View.INVISIBLE);
    empty2 = (TextView) findViewById(android.R.id.empty2);
    empty2.setVisibility(View.INVISIBLE);
    ...
}

EDIT

You need to modify the onPostExecute(...) method in the AsyncTask as below:

protected void onPostExecute(Void result) {
// Put the list of todos into the list view
ArrayAdapter<String> adapter = new ArrayAdapter<String>(ToDoListActivity.this,
        R.layout.todo_row);
for (ParseObject todo : todos) {
    adapter.add((String) todo.get("DataI"));
    adapter.add((String) todo.get("DataO"));
    adapter.add((String) todo.get("DataRSSI"));
    adapter.add((String) todo.get("DataSSID"));
    adapter.add((String) todo.get("DataTIME"));
    adapter.add((String) todo.get("DataRESTRICTED"));
}
setListAdapter(adapter);
ToDoListActivity.this.progressDialog.dismiss();
/* REMOVE BELOW LINES */
//TextView empty = (TextView) findViewById(android.R.id.empty);
//empty.setVisibility(View.VISIBLE);
//TextView empty2 = (TextView) findViewById(android.R.id.empty2);
//empty2.setVisibility(View.VISIBLE);

}

As well as the runOnUiThread(...) method

runOnUiThread(new Runnable() {
   public void run() {
  setVisibility();
  /* REMOVE BELOW LINES */
  //empty.setVisibility(View.VISIBLE);
  //empty2.setVisibility(View.VISIBLE);
}
});

And I do not see the setVisibility() method in your ToDoListActivity class.

UPDATE

protected Void doInBackground(Void... params) {
        // Gets the current list of todos in sorted order
        ParseQuery query = new ParseQuery("TestObject");
        query.orderByDescending("_created_at");

        try {
            todos = query.find();
        } catch (ParseException e) {

        }

        runOnUiThread(new Runnable() {
               public void run() {
                  setVisibility();
               }});

        return null; // RETURN STATEMENT HERE
    }
Sign up to request clarification or add additional context in comments.

22 Comments

I edited my source code above to include the Run On UI Thread but I'm getting several errors (in eclipse) regarding it's implementation : ( (I must have done something wrong)
Can you post the error logs. Also, you did not remove the code in onPostExecute(...) where the TextViews are changed. Also, no setVisibility() method definition in Activity, among other things.
All issues are resolved except " unreachable code on my runonuithread" : )
I edited my source code : ) [thanks again for helping me with this] I'm still getting 3 errors though : ( [I added my problems log above]
Refer to EDIT 2 in answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.