is it possible to create a View which is driven by a SimpleCursorAdapter. The content from this view is ever time a entry from DB.
The View (dataView) looks like:
txtData1
txtData2
txtData3
btnPrev btnNext
I read around and tryd to setup this behavior. Hope its make sens:
public class mActivity extends Activity {
public Context me = this;
public SimpleCursorAdapter mAdapter = null;
public Cursor mCursor = null;
private OnClickListener btnStart_onClick = new OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.dataView);
mCursor = mDB.rawQuery("SELECT * FROM Data", null);
startManagingCursor(mCursor);
mAdapter = new SimpleCursorAdapter(
me,
R.layout.dataView,
mCursor,
new String[] {"Data1", "Data2", "Data3"},
new int[] {R.id.txtData1 , R.id.txtData2, R.id.txtData3});
mAdapter.setViewBinder(VIEW_BINDER);
mCursor.moveToFirst();
}
};
static final ViewBinder VIEW_BINDER = new ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex)
{
switch (view.getId())
{
case R.id.txtData1:
TextView txt = (TextView) view;
if (txt != null)
{
int index = cursor.getColumnIndex("Data1");
txt.setText(cursor.getString(index));
}
return true;
case R.id.txtData2:
TextView txt = (TextView) view;
if (txt != null)
{
int index = cursor.getColumnIndex("Data2");
txt.setText(cursor.getString(index));
}
return true;
case R.id.txtData3:
TextView txt = (TextView) view;
if (txt != null)
{
int index = cursor.getColumnIndex("Data3");
txt.setText(cursor.getString(index));
}
return true;
default:
return false;
}
}
};
}
When I run from the btnStart_onClick I dont get Data in my Textboxes :-(
Can somebody help? Can it work like this?
Next question: how can I use the Prev or Next Buttons? Possible this is the only thing I miss to "load" the first data...
EDIT: I extended my example with the global mCursor and the call to mCursor.moveToFirst()
On my app I also tested with the next / prev buttons and the function mCursor.moveToNext() and mCursor.moveToPrevious()
But its not change :-(
As far as I can tell, there are a lot of what I think are conceptual/organizational/syntactical problems with your code. First of all, an adapter is usually exploited by a view such as ListView or Spinner, that gets populated with the data retrieved by the adapter via the cursor (or whatever data structure is backing it). However, I don't see this pattern in your code, and I'm left wondering what use an adapter would have in your case.
Second, you perform a whole SELECT * query in your click listener, i.e. you retrieve all your 1000 records for each click on... well, on what, exactly? You define the click listener, but never set it onto anything - just as you define the adapter, but you don't bind it to anything. The code that sets up the adapter, with the database query and the binder should really be placed outside the listener.
Last, I believe you mocked variable names a bit before posting the code, because in the following snippet:
TextView txt = (TextView) view;
if (txt != null)
{
int index = cursor.getColumnIndex("Data1");
String txt = cursor.getString(index);
txt.setText(txt);
}
I could hardly see how the compiler is intended to distinguish the two txt variables on the last line of the if body.
Related
I don't know what's wrong with my code, that it always returns null when I use getParseObject().
I'm using parse.com to save my data, and in one table I used one file as a pointer. I have a Game class that has ImgName as a Pointer<Gallery> to a gallery class.
Now I want to retrieve the ImgName value, so this is what I did:
public Adapter(Context context) {
super(context, new ParseQueryAdapter.QueryFactory<ParseObject>() {
public ParseQuery create() {
ParseQuery query = new ParseQuery("Game");
query.include("ImgName");
return query;
}
});
}
// Customize the layout by overriding getItemView
#Override
public View getItemView(final ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_item_landing_cards, null);
}
ParseObject gallery = object.getParseObject("ImgName");
String name=gallery.getString("name");
TextView nameTextView = (TextView) v.findViewById(R.id.text);
nameTextView.setText(name);
But I'm getting null all the time.
Any suggestions?
Use this for the re-use issue:
ParseObject gallery = object.getParseObject("ImgName");
if (gallery != null) {
String name=gallery.getString("name");
TextView nameTextView = (TextView) v.findViewById(R.id.text);
nameTextView.setText(name);
} else {
nameTextView.setText(""); // or any other default value you want to set
}
NOTE:
The cell re-use issue is not on Parse. Cell re-use is a general concept used by the ListView. The cells are recycled for performance by Android. We just have to protect it from re-using old values.
I can't seem to figure out why my list row item won't change color:
/** Populate the Views in act_alliances.xml with data from the database */
private void loadAllianceData() {
TblAlliances mTAlliances = new TblAlliances(this);
mTAlliances.openRead();
Cursor mCursor = mTAlliances.selectSectorData(mSector);
// load Sector Name into act_alliance_detail.xml
TextView mTxtSctName = (TextView) findViewById(R.id.allc_sname);
mTxtSctName.setText("Sector: "+mSector);
// load the "Number of Alliances" field with the count from the cursor
TextView mTxtNumAllcs = (TextView) findViewById(R.id.allc_textView2);
mTxtNumAllcs.setText(String.valueOf(mCursor.getCount()));
String[] cols = new String[] {
mTAlliances.C_FID,
mTAlliances.C_FANAME,
mTAlliances.C_FPLTC,
mTAlliances.C_FSPWER
};
int[] to = new int[] {
R.id.allc_lstRow_textView1,
R.id.allc_lstRow_textView2,
R.id.allc_lstRow_invisible,
R.id.allc_lstRow_textView3
};
// connect to the ListView and clear it just in case this isnt the first time
ListView mListView = (ListView) findViewById(R.id.allc_listView);
mListView.destroyDrawingCache();
mListView.setVisibility(ListView.INVISIBLE);
mListView.setVisibility(ListView.VISIBLE);
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
SimpleCursorAdapter dataAdapter = new SimpleCursorAdapter(
this,
R.layout.act_alliances_list_row,
mCursor,
cols,
to,
0);
dataAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
#Override
public boolean setViewValue(View view, Cursor cursor, int column) {
if( column == 1 ){
TextView tv = (TextView) view;
String mPltc = cursor.getString(cursor.getColumnIndex("FPLTC"));
if (BuildConfig.DEBUG) {
Log.i(Constants.TAG_ACTALLIANCES, "loadAllianceData(): Political Relation: "+mPltc);
}
// Set color of item based on Political Relation
if(mPltc == "Ally"){tv.setTextColor(Color.parseColor("#6699ff"));}
if(mPltc == "Vassal"){tv.setTextColor(Color.parseColor("#00ff00"));}
if(mPltc == "Enemy"){tv.setTextColor(Color.parseColor("#ff0000"));}
return true;
}
return false;
}
});
// Assign adapter to ListView
mListView.setAdapter(dataAdapter);
mListView.setOnItemClickListener( new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// selected item
mAllianceForDetail = ((TextView) arg1.findViewById(R.id.allc_lstRow_textView2)).getText().toString();
startAct("AllianceDetail");
}
});
mTAlliances.close();
}
Everything in the SimpleCursorAdapter.ViewBinder seems to be in order, but the color won't change...i suspect that it may be WHERE i placed the ViewBinder more than the ViewBinder itself.
Any help would be appreciated!
You can't equalize object with a string. You need to use equals or equalsIgnoreCase functions
if (mPltc.equalsIgnoreCase("Ally")){tv.setTextColor(Color.parseColor("#6699ff"));}
if (mPltc.equalsIgnoreCase("Vassal")){tv.setTextColor(Color.parseColor("#00ff00"));}
if (mPltc.equalsIgnoreCase("Enemy")){tv.setTextColor(Color.parseColor("#ff0000"));}
return true;
Try using
if(mPltc.equals("Ally"))...
instead of
if(mPltc == "Ally")
(Same with "Vassal", "Enemy", etc)
You shouldn't use == on String objects, as it doesn't work the way you'd expect.
I have a database with team names and team numbers(both stored as strings - the numbers sometimes have letters in them)
I want it so when you click the "Open" button, it'll bring up a popup that will have all of the teams' names and numbers, and if you click one, it will close the popup and set both editText's to the name & number.
Dialog d = new Dialog(this);
ScoutingFormData info = new ScoutingFormData(this);
info.open();
ScrollView scr = info.getData();
info.close();
d.addContentView(scr, null);
d.show();
That's from the main program, when you click "Open", this happens. ScoutingFormData is my SQLite database, and here's getData:
public ScrollView getData() {
String[] columns=new String[]{KEY_ROWID,KEY_NAME,KEY_NUM};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
ScrollView result=new ScrollView(null);
int iID = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iNum = c.getColumnIndex(KEY_NUM);
LinearLayout vertlay = new LinearLayout(null);
vertlay.setOrientation(LinearLayout.VERTICAL);
for (c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
TextView tv=new TextView(null);
tv.setText(c.getString(iNum)+" "+c.getString(iName));
vertlay.addView(tv);
}
result.addView(vertlay);
return result;
}
So this builds the dialog, but how do I make it so the main program(the top code) will know when one of these TextViews have been clicked?
Side question: when making a View(like LinearLayout vertlay = new LinearLayout(null);), what context am I supposed to be using? I don't fully understand what a 'context' is, so I'm really at a loss of what to replace "null" with.
How do I make it so the main program(the top code) will know when one of these TextViews have been clicked?
Simply make tv clickable and add an OnClickListener.
Side question: what context am I supposed to be using?
You hinted that getData() is in your database adapter class ScoutingFormData, save a reference to the Context passed to the Constructor and reuse it later:
private Context mContext;
public ScoutingFormData(Context context) {
mContext = context;
}
All together:
TextView tv=new TextView(mContext);
tv.setClickable(true);
tv.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
// Do something
}
});
I've had a look around, and it might be because I'm not sure what I'm looking for, but I can't find out how to do something I presume should be quite easy with android.
I have an array of data to display on the screen. This data is a class that holds a database key, name and image.
I'm currently displaying this data as an ImageView and a TextView. I loop through the array and add a new row to a TableLayout containing the image and text.
I'd like both the image and text to be clickable, changing to a new activity.
This new activity needs to know the database key of the row clicked in order to display the correct data.
Here's what I have so far:
private void fillSuggestionTable(TableLayout tabSuggestions, Suggestion[] arrToAdd)
{
for(int i = 0; i < arrToAdd.length; i++)
{
/* Create a new row to be added. */
TableRow trSuggestion = new TableRow(this);
trSuggestion.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
/* Create objects for the row-content. */
ImageView imgDisplayPicture = new ImageView(this);
ImageHandler.loadBitmap(arrToAdd[i].strImageURL, imgDisplayPicture);
imgDisplayPicture.setLayoutParams(new LayoutParams(50,50));
TextView txtArtistName = new TextView(this);
txtArtistName.setText(arrToAdd[i].strName);
txtArtistName.setTextColor(Color.parseColor("#000000"));
/* Add data to row. */
trSuggestion.addView(imgDisplayPicture);
trSuggestion.addView(txtArtistName);
/* Add row to TableLayout. */
tabSuggestions.addView(trSuggestion, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
}
}
Is there a reason you're using a TableView? it seems like what you want to accomplish would be much easier with a ListView & custom CursorAdapter, where the adapter can handle translating from the database to the ListView row. At that point starting a new activity that knows the database ID is trivial:
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(MyActivity.this, MyOtherActivity.class);
i.putExtra("database_id", id);
startActivity(i);
}
});
And in MyOtherActivity:
private int dbId;
protected void onCreate(Bundle savedInstanceState) {
//do stuff
dbId = getIntent().getIntExtra("database_id", -1); // the -1 is the default if the extra can't be found
}
To pass extra data to another Activity, you need to add extra information with the Intent.putExtra(name, value) methods.
For example, to send the Intent:
Intent i = new Intent([pass info about next Activity here]);
i.putExtra("databaseKey", databaseKey);
startActivity(i);
To get the data out again:
public void onCreate(Bundle savedInstance)
{
// Do all initial setup here
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("databaseKey"))
{
int databaseKey = extras.getInt("databaseKey");
// Load database info
}
else
{
// No data was passed, do something else
}
}
EDIT: To find out when the table's row is clicked, you'll need to implement View.OnClickListener and set the onClickListener for the TableRows you use.
For example:
/* Create a new row to be added. */
TableRow trSuggestion = new TableRow(this);
trSuggestion.setOnClickListener([listener]);
The only problem you'll have is relating a View's ID to the related database row ID. A HashMap should help.
This is a pretty simple procedure. This blog explains it in simple terms.
I have a ListView in a ListFragment that is populated via a database query. It loads all the data just fine when it first populates the list in onCreate(). But when I requery the database, assign the return value to assignmentsCursor, and call notifyDataSetChanged(), it doesn't update adapter.mCursor. If I go into the Debugging mode in Eclipse I can see that assignmentsCursor.mCount has changed, but when I look at adapter.mCursor.mCount, it's the same as before. (Yes, I'm checking after notifyDataSetChanged() has been called.)
The relevant parts of my code:
SimpleCursorAdapter adapter;
Cursor assignmentsCursor;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
course = savedInstanceState.getShort(Values.ASSIGNMENT_KEY_COURSE);
}
setRetainInstance(true);
// Create and array to specify the fields we want
String[] from = new String[] { Values.KEY_TITLE, Values.ASSIGNMENT_KEY_COURSE };
// and an array of the fields we want to bind in the view
int[] to = new int[] { R.id.assignment_list_title, R.id.assignment_list_course };
// Need to give assignmentsCursor a value -- null will make it not work
updateAdapter();
adapter = new SimpleCursorAdapter(context, R.layout.assignment_list_item, assignmentsCursor, from, to);
setListAdapter(adapter);
}
public void onResume() {
super.onResume();
updateAdapter();
refresh();
}
/**
* Updates the content in the adapter.
*/
public void updateAdapter() {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean showingCompleted = sharedPrefs.getBoolean(Values.ASSIGNMENT_KEY_SHOWING_COMPLETED, false);
if (course < 0) // Showing assignments from all courses
if (showingCompleted)
assignmentsCursor = DbUtils.fetchAllAssignments(context, Values.ASSIGNMENT_LIST_FETCH, null, true);
else
assignmentsCursor = DbUtils.fetchIncompleteAssignments(context, Values.ASSIGNMENT_LIST_FETCH, null, true);
else // Showing assignments from specified course
if (showingCompleted)
assignmentsCursor = DbUtils.fetchAllAssignments(context, Values.ASSIGNMENT_LIST_FETCH, course, true);
else
assignmentsCursor = DbUtils.fetchIncompleteAssignments(context, Values.ASSIGNMENT_LIST_FETCH, course, true);
}
private void refresh() {
updateAdapter();
adapter.notifyDataSetChanged();
}
Help me!! ;)
P.S. If you need any more details/code, just let me know. I am positive that the database is querying correctly though.
Thanks to Deucalion, I have determined that I incorrectly assumed that the adapter was referencing the assignmentsCursor variable as opposed to creating a copy of the Cursor for itself. What I needed to do was just call adapter.changeCursor(assignmentsCursor) so that it updated its local copy. Thanks again Deucalion!