0

I have a problem when I want to make custom listview in android.

MyListView class :

public class MyListView extends ArrayAdapter<Quotes> {

    Context context;
    int resource ;
    private final List<Quotes> items;
    public MyListView(Context context,int resourse , List<Quotes> items){
        super(context, resourse,items);
        this.resource = resourse;
        this.items = items;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

    try {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }
        Quotes item = items.get(position);
        TextView text = (TextView) v.findViewById(R.id.listItemText);
        TextView id = (TextView) v.findViewById(R.id.listItemId);
        text.setText(item.getText());
        id.setText(item.getID());
    } catch (NullPointerException e) {
        e.printStackTrace();
        Log.e("Error Saeed", e.getMessage());
    }
    return convertView;
}


}

and in MainActivity in onCreate method :

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DatabaseHandler db = new DatabaseHandler(this);
        final ListView lv = (ListView)findViewById(R.id.listView1);
        MyListView adapter = new MyListView(this, R.layout.list_item, db.getAllQuotes());
        lv.setAdapter(adapter);       
    }

DatabaseHander class

public class DatabaseHandler extends SQLiteOpenHelper {

....
....

        public List<Quotes> getAllQuotes(){
        SQLiteDatabase db = getWritableDatabase();
        Cursor c = db.rawQuery("select * from "+TABLE_QUOTE,null);
        List<Quotes> quotes = new ArrayList<Quotes>();
        if(c.moveToFirst()){
            do{
                Quotes q = new Quotes(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
                quotes.add(q);
            }while(c.moveToNext());
        }
        db.close();
        c.close();
        return quotes;
    }

...
...
}

when I run the app its get an error and close.

in this link you can see log.txt

where is the problem?

0

1 Answer 1

1

You probably have a NullPointerException because you don't initialize the ViewHolder in the getView method when convertView is null:

ViewHolder holder = null;
Quotes item = items.get(position);
if(convertView == null){
        convertView = inflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtTitle = (TextView) convertView.findViewById(R.id.listItemText);
        holder.txtID = (TextView)convertView.findViewById(R.id.listItemId);
        convertView.setTag(holder); 
} else {
        holder = (ViewHolder) convertView.getTag();
}

There could be other problems in your code.

Sign up to request clarification or add additional context in comments.

7 Comments

thanks. but when convertView is null i used other way to fill my textviews.TextView textItem = (TextView)convertView.findViewById(R.id.listItemText);textItem.setText(item.getText());
@Mr-Moqadam That will not work. When convertView will not be null you'll get a NullPointerException at the line holder.txtID.setText(item.getID());
hey I'm new to android.I replace your code with my own code but it get error again.can you tell me how to fix this problem?
@Mr-Moqadam To help you I need the stacktrace of the exception you get from the logcat(check the DDMS perspective). Go to the logcat select the red lines with the exception(hold CTRL + mouse click) and then edit your question and post them.
@Mr-Moqadam Add the line this.context = context; in the MyListView constructor.
|

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.