hi guys i insert data from json to sqlite database and its successfully inserted but the problem when i want show the data to listview Using Custom Adapter the error is nullpointerexception .
Custom Adpter :
public class CustomListAdapter extends BaseAdapter implements Filterable{
Context mContext;
private Activity activity;
private LayoutInflater inflater;
private List<Movie> movieItems ;
PlanetFilter ff;
PlanetFilter2 ff2;
private List<Movie> origPlanetList;
int itemPosition;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomListAdapter(Activity activity, List<Movie> movieItems) {
mContext = activity;
this.activity = activity;
this.movieItems = movieItems;
this.origPlanetList = movieItems;
}
public void resetData() {
movieItems = origPlanetList;
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView title;
public TextView rating;
public TextView genre;
public TextView year;
public TextView state;
public Button Button1;
public NetworkImageView thumbNail;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View via ;
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
if (inflater == null)inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)imageLoader = AppController.getInstance().getImageLoader();
holder.thumbNail = (NetworkImageView) convertView.findViewById(R.id.thumbnail);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.rating = (TextView) convertView.findViewById(R.id.rating);
holder.genre = (TextView) convertView.findViewById(R.id.genre);
holder.year = (TextView) convertView.findViewById(R.id.releaseYear);
holder.state = (TextView) convertView.findViewById(R.id.StateText);
holder.Button1= (Button) convertView.findViewById(R.id.AboutBut);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
final ImageView ima = (ImageView) convertView.findViewById(R.id.imagestar);
via = convertView;
// getting movie data for the row
final Movie m = movieItems.get(position);
// thumbnail image
holder.thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// Picasso.with(mContext).load(m.getThumbnailUrl()).into(thumbNail);
// title
holder.title.setText(m.getTitle());
// rating
String Ratee = mContext.getResources().getString(R.string.Ratee);
if(m.gettype() == 1)
{
holder.genre.setVisibility(View.INVISIBLE);
holder.Button1.setVisibility(View.GONE);
ima.setVisibility(View.INVISIBLE);
holder.state.setVisibility(View.INVISIBLE);
holder. year.setText(m.getStat());
String Name = null;
String time;
String str2 = m.gettime();
String delimiter2 = "@";
String[] temp2;
temp2 = str2.split(delimiter2);
for (int j = 0; j < temp2.length; j++) {
String str = temp2[j];
String delimiter = ",";
String[] temp;
temp = str.split(delimiter);
for (int i = 0; i < temp.length; i++) {
Name += Html.fromHtml("<B>- </B>" +"<ul> <li> <B> " + temp[0] + " </B> <br> <small> " + temp[1] +" </small> </li> </ul>") + "\n" ;
}
}
holder.rating.setText(Name.replace("null", ""));
holder.rating.setMaxLines(4);
holder.rating.setEllipsize(TextUtils.TruncateAt.END);
}
else {
holder.rating.setText(Ratee + String.valueOf(m.getRating()));
holder.genre.setText(changLang(m.getGenre()));
holder.state.setText(m.getStat());
// release year
holder.year.setText(String.valueOf(m.getYear()));
}
convertView.setAnimation(AnimationUtils.loadAnimation(mContext
, R.anim.anime_list));
return convertView;
}
database heper :
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "DBsa";
// Contacts table name
private static final String TABLE_Name = "AHtable";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String qury = "CREATE TABLE AHtable (" +
"id INTEGER PRIMARY KEY," +
"title TEXT," +
"imga TEXT," +
"Dec TEXT," +
"Rate TEXT," +
"Year TEXT," +
"State TEXT," +
"time TEXT," +
"type INTEGER," +
"Gen TEXT" +")";
db.execSQL(qury);
}
public void add(String title,String img,String Dec, String Rate, String year, String State,String time, int type , String Gen)
{
SQLiteDatabase db = this.getWritableDatabase();
InsertHelper ih = new InsertHelper(db, TABLE_Name);
final int t = ih.getColumnIndex("title");
final int i = ih.getColumnIndex("imga");
final int d = ih.getColumnIndex("Dec");
final int r = ih.getColumnIndex("Rate");
final int y = ih.getColumnIndex("Year");
final int s = ih.getColumnIndex("State");
final int ti = ih.getColumnIndex("time");
final int ty = ih.getColumnIndex("type");
final int ge = ih.getColumnIndex("Gen");
try {
ih.prepareForInsert();
ih.bind(t, title);
ih.bind(i, img);
ih.bind(d, Dec);
ih.bind(r, Rate);
ih.bind(y, year);
ih.bind(s, State);
ih.bind(ti, time);
ih.bind(ty, type);
ih.bind(ge, Gen);
ih.execute();
}
finally {
ih.close();
}
}
void deleteAll()
{
SQLiteDatabase db= this.getWritableDatabase();
db.delete(TABLE_Name, null, null);
}
// Getting All Contacts
public List<Movie> getAllContacts() {
List<Movie> contactList = new ArrayList<Movie>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_Name;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Movie contact = new Movie();
contact.setTitle(cursor.getString(1));
Log.d("Title: ", cursor.getString(1));
contact.setThumbnailUrl(cursor.getString(2));
Log.d("setThumbnailUrl: ", cursor.getString(2));
contact.seturl(cursor.getString(3));
Log.d("seturl: ", cursor.getString(3));
contact.setRating(cursor.getString(4));
Log.d("setRating: ", cursor.getString(4));
contact.setYear(cursor.getString(5));
Log.d("setYear: ", cursor.getString(5));
contact.setStat(cursor.getString(6));
Log.d("setStat: ", cursor.getString(6));
contact.settime(cursor.getString(7));
Log.d("settime: ", cursor.getString(7));
contact.settype(Integer.parseInt(cursor.getString(8)));
Log.d("settype: ", cursor.getString(8));
Log.d("genre: ", cursor.getString(9));
try{
JSONArray jsonArray = new JSONArray(cursor.getString(9));
ArrayList<String> genre = new ArrayList<String>();
for (int y = 0; y < jsonArray.length(); y++) {
genre.add((String) jsonArray.get(y));
}
contact.setGenre(genre);
}catch(JSONException e){
}
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_Name);
// Create tables again
onCreate(db);
}
using from main activity :
List contacts = db.getAllContacts();
try{
for (Movie cn : contacts) {
movieList.add(cn);
adapter.notifyDataSetChanged();
}
adapter = new CustomListAdapter(MainActivity.this, movieList);
listView.setAdapter(adapter);
}catch (Exception e)
{
e.fillInStackTrace();
}
model class :
public class Movie implements Comparable<Movie> {
private String title, thumbnailUrl;
private String year;
private String urll;
private String State;
private String rating;
private ArrayList<String> genre;
private int type;
private String time;
public Movie() {
}
public Movie(String url,String name) {
this.title = name;
this.urll= url;
}
public Movie(String url,String name,String Stat, String thumbnailUrl, String year, String rating,ArrayList<String> genre, int typ , String tim) {
this.title = name;
this.thumbnailUrl = thumbnailUrl;
this.urll= url;
this.year = year;
this.rating = rating;
this.genre = genre;
this.State = Stat;
this.type= typ;
this.time = tim;
}
public String getTitle() {
return title;
}
public String getStat() {
return State;
}
public String geturl() {
return urll;
}
public String gettime() {
return time;
}
public int gettype() {
return type;
}
public void setStat(String Stata) {
this.State = Stata;
}
public void seturl(String urla) {
this.urll = urla;
}
public void setTitle(String name) {
this.title = name;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public String getYear() {
return year;
}
public void setYear(String string) {
this.year = string;
}
public String getRating() {
return rating;
}
public void setRating(String string) {
this.rating = string;
}
public ArrayList<String> getGenre() {
return genre;
}
public void setGenre(ArrayList<String> genre) {
this.genre = genre;
}
public int compareTo(Movie another) {
return this.title.compareTo(another.title);
}
public int settype(int ty)
{
return this.type = ty;
}
public void settime(String tim) {
this.time = tim;
}
}
Aucun commentaire:
Enregistrer un commentaire