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
Related
I am an intern at this company and I have a task assigned to me by my team leader where I need to make an app that displays a list of items that I can add to and edit/delete the items on the list. I am following the requirements given to me on what the app needs to do.
The problem I'm having is that I need to pass values from an item when clicked on a ListView which uses a custom adapter, and have them sent to a new activity and displayed on the new activity's textviews and imageview. I have tried using putExtras() methods in the list click method and getting the values using getExtras() methods but they didn't work and I've already deleted those codes, so they are no longer there. If you need more of the classes/activities I'm using please let me know. I am using Android Studio 3.1.4
ItemListView.java
public class ItemListView extends AppCompatActivity {
DatabaseHelper myDB;
ArrayList<Item> itemList;
ListView listView;
Item item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list_view);
listView = (ListView) findViewById(R.id.listView);
myDB = new DatabaseHelper(this);
itemList = new ArrayList<>();
Cursor data = myDB.getListContents();
int numRows = data.getCount();
if(numRows == 0){
Toast.makeText(ItemListView.this, "There is nothing in the database.", Toast.LENGTH_LONG).show();
} else {
while(data.moveToNext()){
item = new Item(data.getString(1), data.getString(2), data.getString(3));
itemList.add(item);
}
Row_ListAdapter adapter = new Row_ListAdapter(this, R.layout.list_adapter_view, itemList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);
startActivity(intent);
}
});
}
ViewItemClicked.java
I want the values displayed onto the layout of this activity when a row is clicked.
public class ViewItemClicked extends AppCompatActivity {
ImageView image;
TextView name, desc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_item_clicked);
}
}
Other classes I used:
Row_ListAdapter.java
public class Row_ListAdapter extends ArrayAdapter<Item> {
private LayoutInflater mInflater;
private ArrayList<Item> items;
private int mViewResourceId;
ImageView image;
TextView name;
TextView description;
public Row_ListAdapter(Context context, int textViewResourceId, ArrayList<Item> items){
super(context, textViewResourceId, items);
this.items = items;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parents){
convertView = mInflater.inflate(mViewResourceId, null);
Item item = items.get(position);
if(item != null){
image = (ImageView) convertView.findViewById(R.id.iconIV);
name = (TextView) convertView.findViewById(R.id.nameTV);
description = (TextView) convertView.findViewById(R.id.descTV);
if(image != null){
image.setImageBitmap(item.getImage());
}
if(name != null){
name.setText(item.getName());
}
if(description != null){
description.setText(item.getDescription());
}
}
return convertView;
}
}
Link to GUI: https://i.stack.imgur.com/wHfgv.png
Before you pass the data, implement your Item Class with Serializable like this:
public class Item implements Serializable{
/*your class code here*/
}
Then pass the data in the listView.setOnItemClicklistener like this
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Item passItem = itemList.get(position);
Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);
Bundle itemBundle = new Bundle();
itemBundle.putSerializable("dataItem",passItem)// put the data and define the key
intent.putExtras(itemBundle)
startActivity(intent);
}
});
and to open the data in the ViewItemClicked.Class
public class ViewItemClicked extends AppCompatActivity {
ImageView image;
TextView name, desc;
Item item;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
item = (Item) getIntent().getSerializableExtra("dataItem"); // use the key
setContentView(R.layout.activity_view_item_clicked);
/*now u can use the item data*/
}
}
Try the codes below:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);
String name = itemList.get(position).getString(1);
String description = itemList.get(position).getString(2);
String something_else = itemList.get(position).getString(3);
intent.putExtra("name", name);
intent.putExtra("description", description);
intent.putExtra("something_else", something_else);
startActivity(intent);
}
In your Details Activity:
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String description = intent.getStringExtra("description");
String something_else = intent.getStringExtra("something_else");
Now use the strings to show the values in the desired places.
as
edittext.setText(name);
WHAT FORMAT TYPE OF IMAGE YOUR ARE USING
Is it Base64 or Bitmap if it is Base64 try this code..
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);
String name = list.get(position).getName();
String description = list.get(position).getDescription();
String image= list.get(position).getImage();
startActivity(intent);
}
In Your Second Activity use this code..
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String description = intent.getStringExtra("description");
String image = intent.getStringExtra("image");
Convert here Base64 to Bitmap..
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
Define an interface for listening item click event
public class Row_ListAdapter extends ArrayAdapter<Item> {
private LayoutInflater mInflater;
private ArrayList<Item> items;
private int mViewResourceId;
ImageView image;
TextView name;
TextView description;
OnItemListener mListener;
public Row_ListAdapter(Context context, int textViewResourceId, ArrayList<Item> items){
super(context, textViewResourceId, items);
this.items = items;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
}
public View getView(int position, View convertView, ViewGroup parents){
convertView = mInflater.inflate(mViewResourceId, null);
Item item = items.get(position);
if(item != null){
image = (ImageView) convertView.findViewById(R.id.iconIV);
name = (TextView) convertView.findViewById(R.id.nameTV);
description = (TextView) convertView.findViewById(R.id.descTV);
if(image != null){
image.setImageBitmap(item.getImage());
}
if(name != null){
name.setText(item.getName());
}
if(description != null){
description.setText(item.getDescription());
}
}
convertView.setOnClickListener((v) -> {
if(mListener != null) {
mListener.onItemClick(v, item);
}
})
return convertView;
}
public void setOnItemListener(OnItemListener listener) {
mListener = listener;
}
public interface OnItemClickListener {
void onItemClick(View v, Item item);
}
}
Then, implement OnItemClickListener in the activity and set it to the adapter:
Row_ListAdapter adapter = new Row_ListAdapter(this, R.layout.list_adapter_view, itemList);
adapter.setOnItemListener("your implement");
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'm trying to fetch the data stored using a custom CursorAdapter, but so far it is failing silently. It just loads a blank view and doesn't print anything.
Here is the onCreateView for the main fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.network_listview, container, false);
mListView = (ListView)view.findViewById(R.id.network_listview);
aToken = getSherlockActivity().getIntent().getStringExtra("token");
aTokenSecret = getSherlockActivity().getIntent().getStringExtra("token_secret");
context = getSherlockActivity().getBaseContext();
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(Const.CONSUMER_KEY);
builder.setOAuthConsumerSecret(Const.CONSUMER_SECRET);
builder.setOAuthAccessToken(aToken);
builder.setOAuthAccessTokenSecret((aTokenSecret));
Configuration configuration = builder.build();
mTwitter = new TwitterFactory(configuration).getInstance();
mListAdapter = getListAdapter();
mListView.setAdapter(mListAdapter);
updateList();
return view;
}
getListAdapter():
CursorAdapter getListAdapter() {
CursorAdapter ad = new TweetAdapter(getSherlockActivity(), null);
return ad;
}
TweetAdapter:
public class TweetAdapter extends CursorAdapter
{
private ImageLoader imageLoader;
private static class ViewHolder
{
private ImageView profileView;
private TextView updated;
private ImageView favoriteIcon;
private TextView name;
private TextView message;
private TextView retweeted_by;
private ViewHolder(View row)
{
profileView = (ImageView)row.findViewById(R.id.preview);
updated = (TextView)row.findViewById(R.id.updated);
favoriteIcon = (ImageView)row.findViewById(R.id.favorite_icon);
name = (TextView)row.findViewById(R.id.name);
message = (TextView)row.findViewById(R.id.message);
retweeted_by = (TextView)row.findViewById(R.id.retweeted_by);
}
}
public TweetAdapter(Context context, Cursor c){
super(context, c, true);
}
#Override
public void bindView(View row, Context context, Cursor cursor)
{
// this doesnt print out anything, even though there is data in the database
String tweetText = cursor.getString(cursor.getColumnIndex(Tweets.COL_TEXT_PLAIN));
System.out.println("Tweet Text: " + tweetText);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.item_tweet, parent, false);
ViewHolder viewHolder = new ViewHolder(row);
row.setTag(viewHolder);
return row;
}
}
updateList():
void updateList()
{
mCursor = getCursor();
Cursor oldCursor = mListAdapter.swapCursor(mCursor);
mListAdapter.notifyDataSetChanged();
if (oldCursor != null) {
oldCursor.close();
}
}
You pass a null Cursor when constructing the TweetAdapter instance, so your adapter starts out with no data. Then updateList() replaces the adapter's null Cursor with whatever is in mCursor (which you do not show in the code you provided).
If mCursor is also null, then your code will end up either displaying no content (which is what you are seeing) or throwing an uncaught exception.
So, make sure you actually do a query that fills in the mCursor variable before calling updateList() .
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
I am making a search engine for my Android app that involves a database. My current implementation is as follows:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.result_item, R.id.txtVerbName, verbs);
AutoCompleteTextView txtSearch = (AutoCompleteTextView) findViewById(R.id.txtSearch);
txtSearch.setAdapter(adapter);
However, you will notice I have put "txtVerbName" (which is a TextView) as the textview that gets its value changed with the array attached ("verbs"). How can I make this work so that I can attach a value to txtVerbName and then also attach a different value to another textview in the same layout?
Use a Custom Adapter to accomplish this:
public class AutoCompleteCursorAdapter extends CursorAdapter implements Filterable {
private TextView txtVerbName, txtVerbDefinition;
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
public AutoCompleteCursorAdapter(Context context, Cursor c) {
super(context, c, true);
mInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView txtVerbName = (TextView) view.findViewById(R.id.txtVerbName);
TextView txtVerbDefinition = (TextView) view.findViewById(R.id.txtVerbDefinition);
txtVerbName.setText(cursor.getString(1));
txtVerbDefinition.setText(cursor.getString(2));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = mInflater.inflate(R.layout.result_item, parent, false);
return view;
}
#Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
// this is how you query for suggestions
if (getFilterQueryProvider() != null) {
return getFilterQueryProvider().runQuery(constraint);
}
if (constraint != null) {
DBAdapter db = new DBAdapter(mContext);
db.open();
mCursor = db.getVerbsContaining(constraint);
mCursor.moveToFirst();
db.close();
}
return mCursor;
}
}