I'm having an issue with calling startActivity() on an Intent from within my custom onClick Listener. The code compiles fine, but when it gets to that point in the execution, I get a nullPointerException.
Code
private static class ProverbAdapter extends ArrayAdapter<String> {
MainActivity ma;
public ProverbAdapter(Context context, int layout, int resId, String[] items) {
super(context, layout, resId, items);
this.ma = new MainActivity();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
Context context = getContext();
if(row == null) {
row = LayoutInflater.from(context).inflate(R.layout.proverb_layout, parent, false);
}
String item = getItem(position);
TextView proverbDate = (TextView)row.findViewById(R.id.proverb_date);
TextView proverbText = (TextView)row.findViewById(R.id.proverb_content);
String[] data = item.split("----");
proverbDate.setText(data[0]);
proverbText.setText(data[1]);
ImageButton emailButton = (ImageButton)row.findViewById(R.id.emailButton);
ImageButton twitterButton = (ImageButton)row.findViewById(R.id.twitterButton);
ImageButton facebookButton = (ImageButton)row.findViewById(R.id.facebookButton);
emailButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "email", context));
twitterButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "twitter", context));
facebookButton.setOnClickListener(this.ma.new ProverbOnClickListener(data[0], data[1], "facebook", context));
return row;
}
}
public class ProverbOnClickListener implements OnClickListener {
String proverb_date, proverb_content, eventType;
Context context;
public ProverbOnClickListener(String proverb_date, String proverb_content, String eventType, Context context) {
this.proverb_date = proverb_date;
this.proverb_content = proverb_content;
this.eventType = eventType;
this.context = context;
}
#Override
public void onClick(View v) {
Toast.makeText(this.context, this.eventType + ": " + this.proverb_content, Toast.LENGTH_SHORT).show();
if(this.eventType == "email") {
Intent i = new Intent(Intent.ACTION_SEND);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
context.startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this.context, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
}
}
}
Exception
FATAL EXCEPTION: main android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
I added the new flag as the compiler suggested, but it still crashes with the same error.
* *EDIT ****
After following advice in the first response, my adapter now looks like:
private static class ProverbAdapter extends ArrayAdapter<String> {
Context context;
public ProverbAdapter(Context context, int layout, int resId, String[] items) {
super(context, layout, resId, items);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row == null) {
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = li.inflate(R.layout.proverb_layout, parent, false);
}
String item = getItem(position);
TextView proverbDate = (TextView)row.findViewById(R.id.proverb_date);
TextView proverbText = (TextView)row.findViewById(R.id.proverb_content);
String[] data = item.split("----");
proverbDate.setText(data[0]);
proverbText.setText(data[1]);
ImageButton emailButton = (ImageButton)row.findViewById(R.id.emailButton);
ImageButton twitterButton = (ImageButton)row.findViewById(R.id.twitterButton);
ImageButton facebookButton = (ImageButton)row.findViewById(R.id.facebookButton);
emailButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "email", context));
twitterButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "twitter", context));
facebookButton.setOnClickListener(new ProverbOnClickListener(data[0], data[1], "facebook", context));
return row;
}
}
However I'm receiving a compilation error.
No enclosing instance type of MainActivity is accessible.
Try Few modifications
this.ma = new MainActivity(); //it is wrong
Don't instantiate Activity
try like this
Context context;
public ProverbAdapter(Context context, int layout, int resId, String[] items) {
super(context, layout, resId, items);
this.context=context;
}
use this variable context where ever you need context
Related
I'm creating a To-do list application and I have a question regarding to using checkboxes and its listeners in List Adapter. My single row in listview contains three TextViews and one Checkbox. I want to change background of single row when user "check" the checkbox. I have read that i should put checkbox listener in my adapter class and so I did it. Now is the problem - when i add few rows to my listview and left the checkbox unchecked for all of them all works fine, but when I add a row, check the checkbox and try to add another one I get error
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference
Below is code of my adapter. Thank you for any advice. I'm just starting with Android programming so thank you for understanding in advance.
public class ToDoAdapter extends ArrayAdapter<ToDoTask> {
ArrayList<ToDoTask> objects;
Context context;
int resource;
public ToDoAdapter(#NonNull Context context, #LayoutRes int resource, #NonNull ArrayList<ToDoTask> objects) {
super(context, resource, objects);
this.objects = objects;
this.context = context;
this.resource = resource;
}
#Override
public View getView(final int position, View convertView, final ViewGroup parent) {
View view = convertView;
ToDoHolder toDoHolder = null;
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.row, parent, false);
toDoHolder = new ToDoHolder();
toDoHolder.rowTitle = (TextView) view.findViewById(R.id.rowTitle);
toDoHolder.rowDesc = (TextView) view.findViewById(R.id.rowDesc);
toDoHolder.rowDate = (TextView) view.findViewById(R.id.rowDate);
toDoHolder.rowIsDone = (CheckBox) view.findViewById(R.id.rowCheckBoxDone);
toDoHolder.rowIsDone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
if(checked){
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#8FE370"));
}
else
parent.getChildAt(position).setBackgroundColor(Color.WHITE);
}
});
view.setTag(toDoHolder);
} else {
toDoHolder = (ToDoHolder) view.getTag();
}
ToDoTask object = objects.get(position);
toDoHolder.rowTitle.setText(object.getTitle());
toDoHolder.rowDesc.setText(object.getDescription());
toDoHolder.rowDate.setText(object.getDate());
toDoHolder.rowIsDone.setChecked(object.getDone());
return view;
}
static class ToDoHolder {
TextView rowTitle;
TextView rowDesc;
TextView rowDate;
CheckBox rowIsDone;
}
}
Below is my MainActivity class which get details of single row element from "AddToDoTask" class.
public class MainActivity extends AppCompatActivity {
private final int requestCode = 1;
ArrayList<ToDoTask> lista = new ArrayList<>();
ToDoAdapter adapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.buttonAdd);
ListView listView = (ListView) findViewById(R.id.listView);
adapter = new ToDoAdapter(this, R.layout.row, lista);
listView.setAdapter(adapter);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), AddToDoTask.class);
startActivityForResult(intent, requestCode);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String title, description, date;
Boolean isDone;
if (requestCode == 1) {
if (null != data) {
title = data.getStringExtra("title");
description = data.getStringExtra("description");
date = data.getStringExtra("date");
isDone = data.getBooleanExtra("done", false);
lista.add(new ToDoTask(title, description, date, isDone));
adapter.notifyDataSetChanged();
}
}
}
}
public class ToDoAdapter extends ArrayAdapter<ToDoTask> {
private ArrayList<ToDoTask> objects;
private Context context;
private int resource;
private SparseBooleanArray checkedPositions = new SparseBooleanArray();
public ToDoAdapter(#NonNull Context context, #LayoutRes int resource, #NonNull ArrayList<ToDoTask> objects) {
super(context, resource, objects);
this.objects = objects;
this.context = context;
this.resource = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ToDoHolder toDoHolder;
if (convertView == null) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.row, parent, false);
toDoHolder = new ToDoHolder();
toDoHolder.rowTitle = (TextView) convertView.findViewById(R.id.rowTitle);
toDoHolder.rowDesc = (TextView) convertView.findViewById(R.id.rowDesc);
toDoHolder.rowDate = (TextView) convertView.findViewById(R.id.rowDate);
toDoHolder.rowIsDone = (CheckBox) convertView.findViewById(R.id.rowCheckBoxDone);
convertView.setTag(toDoHolder);
} else {
toDoHolder = (ToDoHolder) convertView.getTag();
}
toDoHolder.rowTitle.setTag(position);
toDoHolder.rowIsDone.setTag(convertView);
toDoHolder.rowIsDone.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
View view = (View) compoundButton.getTag();
TextView title = (TextView) view.findViewById(R.id.rowTitle);
int pos = (int) title.getTag();
if (checked) {
checkedPositions.put(pos, true);
view.setBackgroundColor(Color.parseColor("#8FE370"));
} else {
checkedPositions.put(pos, false);
view.setBackgroundColor(Color.WHITE);
}
}
});
ToDoTask object = objects.get(position);
toDoHolder.rowTitle.setText(object.getTitle());
toDoHolder.rowDesc.setText(object.getDescription());
toDoHolder.rowDate.setText(object.getDate());
toDoHolder.rowIsDone.setChecked(object.getDone() || checkedPositions.get(position));
return convertView;
}
private class ToDoHolder {
private TextView rowTitle;
private TextView rowDesc;
private TextView rowDate;
private CheckBox rowIsDone;
}
}
You must add a layout in your row xml file and put layout in toDoHolder and just change the layouts background color. You can access child views like
layout.findViewByID(int ID);
ListAdapter.java
public class ListAdapter extends ArrayAdapter<String>
{
private Activity context;
private int[] icon;
private String[] title;
public ListAdapter(Activity context, int[] icon, String[] title)
{
super(context, R.layout.list_item, title);
this.context = context;
this.icon = icon;
this.title = title;
}
public View getView(final int position, View view, final ViewGroup parent)
{
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_item, null, true);
try
{
TextView txtTitle = (TextView) rowView.findViewById(R.id.txtTitle);
ImageView imgIcon = (ImageView) rowView.findViewById(R.id.imgIcon);
txtTitle.setText(title[position]);
imgIcon.setBackgroundResource(icon[position]);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
return rowView;
}
}}
MainActivity.java
In this card Events is replaced with Services. When I re-shuffle the positions the changes are reflected in the app but when I run the app again the Gallery is replaced by Services.
public class MainActivity extends AppCompatActivity
{
String[] title = {
"Services" ,
"Solutions",
"Customers",
"Events",
"Gallery",
"Contact Us"
};
int[] icon = {
R.drawable.services_icon,
R.drawable.solution_icon,
R.drawable.customer_icon,
R.drawable.event_icon,
R.drawable.gallery_icon,
R.drawable.contact_us_icon
};
HomeGrid adapter = new HomeGrid(MainActivity.this, title, icon, back);
grid = (GridView) findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (title[position]) {
case "Services":
Intent i1 = new Intent(MainActivity.this, Listing.class);
startActivity(i1);
break;
case "Solutions":
//Intent i2 = new Intent(MainActivity.this, Listing.class);
//startActivity(i2);
Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show();
break;
case "Customers":
Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show();
break;
case "Gallery":
Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show();
break;
case "Events":
Intent i3 = new Intent(MainActivity.this, Form.class);
startActivity(i3);
break;
case "Contact Us":
Toast.makeText(MainActivity.this, "You Clicked at " + title[position], Toast.LENGTH_SHORT).show();
break;
}
}
});
}
Use this.
public class ListAdapter extends ArrayAdapter<String>{
private Activity context;
private int[] icon;
private String[] title;
private LayoutInflater inflater;
public ListAdapter(Activity context, int[] icon, String[] title)
{
super(context, R.layout.list_item, title);
this.context = context;
this.icon = icon;
this.title = title;
inflater = LayoutInflater.from(context);
}
public View getView(final int position, View view, final ViewGroup parent)
{
view= inflater.inflate(R.layout.list_item, parent, false);
try
{
TextView txtTitle = (TextView) view.findViewById(R.id.txtTitle);
ImageView imgIcon = (ImageView) view.findViewById(R.id.imgIcon);
txtTitle.setText(title[position]);
imgIcon.setBackgroundResource(icon[position]);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
return view;
}
}
}
I apologize in advance if this is a duplicate. I am still new to android development and tried looking for a resolution however could not find one that works.
I am creating a to-do app and getting this error in my adapter.
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String assignment.Model.getAssignment()' on a null object reference
at assignment.Adapter.getView(Adapter.java:39)
and the line of code that it is referencing to is
assignment.setText(modelItems[position].getAssignment());
I believe that the position that I am setting it as is what is causing the error but I'm not sure how to fix it.
Here's part of the rest of my code for reference:
MainActivity.Java - onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String assignmentSentBack = data.getStringExtra("editAssignment");
Integer monthSentBack = data.getIntExtra("month", 0);
Integer daySentBack = data.getIntExtra("day", 0);
modelItems = new Model[100];
ArrayList<Model> modelArrayList = new ArrayList<>(Arrays.asList(modelItems));
modelArrayList.add(new Model(assignmentSentBack, (monthSentBack + 1) + "/" + daySentBack, 0));
lv = (ListView) findViewById(R.id.listAssignment);
ListAdapter adapter = new Adapter(this, modelItems);
lv.setAdapter(adapter);
}
Second Activity - onSendActivity (Button)
public void onSendAssignment (View view) {
EditText editAssignmentET = (EditText)
findViewById(R.id.editAssignment);
String editAssignment = String.valueOf(editAssignmentET.getText());
DatePicker datePickerDP = (DatePicker)
findViewById (R.id.datePicker);
Integer month = Integer.valueOf(datePickerDP.getMonth());
Integer day = Integer.valueOf(datePickerDP.getDayOfMonth());
Intent goingBack = new Intent();
goingBack.putExtra("editAssignment", editAssignment);
goingBack.putExtra ("month", month);
goingBack.putExtra("day", day);
setResult(RESULT_OK, goingBack);
finish();
}
Adapter
public class Adapter extends ArrayAdapter {
Model[] modelItems = null;
Context context;
public Adapter(Context context, Model[] resource) {
super(context, R.layout.row, resource);
this.context = context;
this.modelItems = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row, parent, false);
TextView assignment = (TextView) convertView.findViewById(R.id.assignment);
TextView dueDate = (TextView) convertView.findViewById(R.id.dueDate);
CheckBox cb = (CheckBox) convertView.findViewById(R.id.checkBox);
assignment.setText(modelItems[position].getAssignment());
dueDate.setText(modelItems[position].getDueDate());
if (modelItems[position].getValue() == 1)
cb.setChecked(true);
else
cb.setChecked(false);
return convertView;
}
}
Model
public class Model {
String assignment;
String dueDate;
int value;
Model (String assignment, String dueDate, int value){
this.assignment = assignment;
this.dueDate = dueDate;
this.value = value;
}
public String getAssignment(){
return this.assignment;
}
public String getDueDate(){
return this.dueDate;
}
public int getValue(){
return this.value;
}
}
Any help is appreciated. Thank you.
you should try to wrap it in an inner Holder class and define the parameters in that class
public class Adapter extends ArrayAdapter {
Model[] modelItems = null;
Context context;
public Adapter(Context context, Model[] resource) {
super(context, R.layout.row, resource);
this.context = context;
this.modelItems = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (convertView == null) {
holder = new ViewHolder();
convertView = vi.inflate(R.layout.list_layout, null);
// Find the child views.
holder.assignment= (TextView) convertView.findViewById(R.id.txt_name);
holder.dueDate= (Button) convertView.findViewById(R.id.btn_invite);
holder.cb= (Button) convertView.findViewById(R.id.btn_track);
convertView.setTag(holder);
//....
}
// Reuse existing row view
else {
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
class ViewHolder {
TextView assignment;
TextView dueDate;
CheckBox cb;
}
}
change
public class Adapter extends ArrayAdapter {
to
public class Adapter extends ArrayAdapter<Model> {
You have created model array of size 100 here:
modelItems = new Model[100];
So, 100 models are being created but all 100 indexes have null value.
Then you have created ArrayList using that array:
ArrayList<Model> modelArrayList = new ArrayList<>(Arrays.asList(modelItems));
So again your modelArrayList has 100 null objects. Which BTW you are using no where.
You are passing modelItems into constructor of Adapter. So now since all you items are null, you are getting this exception.
Try to do something like this:
ArrayList<Model> modelArrayList = new ArrayList<>();
modelArrayList.add(new Model(assignmentSentBack, (monthSentBack + 1) + "/" + daySentBack, 0));
Similarly add more model objects like that.
Pass this modelArrayList in your adapter's constructor and use this (instead of array) to display the list.
lv = (ListView) findViewById(R.id.listAssignment);
ListAdapter adapter = new Adapter(this, modelArrayList)
And thus your adapter will be like this:
ArrayList<Model> modelArrayList;
Context context;
public Adapter(Context context, ArrayList<Model> resource) {
super(context, R.layout.row, resource);
this.context = context;
this.modelArrayList = resource;
}
I created singe choice AlertDialog with radio buttons using ArrayAdapter. It's possible to dissmiss that alert dialog when Ratio is selected? I have OnClick listener in ArrayAdapter class holder.name.setOnClickListener, but i have no idea how to to that.
AlertDialog code:
Builder builder = new Builder(serveris, useris, paswordas, BuildBuildingsViewActivity.this, USER_AGENT);
ArrayList<AvailableBuildings> availableBuildings = builder.checkForPossibleBuildings(pastatas.getBuildingLink());
ArrayAdapter<AvailableBuildings> adapter = new AvailableBuildingsAdapter(BuildBuildingsViewActivity.this, R.layout.choice_main, availableBuildings, host, curdid, pastatas.getBuildingLink());
new AlertDialog.Builder(BuildBuildingsViewActivity.this)
.setSingleChoiceItems(adapter, 0, null)
.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
})
.show();
ArrayAdapter code:
public class AvailableBuildingsAdapter extends ArrayAdapter<AvailableBuildings> {
public DBAdapterBuild db_build;
public AvailableBuildingsAdapter(Context context, int textViewResourceId,ArrayList<AvailableBuildings> availableBuildings, String host, String curdid, String aiksteles_link) {
super(context, textViewResourceId, availableBuildings);
this.context = context;
this.availableBuildings = new ArrayList<AvailableBuildings>();
this.availableBuildings.addAll(availableBuildings);
this.host = host;
this.curdid = curdid;
this.aiksteles_link = aiksteles_link;
db_build = new DBAdapterBuild(context, host);
}
private String host;
private Context context;
private String curdid;
private String aiksteles_link;
private ArrayList<AvailableBuildings> availableBuildings;
static class ViewHolder {
RadioButton name;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.choice_row, null);
holder = new ViewHolder();
holder.name = (RadioButton) convertView.findViewById(R.id.building_name);
convertView.setTag( holder );
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
db_build.open();
RadioButton name = (RadioButton) v;
AvailableBuildings building = (AvailableBuildings) name.getTag();
Log.e( "Available: ", ""+building.getAvailable_title()+building.getAvailable_code());
Log.e( "Available: ", ""+db_build.insertTitle(1, building.getAvailable_title(), aiksteles_link, curdid, 0, 1, building.getAvailable_type(), "3", building.getAvailable_code()));
db_build.close();
}
});
} else {
// view already defined, retrieve view holder
holder = (ViewHolder) convertView.getTag();
}
AvailableBuildings building = availableBuildings.get(position);
if ( building == null ) {
}
holder.name.setText(building.getAvailable_title());
holder.name.setTag(building);
return convertView;
}
}
I solve this problem passing to ArrayAdapter constructor Dialog object and next doing like this:
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
db_build.open();
RadioButton name = (RadioButton) v;
AvailableBuildings building = (AvailableBuildings) name.getTag();
Log.e( "Available: ", ""+building.getAvailable_title()+building.getAvailable_code());
Log.e( "Available: ", ""+db_build.insertTitle(1, building.getAvailable_title(), aiksteles_link, curdid, 0, 1, building.getAvailable_type(), "3", building.getAvailable_code()));
db_build.close();
dia.dismiss();
}
});
Working on figuring out my NullPointerException within my custom simplecursoradapter when trying to refresh the data in my view. When using SimpleCursorAdapter you cannot use NotifyDataSetChanged() so I need to create a new adapter and I am having difficulty passing on the data required.
public class DxSimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
Activity activity;
DxDbAdapter dbh;
DxSimpleCursorAdapter adapter;
ListView lv;
protected String subcategory;
public DxSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, String param) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
subcategory = param;
}
public DxSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.list_detail, null);
ViewHolder holder = new ViewHolder();
holder.image = (ImageView) row.findViewById(R.id.fav);
holder.diagnosis = (TextView) row.findViewById(R.id.diagnosis);
holder.code = (TextView) row.findViewById(R.id.code);
row.setTag(holder);
return row;
}
class ViewHolder {
ImageView image;
TextView diagnosis;
TextView code;
}
class Status {
int status;
Long id;
}
#Override
public void bindView(View v, Context con, Cursor cursor) {
ViewHolder holder = (ViewHolder) v.getTag();
int favstatus = cursor.getInt(cursor.getColumnIndex(DxDbAdapter.FAV));
Status state = new Status();
if (favstatus == 1) {
holder.image.setImageResource(R.drawable.btn_star_on_normal);
state.status = 1;
}
else if (favstatus == 0) {
holder.image.setImageResource(R.drawable.btn_star_off_normal);
state.status = 0;
}
long id = cursor.getLong(cursor.getColumnIndex(DxDbAdapter.DIAG_ID));
state.id = id;
holder.image.setTag(state);
holder.diagnosis.setText(cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG)));
holder.code.setText(cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG_CODE)));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Status current_state = (Status) v.getTag();
ImageView fav = (ImageView) v.findViewById(R.id.fav);
fav.setImageResource(R.drawable.ic_fav);
if (current_state.status == 1) {
Toast toast = Toast.makeText(context,"Update fav status to 1",Toast.LENGTH_SHORT);
toast.show();
}
else if (current_state.status == 0) {
Toast toast = Toast.makeText(context,"Update fav status to 0",Toast.LENGTH_SHORT);
toast.show();
}
dbh = new DxDbAdapter(context);
dbh.open();
Cursor newCursor = dbh.fetch(1, subcategory);
String[] columns = new String[] {"diagnosis", "diagcode"};
int[] to = new int[] {R.id.diagnosis, R.id.code};
adapter = new DxSimpleCursorAdapter(context, R.layout.list_detail, newCursor, columns, to);
lv.setAdapter(adapter);
}
});
String diag = cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG));
String code = cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG_CODE));
holder.diagnosis.setText(diag);
holder.code.setText(code);
}
The issue is with subcategory = getIntent().getStringExtra("SUBCATEGORY"); and Cursor newCursor = dbh.fetch(1, subcategory); and that I have to reference the Activity that is using the adapter. I guess I am having issues on the correct syntax to reference the Activity so that I don't get a NullPointerException.
You should really pass in the subcategory from your parent activity since this value will not change while using your adapter (instead of retrieving it every single time you bind to a row)
Ex: from the parent activity you would do something like
String subcategory = getIntent().getStringExtra("SUBCATEGORY");
DxSimpleCursorAdapter adapter = DxSimpleCursorAdapter(context, layout, c, from, to, subcategory);
listView.setAdapter(adapter);
Then obviously you would need to modify the constructor of your adapter so it took that string and then assigned it to your subcategory variable within the adapter