android error unknown method 'getTitle' - java

Hello i'm testing the following code it's suppose to list all apps installed on an android powered machine.
To edit an compile it i use AIDE
an android java editor the problem is that AIDE always showing me an error: " unknown method 'getTitle'".
Someone can help me please?
public class AppListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<App> mApps;
public AppListAdapter(Context context) {
// cache the LayoutInflater to avoid asking for a new one each time
mInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return mApps.size();
}
#Override
public Object getItem(int position) {
return mApps.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
AppViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
// creates a ViewHolder and stores a reference to the child view
holder = new AppViewHolder();
holder.mTitle = (TextView) convertView.findViewById(R.id.apptitle);
convertView.setTag(holder);
} else {
// reuse/overwrite the view passed assuming(!) that it is castable!
holder = (AppViewHolder) convertView.getTag();
}
holder.setTitle(mApps.get(pos).getTitle());
return convertView;
}
public void setListItems(List<App> list) {
mApps = list;
}
/**
* A view holder which is used to reuse views inside a list.
*/
public class AppViewHolder {
private TextView mTitle;
/**
* Sets the text to be shown as the app's title
*
* #param title the text to be shown inside the list row
*/
public void setTitle(String title) {
mTitle.setText(title);
}
}
}

You probably have no getTitle() method in your App class

mApps.get(pos)
returns you an object of type App
You would have to create a getTitle method in the App class.

Related

Listview show only last item over and over

I am new to android development. I enrolled Udacity course and I get to where I have to use a costume arrayAdapter to display a listView of data (text) I nailed most of it but i have a problem with data displayed which is : when I launch my activity listView displays last item on may arraylist
i have tried almost everything out their nothing worked for me
My Data
/**
* Displays text to the user.
*/
public class Numbers {
//First we Set the Stat of our Class :
// English Translation
public static String englishTranslation;
//Tamazight Translation
public static String tamazightTranslation;
/**
* Create a new Numbers Object :
*
* #param englishTranslation is for ENGLISH NUMBERS
* #param tamazightTranslation is for TAMAZIGHT NUMBERS
*/
//Constructor
public Numbers(String englishTranslation, String tamazightTranslation) {
this.englishTranslation = englishTranslation;
this.tamazightTranslation = tamazightTranslation;
}
//Getter
public static String getEnglishTranslation() {
return englishTranslation;
}
public static String getTamazightTranslation() {
return tamazightTranslation;
}
}
My ArrayList :
public class ActivityNumbers extends AppCompatActivity {
//Add English numbers to The ARRAYList
ArrayList<Numbers> englishNumbers = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
englishNumbers.add(new Numbers("One", "Yan"));
englishNumbers.add(new Numbers("Two", "Sin"));
englishNumbers.add(new Numbers("Three", "Krad"));
englishNumbers.add(new Numbers("Four", "Koz"));
englishNumbers.add(new Numbers("Five", "Smos"));
englishNumbers.add(new Numbers("Six", "Sdis"));
englishNumbers.add(new Numbers("Seven", "Sa"));
englishNumbers.add(new Numbers("Eight", "Tam"));
englishNumbers.add(new Numbers("Nine", "Tza"));
englishNumbers.add(new Numbers("Ten", "Mraw"));
//EnglishNumbers.remove(0);
//EnglishNumbers.size();
// EnglishNumbers.get(1);
//Create a NEW TV object
/**Creating an ArrayAdapter (DATA HOLDER)
#param Context / the ACTIVITY concerned
#param Android.R.layout : an XML layout file contains TextView predefined by Android
#param Items to display */
NumbersAdapter itemsAdapter = new NumbersAdapter (this, englishNumbers);
// Linking the ListView object to a Variable
ListView numbersListView = (ListView) findViewById(R.id.list);
//Calling setAdapter Method on numbersListView with "itemsAdapter
numbersListView.setAdapter(itemsAdapter);
}
}
My Adapter :
public class NumbersAdapter extends ArrayAdapter<Numbers> {
public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers) {
super(context, 0, englishNumbers);
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.listitem, parent, false);
}
Numbers numbers = getItem(position);
System.out.println(position);
TextView tamazight_item = (TextView) listItemView.findViewById(R.id.tamazight_item);
TextView english_item = (TextView) listItemView.findViewById(R.id.english_item);
tamazight_item.setText(numbers.getEnglishTranslation());
System.out.println(numbers.getEnglishTranslation());
english_item.setText(numbers.getTamazightTranslation());
System.out.println(numbers.getTamazightTranslation());
return listItemView;
}
}
try this way
Context context;
ArrayList<Numbers> englishNumbers;
public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers)
{
this.context=context;
this.englishNumbers=englishNumbers;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView= LayoutInflater.from(context).inflate(R.layout.listitem,parent,false);
TextView tamazight_item = (TextView) listItemView.findViewById(R.id.tamazight_item);
TextView english_item = (TextView) listItemView.findViewById(R.id.english_item);
tamazight_item.setText(englishNumbers.get(position).getEnglishTranslation());
english_item.setText(englishNumbers.get(position).getTamazightTranslation());
return convertView;
}
And not forgot to make NumbersAdapter extends BaseAdapter
Your tamazightTranslation and englishTranslation attributes are static on the Numbers class meaning that, those are class attributes. Remove static from the variables and from the getters and you should be fine. See the difference between class attributes and instance attributes here https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
I have changed your model class and Adapter class please follow this It will work
public class Numbers {
//First we Set the Stat of our Class :
// English Translation
private String englishTranslation;
//Tamazight Translation
private String tamazightTranslation;
public String getEnglishTranslation() {
return englishTranslation;
}
public void setEnglishTranslation(String englishTranslation) {
this.englishTranslation = englishTranslation;
}
public String getTamazightTranslation() {
return tamazightTranslation;
}
public void setTamazightTranslation(String tamazightTranslation) {
this.tamazightTranslation = tamazightTranslation;
}
}
Adapter Class
public class NumbersAdapter extends ArrayAdapter<Numbers> {
private Context mContext;
private ArrayList<Numbers>list;
public NumbersAdapter(Context context, ArrayList<Numbers> englishNumbers) {
super(context, 0, englishNumbers);
this.mContext=context;
this.list = englishNumbers;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(mContext).inflate(R.layout.listitem, parent, false);
}
Numbers numbers = list.get(position);
System.out.println(position);
TextView tamazight_item = (TextView) listItemView.findViewById(R.id.tamazight_item);
TextView english_item = (TextView) listItemView.findViewById(R.id.english_item);
tamazight_item.setText(numbers.getEnglishTranslation());
System.out.println(numbers.getEnglishTranslation());
english_item.setText(numbers.getTamazightTranslation());
System.out.println(numbers.getTamazightTranslation());
return listItemView;
}
}
If it is showing one last item in your listview, there might be several reasons for that.
You need to override getCount() in ArrayAdapter to have size of the list. It may fix the problem.
Make sure, the list view is not declared inside scrollview in your xml layout. If it is, handle it properly using nestedscrollview or else.
If it is showing the same data in all rows of your list view, then it might be
In your case, you need to remove static keywords in front of properties, static means that properties or methods belong to Number class, not to the instance of your Number class object. Basically, it must be the reason.

How to handle checkboxes in a listview and send information back from a custom base adapter

I have a list view and each item contains a check box and other various text views. In the Main Activity I have an ArrayList of objects called listOfStuff. From the main activity I'm defining and using a custom base adapter. In the getView method I defined a listener for the check box like so:
holder.cbCompletionStatus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(holder.cbCompletionStatus.isChecked()){
listOfStuff.get(position).setComplete(1);
} else {
listOfStuff.get(position).setComplete(0);
};
}
});
My problem is that I don't know how to access the listOfStuff and the objects within it to modify the information within. The code in the if/else statement hopefully gives an idea what I was trying to do. Just a quick warning, I'm not only new to Android and Java, but to programming field on the whole. Thanks.
UPDATE:
So I ended up figuring this out on my own. I just had to make the listOfStuff Arraylist a static in the Main Activity. Then I could call a static function in the Main Activity to manipulate whatever data in the Array list I needed like so:
MainActivity.checkBoxClicked(result, position);
Here is my class:
class ImageInfoAdapter extends BaseAdapter{
#Override
public int getCount() {
if(viewcount == 0){
return 0;
}
return viewcount;
}
#Override
public Object getItem(int position) {
return isSentAlList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder viewHolder;
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate = LayoutInflater.from(ListPictures.this);
rowView=layoutinflate.inflate(R.layout.listviewayout, parent, false);
viewHolder = new ViewHolder();
viewHolder.textViewisSentFlag = (TextView)rowView.findViewById(R.id.textViewisSentFlag);
viewHolder.imageViewToSent = (ImageView)rowView.findViewById(R.id.imageViewToSent);
viewHolder.checkBoxToSend = (CheckBox)rowView.findViewById(R.id.checkBoxToSend);
rowView.setTag(viewHolder);
} else{
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.ref = position;
Log.i("InfoLog","viewHolder.ref = position; "+viewHolder.ref);
viewHolder.textViewisSentFlag.setText(isSentAlList.get(position));
Bitmap blob = BitmapFactory.decodeByteArray(imageAlList.get(position), 0, imageAlList.get(position).length);
viewHolder.imageViewToSent.setImageBitmap(blob);
viewHolder.checkBoxToSend.setClickable(true);
if(checked.containsKey(""+viewHolder.ref)){ ///if this id is present as key in hashmap
Log.i("InfoLog","checked.containsKey "+viewHolder.ref);
if(checked.get(""+viewHolder.ref).equals("true")){ //also check whether it is true or false to check/uncheck checkbox
Log.i("InfoLog","checked.get(position) "+viewHolder.ref);
viewHolder.checkBoxToSend.setChecked(true);
} else
viewHolder.checkBoxToSend.setChecked(false);
} else
viewHolder.checkBoxToSend.setChecked(false);
viewHolder.checkBoxToSend.setOnCheckedChangeListener(new OncheckchangeListner(viewHolder));
return rowView;
}//End of method getView
}//End of class ImageInfo
class ViewHolder{
private TextView textViewisSentFlag = null;
private ImageView imageViewToSent = null;
private CheckBox checkBoxToSend = null;
int ref;
}//End of class ViewHolder
/////////////////////////
and here is my oncheckchangedlistener
////////////////////////
class OncheckchangeListner implements OnCheckedChangeListener{
ViewHolder viewHolder = null;
public OncheckchangeListner(ViewHolder viHolder) {
viewHolder = viHolder;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(viewHolder.checkBoxToSend.equals(buttonView)) {
if(!isChecked) {
Log.i("InfoLog","checked.get before "+checked.get(""+viewHolder.ref));
checked.put(""+viewHolder.ref,"false");
Log.i("InfoLog","checked.get after "+checked.get(""+viewHolder.ref));
} else
checked.put(""+viewHolder.ref,"true");
} else
Log.i("InfoLog","i m in checkchange ");
}
}

How to change a RealmList to RealmResult?

I am building a Java android app and I am using realm.io for my database. My problem is I have a RealmList and my Custom ListView adapter only accepts RealmResults. Below is the code and more details.
I have an Chat class that has a RealmList, RealmList, userId and a chatId.
public class Chat extends RealmObject{
private RealmList<Friend> participants;
private RealmList<Message> messages;
#PrimaryKey
private String chatId;
private String userId;
...
}
In my activity where I am trying to display all the messages that the chat has, I can call chat.getMessages() to get all the messages for this chat as a RealmList but my ListView adapter below takes a RealmResult because it extends RealmBaseAdapter
public class MessageAdapter extends RealmBaseAdapter<Message> implements ListAdapter {
private String TAG = getClass().getSimpleName();
public MessageAdapter(Context context,
RealmResults<Message> realmResults,
boolean automaticUpdate) {
super(context, realmResults, automaticUpdate);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
{
convertView = inflater.inflate(R.layout.listitem_message, parent, false);
}
Message message = getRealmResults().get(position);
if (message != null)
{
((TextView) convertView.findViewById(R.id.message_content)).setText(message.getContent());
DateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.CANADA);
((TextView) convertView.findViewById(R.id.message_time)).setText(dateFormat.format(message.getTimestamp()));
}
return convertView;
}
public RealmResults<Message> getRealmResults() {
return realmResults;
}
}
Here is where I call it all
RealmList<Message> messages = chat.getMessages();
ListView messageList = (ListView) findViewById(R.id.message_list);
adapter = new MessageAdapter(this, messages, true);
messageList.setAdapter(adapter);
I am open to changing my RealmList to a RealmResult if possible (I have looked and it doesn't seem to be) or If I can use a RealmList in the custom realm adapter that would another solution. Anything to help me move forward would be great help.
thanks
Just do
chat.getMessages().where().findAll()
(Answer from here)
RealmBaseAdapter has a very simple implementation.
So in the case, you can pass the specific Chat to you adapter, and overload below methods:
#Override
public int getCount() {
if (realmResults == null || realmResults.size() == 0) {
return 0;
}
return realmResults.first().getMessages().size();
}
#Override
public T getItem(int i) {
if (realmResults == null || realmResults.size() == 0) {
return null;
}
return realmResults.first().getMessages().get(i);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// You message view here
}

Custom BaseAdapter getview get wrong positions android

I know questions like this one was already asked but I went through all the solutions I saw and non has worked.
This is my adapter
class Adapter extends BaseAdapter {
Activity context;
public Adapter(Activity context) {
this.context = context;
}
#Override
public int getCount() {
return Constants.CHARACTERS_NAMES.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = context.getLayoutInflater().inflate(R.layout.list_charecter_item, null);
holder = new ViewHolder();
holder.PlayerName = (TextView) convertView.findViewById(R.id.name);
holder.PlayerName.setText(Constants.CHARACTERS_NAMES[position]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private class ViewHolder {
TextView PlayerName;
}
}
This is a standard adapter but when I debug it with my listview Instead of getting All my 7 items which are in Constants.CHARACTERS_NAMES I get the 4 first items right, but the I get the first item twice and then the second item.
Please help.

Android java.lang.IllegalStateException on ListView

I use ListActivity with SpecialAdapter that show some log information.
I do not use background thread but I get an error when scroll list:
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class ru.FoxGSM.ui.DebugActivity$SpecialAdapter)]
Please, help me.
(FoxLog is static class that get log information, perhaps from another thread. But in list a want show FoxLog snapshot)
public class DebugActivity extends ListActivity {
private class SpecialAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public SpecialAdapter(Context context) {
super();
// Cache the LayoutInflate to avoid asking for a new one each time.
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return FoxLog.count();
}
public long getItemId(int index) {
return index;
}
public Object getItem(int index) {
return FoxLog.get(FoxLog.count()-index-1);
}
// Get the type of View
public View getView(int position, View convertView, ViewGroup parent) {
TextView text;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.debug_list, null);
text = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(text);
} else
text = (TextView) convertView.getTag();
String s = (String) getItem(position);
if (s==null)
return convertView;
text.setText(s);
boolean isError = false;
if (s!=null && s.length()>0) {
String prefix = s.substring(0, 1);
if (prefix.equals("E") || prefix.equals("W"))
isError = true;
}
if (isError)
text.setBackgroundResource(R.color.red);
else
text.setBackgroundDrawable(null);
return convertView;
}
}
private void RefreshData() {
FoxLog.ReInit(this);
SpecialAdapter adapter = (SpecialAdapter)this.getListAdapter();
adapter.notifyDataSetChanged();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.debug);
FoxLog.ReInit(this);
SpecialAdapter adapter = new SpecialAdapter(this);
setListAdapter(adapter);
Button mExit = (Button)findViewById(R.id.exit);
mExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}

Categories