Im absolutely new in android studio, and Im trying to create custom listview, the problem is that it creating wrong list, as
Name
Name
------
Surname
Surname
-----
... and repeating
what I want to do is:
Name
Surname
-------
My code is:
protected void onResume() {
DBHelper db = new DBHelper(this);
ArrayList<String> names = new ArrayList<String>();
for (int i = 0; i < db.getAllContacts().size(); i++) {
names.add(db.getAllContacts().get(i).getName());
names.add(db.getAllContacts().get(i).getEmail());
}
ArrayAdapter adapter = new custom_row(this, names);
listView_allContacts.setAdapter(adapter);
super.onResume();
}
What is wrong here? Thanks in advance!
My custom_row code:
public class custom_row extends ArrayAdapter {
public custom_row(Context context, ArrayList<String> names) {
super(context, R.layout.custom_cell, names);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater peoInf = LayoutInflater.from(getContext());
View customView = peoInf.inflate(R.layout.custom_cell, parent , false);
String singlePeople = getItem(position);
TextView name_text = (TextView) customView.findViewById(R.id.name_text);
TextView email_text = (TextView) customView.findViewById(R.id.email_text);
name_text.setText(singlePeople);
email_text.setText(singlePeople);
return customView;
}
}
You need two different ArrayList one for the Names and one for the email.
try this
ArrayList<String> list;
public custom_row(Context context, ArrayList<String> names, ArrayList<String> email) {
super(context, R.layout.custom_cell, names);
list = email;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater peoInf = LayoutInflater.from(getContext());
View customView = peoInf.inflate(R.layout.custom_cell, parent , false);
String singlePeople = getItem(position);
String email = list.get(position);
TextView name_text = (TextView) customView.findViewById(R.id.name_text);
TextView email_text = (TextView) customView.findViewById(R.id.email_text);
name_text.setText(singlePeople);
email_text.setText(email);
return customView;
}
Related
I have a custom adapter where i display text and a spinner. I have tried to store the state of each spinner so as to display the same items when the activity is open again i have tried different things but i have not been successfull. Please how do i accomplish this thanks. This is the latest thing i have tried
CustomListAdapter(Context context, ArrayList<String> subjects) {
super(context, R.layout.custom_list_view, subjects);
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.custom_list_view, parent, false);
singleSubject = getItem(position);
TextView singleText = (TextView) customView.findViewById(R.id.listSubjectsMyCourses);
colorLayout = (LinearLayout)customView.findViewById(R.id.colorForSubjects);
relativeLayout = (RelativeLayout)
customView.findViewById(R.id.relativeForView);
parentLayout = (RelativeLayout)
customView.findViewById(R.id.parentLayout);
points = new ArrayList<>();
selected = new ArrayList<>();
selectedsttring = new ArrayList<>();
customView.findViewById(R.id.textViewForGrades);
tinyDB = new TinyDB(getContext());
spinnerForGradePoints = (Spinner)customView.findViewById(R.id.spinnerForGrades);
final ArrayAdapter<String> gradePointAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, UserCourseSelection2.userSubjectGradePoint);
spinnerForGradePoints.setAdapter(gradePointAdapter);
spinnerForGradePoints.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedItempos = adapterView.getSelectedItemPosition();
String getSelectedItem = adapterView.getItemAtPosition(i).toString();
tinyDB.putInt("selected", selectedItempos);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
singleText.setText(singleSubject);
colorLayout.setBackgroundColor(UserCourseSelection2.userSubjectsListColor.get(position));
int getSelected = tinyDB.getInt("selected");
spinnerForGradePoints.setSelection(getSelected);
return customView;
}
SharedPreferences is the way if you dont want to use dependency injection like dagger or some kind of custom local cache on the application level.
For SharedPreferences in spinner check this answer:
https://stackoverflow.com/a/13431729/5577679
I want to add every element of an arrayList to a listView ,Here I am passing an arrayList to the adapter which contains package name and size ,
How to iterate and display all the items in the listView.
public class CCacheAdapter extends ArrayAdapter<CCacheInfo>
{
private ArrayList<CCacheInfo> arrayList;
private Context mContext;
public CCacheAdapter(Context context, ArrayList<CCacheInfo> cacheInfos)
{
super(context,0, cacheInfos);
mContext=context;
arrayList=cacheInfos;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
CCacheInfo cCacheInfo = getItem(position);
ViewHolder oViewHolder;
if (convertView == null)
{
convertView = LayoutInflater.from(getContext()).inflate(R.layout.cache_items, parent, false);
oViewHolder =new ViewHolder();
oViewHolder.mPackageName = (TextView)convertView.findViewById(R.id.package_name);
oViewHolder.mPackageSize = (TextView)convertView.findViewById(R.id.package_size);
oViewHolder.mPackageIcon = (ImageView)convertView.findViewById(R.id.appIcon);
oViewHolder.mCheckbox = (CheckBox)convertView.findViewById(R.id.checkbox);
convertView.setTag(oViewHolder);
}else
{
oViewHolder = (ViewHolder)convertView.getTag();
}
oViewHolder.mPackageName.setText(cCacheInfo.m_szAppName);
oViewHolder.mPackageSize.setText(CCleanTool.formatShortFileSize(mContext, cCacheInfo.m_nSize));
return convertView;
}
private class ViewHolder
{
TextView mPackageName;
TextView mPackageSize;
CheckBox mCheckbox;
ImageView mPackageIcon;
}
}
Just set the adapter to ListView, like the following
ArrayList<CCacheInfo> cacheInfos = ... // you get your data from somewhere
ListView listView = (ListView) findViewById(R.id.list_view);
CCacheAdapter adapter = new CCacheAdapter(this, cacheInfos);
listView.setAdapter(adapter);
i have two problems
1. spinner cannot be selected.
2. getting the spinner item if selected
i tried lots of things but dunno really what's with it if someone would help me i'd be really thankful
here's the code:
`
public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final List values;
public MobileArrayAdapter(Context context, List values) {
super(context, R.layout.req_row, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.req_row, parent, false);
TextView req_date = (TextView) rowView.findViewById(R.id.req_date);
TextView request_time = (TextView) rowView.findViewById(R.id.request_time);
TextView task = (TextView) rowView.findViewById(R.id.tsk);
TextView issue = (TextView) rowView.findViewById(R.id.issue);
TextView employee_name = (TextView) rowView.findViewById(R.id.emp_name);
TextView contactno = (TextView) rowView.findViewById(R.id.contact);
TextView department = (TextView) rowView.findViewById(R.id.dep);
TextView comment = (TextView) rowView.findViewById(R.id.comment);
// final Spinner empSpinner = (Spinner) rowView.findViewById(R.id.spinner);
HashMap<String, String> m = new HashMap<String, String>();
m = (HashMap) values.get(position);
req_date.setText(m.get("req_date"));
request_time.setText(m.get("req_time"));
task.setText("TSK/"+m.get("id"));
issue.setText(m.get("Issue"));
employee_name.setText(m.get("Emp_Name"));
contactno.setText(m.get("Contact_No"));
department.setText(m.get("Department"));
comment.setText(m.get("Comment"));
final Spinner empSpinner = (Spinner) rowView.findViewById(R.id.spinner);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context,
R.layout.support_simple_spinner_dropdown_item, specialistsList);
dataAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
empSpinner.setAdapter(dataAdapter);
empSpinner.setSelection(position);
empSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
sp = empSpinner.getSelectedItem().toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Button send = (Button) rowView.findViewById(R.id.snd_tsk);
send.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new Send().execute();
}
});
return rowView;
}
}
`
I have my listview working to display it on the MainActivity and the layout is set to have 3 TextView properties of a new Person object.
My previous code was working but the scrolling was slow, so i looked into using the Holder pattern for optimization. However, when trying to implement this i am getting NullPointerException on lines TextView personID=(TextView) view.findViewById(R.id.person_field1)
listView is set to a ListView on the MainActivity and i need the person objects to display on this view within the MainActivity.
MainActivity class:
public void onButtonClick()
{
//put the persons objects on the listView within MainActivity
listView.setAdapter(new personAdapter(MainActivity.this, R.layout.list_layout,
personList));
}
class personAdapter extends ArrayAdapter<Person>
{
private LayoutInflater layoutInflator;
public personAdapter(Context context, int resource, List<Person> p) {
super(context, resource, p);
layoutInflator = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder = null;
Person p = getItem(position);
if (view == null) {
layoutInflator.inflate(R.layout.list_layout, null);
//getting null pointer exceptions here
TextView personID = (TextView) view.findViewById(R.id.person_field1);
TextView personFName = (TextView) view.findViewById(R.id.person_field2);
TextView personLName = (TextView) view.findViewById(R.id.person_field3);
holder = new Holder(personID, personFName, personLName);
view.setTag(holder);
}
else {
holder = (Holder) view.getTag();
}
holder.stop.setText(person.getID());
holder.location.setText(person.getFName());
holder.time.setText(person.getLName());
return view;
}
}
static class Holder
{
public TextView id;
public TextView FName;
public TextView LName;
public Holder(TextView id, TextView FName, TextView LName) {
this.id = id;
this.FName = FName;
this.LName = LName;
}
}
because there really is null
pay attention to check view == null, and then view never initialized
try this
view = layoutInflator.inflate(R.layout.list_layout, null);
Just add reference to your view:
view = layoutInflator.inflate(R.layout.list_layout, null);
view is null, you're even checking for it:
if (view == null) { ...
}
What you're missing is assigning the inflated layout to view
view = layoutInflator.inflate(R.layout.list_layout, null);
Can someone tell me how should am i going to create my listview which look similar [here][1].
Problem: How am i going to fulfill the sort of look and feel in my codes which has an icons, file name, and file size yet at the same time looking clean and simple on each file object as shown in the example in the link [here][2]??
Can someone guide on this matter because i'm rather new in android/java... Thanks
Refer to following url for how to implement custom listview
Update
public static class VideoInfo {
public String name = "";
public String size= "";
public String path = "";//add any more variables of which you want info
}
then where you are creating arraylist i.e getVideoFiles() create object of this class
Declare ArrayList<VideoInfo> videoItems;
private void getVideoFiles(File[] videoList)
{
videoItems = new ArrayList<VideoInfo>();
for (int i = 0; i < videolist.length; i++)
{
File mfile = videoList[i];
String path = mfile.getPath();
path = path.substring(path.lastIndexOf("/", path.indexOf("."));
VideoInfo model = new VideoInfo();
model.name = path;
model.size = mfile.length();
videoItems.add(model);
}
setListAdapter(new ListViewAdapter(this, R.layout.row, videoItems));
}
now in your adapter pass this object of arraylist and how you set variables in listview is already given in link above
Problem 2: And why is my listview
having error when the directory is
empty despite having this to handle
the "empty" in my xml
did you remember to name your listview?:
#id/android:list
And if you could for further helper PLEASE clear up problem 1, so its more clear and concise what you want.
UPDATE
The ID of the listview should be: #id/android:list
The ID of the texview should be: #id/android:empty
You haven't create custom adapter to incorporate the layout into codes listview.
Here is something you can use
private class ListViewAdapter extends ArrayAdapter<Object> {
private ArrayList<Object> items;
public ListViewAdapter(Context context, int textViewResourceId,
ArrayList<Object> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Object info = items.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
if (info != null) {
ImageView imView = (ImageView) v.findViewById(R.id.icon);
TextView txtname =(TextView) v.findViewById(R.id.toptext);
TextView txtAddr = (TextView) v.findViewById(R.id.bottomtext);
//set image and set text as you like
}
return v;
}
}
Hope this can help.
First Create the Layout that you want to display for each row in your list then Extend the BaseAdapter class to Customize your Lists. This class contains getView method to replicate the rows in your lists as many times as you want to.
class HighScoreAdapter extends BaseAdapter
{
/* layout inflater to convert your XML layout to View to be rendered in your Each row of Lists.*/
private LayoutInflater minflator = LayoutInflater.from(HighScoreList.this);
ViewHolder holder;
#Override
public Object getItem(int position)
{
return position;
}
#Override
public long getItemId(int position)
{
return position;
}
boolean toRemove = false;
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
/* first time is null */
if (convertView == null )
{
convertView = minflator.inflate(R.layout.highscorelist, null);
holder = new ViewHolder();
holder.rank = (TextView) convertView.findViewById(R.id.user_id);
holder.username = (EditText) convertView.findViewById(R.id.user_nameedit);
holder.time = (TextView) convertView.findViewById(R.id.user_time);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.rank.setText(String.valueOf(position+1)+".");
/* returns the view for next row as layout will be same i.e. this increases the your list's scrolling and working faster even though your list contains thousands of Entry*/
return convertView;
}
/* this class is to make reference to your child views of your layout by using this you can set your child views properties and Listeners acording to your need.*/
class ViewHolder
{
TextView rank;
EditText username;
TextView time;
}
}
I hope this solves your problem completely.. :)
Add this method to your code and if you get any error or Exception please let me know.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Object info = items.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
if (info != null) {
ImageView imView = (ImageView) v.findViewById(R.id.icon);
TextView txtname =(TextView) v.findViewById(R.id.toptext);
TextView txtAddr = (TextView) v.findViewById(R.id.bottomtext);
//set image and set text as you like
imview.setImageBitmap(IMAGE YOU WANT TO SET AAS ICON);
txtname.setText(FILENAME YOU WANT TO SET);
txtadr,setText(FILESIZE YOU WANT TO SET);
// Better you take each info filename,filesize and icon in a arraylist and set them
}
return v;
}