d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Android Studio - Extending To Listview > Fg For Help
123Next
Add Reply New Topic New Poll
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 11 2015 07:00pm
I am trying to get from one screen to another ( I can do so sucessfully but its not the right screen)

Code

AdapterView.OnItemClickListener classListListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


if(id == 1){
Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_SHORT).show();
Intent i = new Intent(QuestionBankListActivity.this, Questions.class);
startActivity(i);
finish();
}
}


this portion of my code gets me to my Questions.class which is what I need, but my issue is here
class Questions extends AppCompatActivity, and I need extends to ListView

Code
public class Questions extends AppCompatActivity {
private QuestionBankDB qbdb = null;
private ListView questionsListView;
private CursorAdapter questionsListViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
qbdb = new QuestionBankDB();
new questionListTask().execute((Object[]) null);
}
}



However if I change extends ListView, even with nothing besides onCreate in the class, when I run the app it crashes when i get to that point of the app (when its supposed to pull up the UI for questions)
but if I keep it at extends AppCompatActivity with just onCreate it goes to that screen fine with no error.

The reason I cannot keep it in extends AppCompatActivity is because my code needs list view
Code
package edu.davenport.cisp340.studentdbprototype;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class Questions extends AppCompatActivity {
private QuestionBankDB qbdb = null;
private ListView questionsListView;
private CursorAdapter questionsListViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
qbdb = new QuestionBankDB();
new questionListTask().execute((Object[]) null);
}
private class questionListTask extends AsyncTask<Object, Object, Cursor>
{

@Override
protected Cursor doInBackground(Object... params)
{
return qbdb.Questions();
}
@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Cursor cursor) {
super.onPostExecute(cursor);
questionsListView = getListView();
questionsListView.setOnItemClickListener(questionsListListener);

// map to on resume
String[] map_from = new String[]{"QUESTION_TEXT"};
int[] map_to = new int[]{R.id.txtQuestions};

questionsListViewAdapter = new SimpleCursorAdapter(Questions.this, R.layout.content_questions, cursor, map_from, map_to);
setListAdapter(questionsListViewAdapter);

}
} // end StudentListTask
AdapterView.OnItemClickListener questionsListListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


}


};

}


getListView; and SetListAdapter are red cannot resolve method..


I also tried doing this
Code
public class Questions extends AppCompatActivity {
private QuestionBankDB qbdb = null;
private ListView questionsListView;
private CursorAdapter questionsListViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
{

questionsListView = (ListView) findViewById(R.id.listView);
CursorAdapter questionlistAdapter = new CursorAdapter();
questionsListView.setAdapter(questionlistAdapter);

}
qbdb = new QuestionBankDB();
new questionListTask().execute((Object[]) null);


but gives me an error on
CursorAdapter questionListAdapter = new CursorAdapter();
saying it is abstract and cannot be instantaited
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 11 2015 07:38pm
just because you want to use a listview doesn't mean you have to extend it in your activity.

as for your CursorAdapter issue, the error message explains it. use a concrete class like SimpleCursorAdapter.

This post was edited by carteblanche on Dec 11 2015 07:38pm
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 12:11am
Quote (carteblanche @ Dec 11 2015 09:38pm)
just because you want to use a listview doesn't mean you have to extend it in your activity.

as for your CursorAdapter issue, the error message explains it. use a concrete class like SimpleCursorAdapter.


Okay ill try that, the only reason im using extends listactivity is becuase I used it to go from my login screen to my class list screen (a screen that dsiplays a list of courses from my database) without a hitch so i figured i had to use the same thing to display my question activity (showing a question from my database)
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 10:49am
Code
package edu.davenport.cisp340.studentdbprototype;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class Questions extends AppCompatActivity {
private QuestionBankDB qbdb = null;
private ListView questionsListView;
private SimpleCursorAdapter questionsListViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
qbdb = new QuestionBankDB();
new questionListTask().execute((Object[]) null);
}
private class questionListTask extends AsyncTask<Object, Object, Cursor>
{

@Override
protected Cursor doInBackground(Object... params)
{
return qbdb.Questions();
}
@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Cursor cursor) {
super.onPostExecute(cursor);
questionsListView = getListView();
questionsListView.setOnItemClickListener(questionsListListener);

// map to on resume
String[] map_from = new String[]{"QUESTION_TEXT"};
int[] map_to = new int[]{R.id.txtQuestions};
questionsListView = (ListView) findViewById(R.id.listView);
questionsListViewAdapter = new SimpleCursorAdapter(Questions.this, R.layout.content_questions, cursor, map_from, map_to);
questionsListView.setAdapter(questionsListViewAdapter);

}
} // end StudentListTask
AdapterView.OnItemClickListener questionsListListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


}


};

}


It doesnt like getListView(); says cannot resolve.
if i take out getListView(); it runs but gives me a null pointer exception
Code
Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase edu.davenport.cisp340.studentdbprototype.DatabaseOpenHelper.getWritableDatabase()' on a null object reference
12-12 11:34:00.782 3963-3991/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at edu.davenport.cisp340.studentdbprototype.AppDB.Open(AppDB.java:24)
12-12 11:34:00.782 3963-3991/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at edu.davenport.cisp340.studentdbprototype.StudentDB.List(StudentDB.java:19)
12-12 11:34:00.782 3963-3991/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at edu.davenport.cisp340.studentdbprototype.LoginActivity$StudentListTask.doInBackground(LoginActivity.java:91)
12-12 11:34:00.782 3963-3991/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at edu.davenport.cisp340.studentdbprototype.LoginActivity$StudentListTask.doInBackground(LoginActivity.java:82)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2015 10:54am
where is getListView() defined? i dont see it defined.

you're assigning it twice. think about it for a moment and explain what the difference is between the two.

Code
questionsListView = getListView();
questionsListView = (ListView) findViewById(R.id.listView);


as for the NPE, it's because of this. second line will be null if you ignore the first.
Code
questionsListView = getListView();
questionsListView.setOnItemClickListener(questionsListListener);


IMO you should be comfortable with basic java se before trying android.

This post was edited by carteblanche on Dec 12 2015 10:55am
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 11:07am
Quote (carteblanche @ Dec 12 2015 12:54pm)
where is getListView() defined? i dont see it defined.

you're assigning it twice. think about it for a moment and explain what the difference is between the two.

Code
questionsListView = getListView();
questionsListView = (ListView) findViewById(R.id.listView);


as for the NPE, it's because of this. second line will be null if you ignore the first.
Code
questionsListView = getListView();
questionsListView.setOnItemClickListener(questionsListListener);


IMO you should be comfortable with basic java se before trying android.


I agree, however my sports counselor put me in it without checking my pre-req's for the class so im kind of at a disadvantage.
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 12:06pm
Nevermind, basically same error.

This post was edited by Trig on Dec 12 2015 12:09pm
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 12:27pm
Code
package edu.davenport.cisp340.studentdbprototype;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DataSetObserver;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class QuestionBankListActivity extends ListActivity {

private QuestionBankDB qbdb = null;

private ListView classListView;
private CursorAdapter classListViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
protected void onResume(){
super.onResume();
qbdb = new QuestionBankDB();
new QuestionBankListTask().execute((Object[]) null);
}

private class QuestionBankListTask extends AsyncTask<Object, Object, Cursor>
{

@Override
protected Cursor doInBackground(Object... params)
{

//String name = String.valueOf(params[STUDENT_NM]);
//int row_limit = (int)params[NUM_ROWS];
return qbdb.List();
}
@Override
protected void onProgressUpdate(Object... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Cursor cursor) {
super.onPostExecute(cursor);
classListView = getListView();
classListView.setOnItemClickListener(classListListener);

// map to on resume
String[] map_from = new String[]{"QUESTION_BANK_NAME"};
int[] map_to = new int[]{R.id.txtSTUDENT_NM};

classListViewAdapter = new SimpleCursorAdapter(QuestionBankListActivity.this, R.layout.content_question_bank_list, cursor, map_from, map_to);
setListAdapter(classListViewAdapter);

}
} // end StudentListTask

AdapterView.OnItemClickListener classListListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


if(id == 1){
Toast.makeText(getApplicationContext(),"1",Toast.LENGTH_SHORT).show();
Intent i = new Intent(QuestionBankListActivity.this, Questions.class);
startActivity(i);
finish();
}
if(id == 2){
Toast.makeText(getApplicationContext(),"2",Toast.LENGTH_SHORT).show();
Intent i = new Intent(QuestionBankListActivity.this, Questions.class);
startActivity(i);
finish();
}
if(id == 3){
Toast.makeText(getApplicationContext(),"3",Toast.LENGTH_SHORT).show();
Intent i = new Intent(QuestionBankListActivity.this, Questions.class);
startActivity(i);
finish();
}
if(id == 4){
Toast.makeText(getApplicationContext(),"No questions to show yet",Toast.LENGTH_SHORT).show();
}
if(id == 5){
Toast.makeText(getApplicationContext(),"No questions to show yet",Toast.LENGTH_SHORT).show();
}

}


};
}


but see in this code I dont understand where getListView(); is defined in this portion of my project and it works fine.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2015 01:04pm
Quote (Trig @ Dec 12 2015 01:27pm)
Code

public class QuestionBankListActivity extends ListActivity {


but see in this code I dont understand where getListView(); is defined in this portion of my project and it works fine.


it's defined in ListActivity. since you inherit from the class, you get access to the method. your other class does not extend from ListActivity, so you do not have it.

http://developer.android.com/reference/android/app/ListActivity.html#getListView()
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 01:08pm
Quote (carteblanche @ Dec 12 2015 03:04pm)
it's defined in ListActivity. since you inherit from the class, you get access to the method. your other class does not extend from ListActivity, so you do not have it.

http://developer.android.com/reference/android/app/ListActivity.html#getListView()


yeah thats what I thought but when i change appcompatactivity to listactivity (even with no code in the freshly made activity) it errors..
thats where im stuck.. i don't know how to get access to that without changing the extends but it gives me an error..
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll