Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:03:44+00:00 2026-06-04T00:03:44+00:00

after my database is updated I can’t get the adapter notified of changes. I

  • 0

after my database is updated I can’t get the adapter notified of changes. I have to exit the activity and return in order to see the list updated. I’ve tried a few of the ideas I’ve read about but with no luck so I thought I would post my code in hopes someone can see where this bug lies. thanks!

//public class ViolationsList extends ListActivity {
public class ViolationsList extends Activity {

// protected ListView violationsList;
private static final String TAG = "MyActivity";

protected static String violation = "";
protected String graph_or_fql;
protected String myFriendID = "not set";
protected String picture = "";
protected String response = "";
protected String name = "";
boolean violatorSelected;
long friendId = 0;
private Cursor cursor=null;
//this is the array list for the custom violations from the database
private ArrayList<String> myCustomsArrayList = new ArrayList<String>();
//string array for custom violations
private String[] customViols;
//array list for building your three arrays into one array
private ArrayList<String> violationsCompArray = new ArrayList<String>();

String[] violations1;

String[] violations2;



ListView list;
private SpecialAdapter adapter;


//the view holder for the listview      
static class ViewHolder {
    TextView text;
}



//
private class SpecialAdapter extends BaseAdapter {



    //Defining the background color of rows. The row will alternate between green light and green dark.
    private int[] colors = new int[] { 0xff000000, 0xff888888, 0xffCCCCCC };
    private LayoutInflater mInflater;
    Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/Colossalis-Bold.ttf");
    Typeface font2 = Typeface.createFromAsset(getAssets(), "fonts/helveticaneue.ttf");
    //The variable that will hold our text data to be tied to list.

    //private String[] data;
    private ArrayList data = new ArrayList();


    //public SpecialAdapter(Context context, String[] items) {
    public SpecialAdapter(Context context, ArrayList items) {
        mInflater = LayoutInflater.from(context);
        this.data = items;
    }

    @Override
    public int getCount() {
        //return data.length;
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public void notifyDataSetChanged() {
        // TODO Auto-generated method stub
        super.notifyDataSetChanged();
    }

    //A view to hold each row in the list
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row, null);

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.headline);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // Bind the data efficiently with the holder.
        holder.text.setText((CharSequence) data.get(position));


        //Set the background color of the holder
        int colorPos = position % colors.length;


        if ((holder.text.getText().toString().equals("ORIGINALS")) || (holder.text.getText().toString().equals("CUSTOM"))) {
            Log.v(TAG, holder.text.getText().toString());
            colorPos=1;  
            holder.text.setTypeface(font1);
            //holder.text.setTextColor(Color.parseColor("#000000"));


        } else if ("Make your own".equals(holder.text.getText().toString())) {
            Log.v(TAG, holder.text.getText().toString());
            colorPos=2;  



        }else {

            colorPos=0;
            holder.text.setTypeface(font2);
        }


        convertView.setBackgroundColor(colors[colorPos]);

        return convertView;
    }
}




////code for the custom violations database

private void insertCustom(String newViolation) {
    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    SQLiteDatabase db = databaseHelper.getWritableDatabase();

    ContentValues cv = new ContentValues();
    cv.put(DatabaseHelper.TITLE, newViolation);


    db.insert("customViolations", DatabaseHelper.TITLE, cv);

    cursor = db.query("customViolations",null, null, null, null, null, null);
    startManagingCursor(cursor);
    Log.v(TAG, "cursor data = " + cursor.toString());


    cursor.requery();
    db.close();
    updateData();
    adapter.notifyDataSetChanged();



}

private void DeleteCustom(String newViolation)
{
    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    db.delete("customViolations", "myViolation" + "=?", new String[] { violation });



    Log.v(TAG, violation);
    db.close();
    Log.v(TAG, "delete your custom violation");


    cursor.requery();
    updateData();
    adapter.notifyDataSetChanged();


}


/////code to log cursor info   KEEP FOR TESTING!!!!
public void logCursorInfo(Cursor cursor1) {

    Log.i(TAG, "*** Cursor Begin *** " + " Results:" + cursor1.getCount() + " Columns: " + cursor1.getColumnCount());

    //print column names

    String rowHeaders = "|| ";
    for (int i = 0; i < cursor1.getColumnCount(); i++) {

        rowHeaders = rowHeaders.concat(cursor1.getColumnName(i) + " || ");
    }

    //print records
    cursor1.moveToFirst();
    while (cursor1.isAfterLast() == false) {

        String rowResults = "|| ";
        for (int i = 0;  i < cursor1.getColumnCount(); i++) {

            rowResults = rowResults.concat(cursor1.getString(i) + " || ");
        }
        Log.i(TAG, "Row " + cursor1.getPosition() + ": " + rowResults);
        cursor1.moveToNext();
    }


    Log.i(TAG, "***  Cursor End  ***");

}





///code to bring up text entry alert dialog
public void enterText() {


    //insert dialog builder
    AlertDialog.Builder alert = new AlertDialog.Builder(this);


    String Title = getResources().getString(R.string.dialogTitle);
    alert.setTitle(Title);

    //alert.setMessage("Message");

    // Set an EditText view to get user input 
    final EditText input = new EditText(this);
    int maxLength = 60;  
    InputFilter[] FilterArray = new InputFilter[1];  
    FilterArray[0] = new InputFilter.LengthFilter(maxLength);  
    input.setFilters(FilterArray); 
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            Editable value = input.getText();
            // Do something with value!
            insertCustom(value.toString());
            cursor.requery();


        }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
        }
    });

    alert.show();


}

///code to bring up text entry alert dialog
public void editYourViolation() {


    //insert dialog builder
    AlertDialog.Builder yourEdit = new AlertDialog.Builder(this);


    String Title = getResources().getString(R.string.customdialogTitle);
    yourEdit.setTitle(Title);


    yourEdit.setPositiveButton("Use", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do something with value!
            createViolation();
        }
    });

    yourEdit.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Canceled.
            DeleteCustom(violation);
        }
    });

    yourEdit.show();


}



///create the violation
public void createViolation() {


    Intent myIntent = new Intent(getApplicationContext(),
            Selection.class);
    myIntent.putExtra("friendId", friendId);
    myIntent.putExtra("name", name);
    myIntent.putExtra("picture", picture);
    myIntent.putExtra("violation", violation);
    myIntent.putExtra("violatorSelected", violatorSelected);

    startActivity(myIntent);

}



public void querydatabase() {
    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    cursor = db.query("customViolations",null, null, null, null, null, null);

}


public void populateArray() {

    cursor.moveToFirst();
    while(!cursor.isAfterLast()) {
        myCustomsArrayList.add(cursor.getString(cursor.getColumnIndex(DatabaseHelper.TITLE)));
        cursor.moveToNext();
    }
    customViols = (String[]) myCustomsArrayList.toArray(new String[myCustomsArrayList.size()]);

}




private void updateData() {
    querydatabase();
    populateArray();
    for(String i : violations1)
    {
        violationsCompArray.add(i);
    }
    //these are your custom violations
    for(String z : customViols)
    {
        violationsCompArray.add(z);
    }
    //original violations list
    for(String s : violations2)
    {
        violationsCompArray.add(s);
    }





}


@Override
public void onCreate(Bundle savedInstanceState) {




    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    response = extras.getString("API_RESPONSE");
    graph_or_fql = extras.getString("METHOD");
    friendId = extras.getLong("friendId");
    name = extras.getString("name");
    picture = extras.getString("picture");
    violatorSelected = extras.getBoolean("violatorSelected");
    Log.v(TAG, "violations list created");

    setContentView(R.layout.violations_list);

    violations1 = getResources().getStringArray(
            R.array.violations_array1);

    violations2 = getResources().getStringArray(
            R.array.violations_array2);

    ListView list = (ListView) findViewById(R.id.violations_list);
    list.setTextFilterEnabled(true);

    //set up the database connection and cursor for the custom violations

    updateData();

    SpecialAdapter adapter = new SpecialAdapter(this, violationsCompArray); 
    list.setAdapter(adapter);




    ///new onItemClickListener
    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView parent, View view,
                int position, long id) {

            for(String i : customViols)
            {
                Log.v(TAG, i);
            }   



            String text = (String) ((TextView) view).getText();
            violation = text;


            //the make your own value will launch the text editor
            if ("Make your own".equals(violation)) {
                Log.v(TAG, "create the code for Make your own screen");
                enterText();
                //these values are section headers and should have no selection effect                  
            }else if("ORIGINALS".equals(violation) || "CUSTOM".equals(violation))   {
                Toast.makeText(getApplicationContext(), violation,
                        Toast.LENGTH_SHORT).show();
                //test to see if the violation is from the custom violations list   
            }else if(Arrays.asList(customViols).contains(violation)) {

                editYourViolation();  

                //use one of the originals                      
            }else {

                createViolation();


            }
        }
    });



}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:03:46+00:00Added an answer on June 4, 2026 at 12:03 am

    In your onCreate you defined adapter as:

    SpecialAdapter adapter = new SpecialAdapter(this, violationsCompArray);
    

    but then everywhere else you are referencing the private member variable by the same name

    private SpecialAdapter adapter;
    

    so pretty much to fix this you just need to change the line in onCreate to this:

    adapter = new SpecialAdapter(this, violationsCompArray);
    

    that way it is referencing the class member variable rather than the local one you were creating.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a tool which will push changes to database after commit to svn.
After using this code to get my link to show me 3 database entrys
I have a tableView where the user can add contacts to. These contacts get
I have a basic CMS where a user can update a database of articles
So I have discovered that an app can be updated whilst the app is
I have ajax call of action in the controller, after it update the database
I Have database. I get my data from database with LINQ and save in
I use CodeIgniter 2.1.0, i want after insert data in database get a message
How can I identify the time passed after an user updated his account in
why wouldn't there exist a SQLite database after the app was updated to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.