d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Android Studio - Extending To Listview > Fg For Help
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2015 01:26pm
Quote (Trig @ Dec 12 2015 02:08pm)
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..


i think you ignored part of my post, so i'm reposting it.

Quote (carteblanche @ Dec 12 2015 11:54am)
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);


Explain.
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 02:13pm
Quote (carteblanche @ Dec 12 2015 03:26pm)
i think you ignored part of my post, so i'm reposting it.



Explain.


questionsListView = getListView();
is getting my list view on my ui and assigning it to questionsListView

questionsListView = (ListView) findViewByID(R.id.listView)
is getting my list view on my ui and assigning it to questionsListView

theyre doing the same thing..

so this is my updated code
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 ListActivity {
private QuestionsDB qbdb = null;
private ListView questionsListView;
private CursorAdapter questionsListViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
qbdb = new QuestionsDB();
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);
questionsListView.setAdapter(questionsListViewAdapter);

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


}


};

}


I extended to list view, took the one findviewbyid out and left the getListView();

and it errored out to this
Code
Process: edu.davenport.cisp340.studentdbprototype, PID: 2301
12-12 15:11:37.511 2301-2301/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: java.lang.IllegalArgumentException: [B]column '_id' does not exist[/B]
12-12 15:11:37.511 2301-2301/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
12-12 15:11:37.511 2301-2301/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at android.widget.CursorAdapter.init(CursorAdapter.java:180)
12-12 15:11:37.511 2301-2301/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at android.widget.CursorAdapter.<init>(CursorAdapter.java:128)
12-12 15:11:37.511 2301-2301/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:55)
12-12 15:11:37.511 2301-2301/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
12-12 15:11:37.511 2301-2301/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at edu.davenport.cisp340.studentdbprototype.Questions$questionListTask.onPostExecute([B]Questions.java:56[/B])
12-12 15:11:37.511 2301-2301/edu.davenport.cisp340.studentdbprototype E/AndroidRuntime: at edu.davenport.cisp340.studentdbprototype.Questions$questionListTask.onPostExecute([B]Questions.java:32[/B])



but I dont know where its trying to get column '_id' from, obviously it has something to do with my cursor?

but here is my QuestionsDB
Code

package edu.davenport.cisp340.studentdbprototype;

import android.database.Cursor;

public class QuestionsDB {

public Cursor Questions() {
Cursor c = null;
AppDB.Open();
c = AppDB.database.query("QUESTION", new String[]{"QUESTION_ID", "QUESTION_TEXT"}, null, null, null, null, "QUESTION_TEXT");
return c;
}

}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2015 02:45pm
http://developer.android.com/guide/topics/providers/content-provider-creating.html#ContentURI

Quote
Handling content URI IDs

By convention, providers offer access to a single row in a table by accepting a content URI with an ID value for the row at the end of the URI. Also by convention, providers match the ID value to the table's _ID column, and perform the requested access against the row that matches.

This convention facilitates a common design pattern for apps accessing a provider. The app does a query against the provider and displays the resulting Cursor in a ListView using a CursorAdapter. The definition of CursorAdapter requires one of the columns in the Cursor to be _ID
The user then picks one of the displayed rows from the UI in order to look at or modify the data. The app gets the corresponding row from the Cursor backing the ListView, gets the _ID value for this row, appends it to the content URI, and sends the access request to the provider. The provider can then do the query or modification against the exact row the user picked.


http://developer.android.com/reference/android/widget/CursorAdapter.html

Quote
Class Overview
Adapter that exposes data from a Cursor to a ListView widget.

The Cursor must include a column named "_id" or this class will not work.


This post was edited by carteblanche on Dec 12 2015 02:47pm
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 03:28pm
all that work to get it working, it worked, and its not what i wanted LOL...
fml.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2015 03:54pm
Rinse and repeat.
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 04:31pm
Quote (carteblanche @ Dec 12 2015 05:54pm)
Rinse and repeat.

yes sir i shall try this again.
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 05:39pm
Do you know where i can find documentation to pull text (questions) from my database and display it on an activity?


So far i have this:

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;
public final static String ID_EXTRA ="edu.davenport.cisp340.studentdbprototype.QUESTION_TEXT";
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)
{
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);
i.putExtra(ID_EXTRA, ID_EXTRA.toString());
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();
}

}


};
}



and this is my new intent

Code
package edu.davenport.cisp340.studentdbprototype;

import android.app.AlertDialog;
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.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

public class Questions extends AppCompatActivity {
String passedVar = null;
private TextView passedView = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_questions);

passedVar = getIntent().getStringExtra(QuestionBankListActivity.ID_EXTRA);
passedView=(TextView)findViewById(R.id.passed);
passedView.setText("ID: " + passedVar);
}


}


pu its passing the literal string, I cant figure out how to have it pull from my database.

I did manage to get it to pull the id from the database tho and put it on screen but thats not what I want. and i used getValueOf on that so it wasnt the same easy transition..
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 12 2015 07:12pm
a brief google search of android sqlite should give you what you need.
Member
Posts: 18,191
Joined: May 31 2010
Gold: 149.27
Dec 12 2015 11:20pm
Quote (carteblanche @ Dec 12 2015 09:12pm)
a brief google search of android sqlite should give you what you need.


I tried youtubing but they only gave me instructions on getting number data types 😧
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll