Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom list view in android giving error while updating data from database in android
    text
    copied!<p>I am trying to add check box and some text view (text view is getting value from database) in customized list view but it is giving error of NullPointerException. I don't know why and what is wrong with my code. Below is my code.</p> <p>My MainActivity Class:</p> <pre><code>public class Classes extends Activity { ImageView imageViewNewClass; ListView mListView; String[] stg1; List&lt;String[]&gt; names2 = null; DataManipulatorClass dataManipulator; CustomAdapter customAdapter; public Classes classes = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.classes); imageViewNewClass = (ImageView) findViewById(R.id.newclass); mListView = (ListView) findViewById(R.id.displaydata); imageViewNewClass.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Classes.this, Class_Create.class); startActivity(intent); } }); Resources res =getResources(); classes = this; dataManipulator = new DataManipulatorClass(this); names2 = dataManipulator.selectAll(); stg1 = new String[names2.size()]; int x = 0; String stg = null; for (String[] name : names2) { stg = "Class Name : " + name[1]; stg1[x] = stg; x++; } customAdapter= new CustomAdapter( classes, stg1,res ); mListView.setAdapter( customAdapter ); customAdapter.notifyDataSetChanged(); mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View item, int position, long id) { Toast.makeText(getApplicationContext(), "Listview item clicked", Toast.LENGTH_LONG).show(); } }); } } </code></pre> <p>CustomAdapterClass.java</p> <pre><code>public class CustomAdapter extends BaseAdapter { /*********** Declare Used Variables *********/ private Activity activity; private String[] data; private static LayoutInflater inflater = null; public Resources res; int i = 0; String[] stg1; List&lt;String[]&gt; names2 = null; DataManipulatorClass dataManipulator; /************* CustomAdapter Constructor *****************/ public CustomAdapter(Activity a, String[] stg1, Resources resLocal) { /********** Take passed values **********/ activity = a; data = stg1; res = resLocal; /*********** Layout inflator to call external xml layout () ***********/ inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } /******** What is the size of Passed Arraylist Size ************/ public int getCount() { if (data.length &lt;= 0) return 1; return data.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public static class ViewHolder { public CheckBox checkBox; public TextView textView; } public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; ViewHolder holder; if (convertView == null) { vi = inflater.inflate(R.layout.check, null); holder = new ViewHolder(); holder.checkBox = (CheckBox) vi.findViewById(R.id.checkBox1); holder.textView = (TextView) vi.findViewById(R.id.selection); vi.setTag(holder); } else holder = (ViewHolder) vi.getTag(); if (data.length &lt;= 0) { holder.textView.setText("No Data"); } else { dataManipulator = new DataManipulatorClass(this); names2 = dataManipulator.selectAll(); stg1 = new String[names2.size()]; int x = 0; String stg = null; for (String[] name : names2) { stg = "Class Name : " + name[1]; stg1[x] = stg; x++; } holder.textView.setText(stg1[x]); } return vi; } public void onClick(View v) { Log.v("CustomAdapter", "=====Row button clicked====="); } } </code></pre> <p>DataManipulator.Java</p> <pre><code>public class DataManipulatorClass { private static final String DATABASE_NAME = "mydatabaseclass.db"; private static final int DATABASE_VERSION = 1; static final String TABLE_NAME = "newtableclass"; private static Context context; static SQLiteDatabase db; private SQLiteStatement insertStmt; private static final String INSERT = "insert into " + TABLE_NAME + "(classname) values (?)"; public DataManipulatorClass(Context context) { DataManipulatorClass.context = context; OpenHelper openHelper = new OpenHelper(DataManipulatorClass.context); DataManipulatorClass.db = openHelper.getWritableDatabase(); this.insertStmt = DataManipulatorClass.db.compileStatement(INSERT); } public DataManipulatorClass(CustomAdapter customAdapter) { OpenHelper openHelper = new OpenHelper(DataManipulatorClass.context); DataManipulatorClass.db = openHelper.getWritableDatabase(); this.insertStmt = DataManipulatorClass.db.compileStatement(INSERT); } public long insert(String classname) { this.insertStmt.bindString(1, classname); return this.insertStmt.executeInsert(); } public void deleteAll() { db.delete(TABLE_NAME, null, null); } public List&lt;String[]&gt; selectAll() { List&lt;String[]&gt; list = new ArrayList&lt;String[]&gt;(); Cursor cursor = db.query(TABLE_NAME, new String[] { "id", "classname" }, null, null, null, null, "classname asc"); int x = 0; if (cursor.moveToFirst()) { do { String[] b1 = new String[] { cursor.getString(0), cursor.getString(1) }; list.add(b1); x = x + 1; } while (cursor.moveToNext()); } if (cursor != null &amp;&amp; !cursor.isClosed()) { cursor.close(); } cursor.close(); return list; } public void delete(int rowId) { db.delete(TABLE_NAME, null, null); } private static class OpenHelper extends SQLiteOpenHelper { OpenHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY, classname TEXT)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } } } </code></pre> <p>In Classes Class I have listview in which i want to show the data, My logcat is here <a href="http://i.share.pho.to/9739fdca_o.png" rel="nofollow">http://i.share.pho.to/9739fdca_o.png</a></p> <p>It is giving error on mListView.setAdapter( customAdapter ); this line while setting adapter in listview as a customadapter...</p> <p>Thanks in advance...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload