I am trying to use listview in Fragment but it does not work when I press the selected row.
// Group1ListViewFragment.java
private ListView listview1;
public Group1ListViewFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.group1_list_view_fragment, container, false);
Team[] team_data = new Team[]
{
new Team(R.drawable.ic_launcher, "Arsenal"),
new Team(R.drawable.ic_launcher, "Chelsea"),
new Team(R.drawable.ic_launcher, "Everton"),
new Team(R.drawable.ic_launcher, "Liverpool"),
new Team(R.drawable.ic_launcher, "Manchester United")
};
ListView listview1 = (ListView)rootView.findViewById(R.id.listView1);
// Using the adapter to connect between item view and the photo array
listview1.setAdapter(new TeamAdapter(getActivity(), 0, team_data));
return rootView;
}
Also, i make a adapter to connect listView and array
This is TeamAdapter.java
public TeamAdapter(Context context, int layoutResourceId, Team[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
TeamHolder holder = null;
if (row == null) {
// LayoutInflater inflater = ((Activity) context).getLayoutInflater();
// Ref: http://stackoverflow.com/questions/7803771/call-to-getlayoutinflater-in-places-not-in-activity
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
row = inflater.inflate(layoutResourceId, parent, false);
holder = new TeamHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
row.setTag(holder);
} else {
holder = (TeamHolder) row.getTag();
}
Team team = data[position];
holder.txtTitle.setText(team.title);
holder.imgIcon.setImageResource(team.icon);
return row;
}
static class TeamHolder {
ImageView imgIcon;
TextView txtTitle;
}
you forget pass listview row Xml in adapter class
Replace
listview1.setAdapter(new TeamAdapter(getActivity(), 0, team_data));
with
listview1.setAdapter(new TeamAdapter(getActivity(), R.layout.yourrowxml, team_data));
thats it...
Related
I have a list view app and using a custom adapter, but I can't hide a specific row from the adapter.
My codes
-: In java :-
Adapter
class Adapter extends ArrayAdapter {
int[] imageArray;
String[] titleArray;
String[] descriptionArray;
public Adapter(Context context, String[] titles1, String [] description1, int[] img1) {
super(context, R.layout.settings_list_row, R.id.settingsTitles, titles1);
this.imageArray = img1;
this.titleArray = titles1;
this.descriptionArray = description1;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.settings_list_row,parent,false);
ImageView myImage = (ImageView) row.findViewById(R.id.settingsIcons);
TextView myTitle = (TextView) row.findViewById(R.id.settingsTitles);
TextView myDescription = (TextView) row.findViewById(R.id.settingsDescriptions);
myImage.setImageResource(imageArray[position]);
myTitle.setText(titleArray[position]);
myDescription.setText(descriptionArray[position]);
return row;
}
}
Main activity
String [] titles = {"ti1","ti2","ti3","ti4","ti5"};
String [] descriptions = {"de1","de2","de3","de4","de5"};
int [] images = {R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image,R.drawable.ic_image};
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
lv = (ListView) findViewById(R.id.listView);
final Adapter adapter = new Adapter(getApplicationContext(), titles, descriptions, images);
lv.setAdapter(adapter);
}
-: To remove :-
adapter.remove(position == 0);
But not working!!
In your ListAdapter, you would simply filter out the items you do not want displayed by returning modified values for getCount(), getItem() and getItemId() as appropriate.
For Example
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_list_item, null);
holder.tvName = (TextView) rowView.findViewById(R.id.textViewName);
holder.img = (ImageView) rowView.findViewById(R.id.imageView1);
if (items[position].NeedForCollapsed){
System.out.println("set Visibility.Gone");
holder.tvName.setVisibility(View.GONE);
holder.img.setVisibility(View.GONE);
return rowView;
}
//new values
holder.tvName.setText(items[position].name);
holder.img.setImageResource(GetImageID(items[position]));
return rowView;
}
public class Holder
{
TextView tvName;
ImageView img;
}
i have listView, and there are checkbox, imageView, TextView. I wanna pass that images, that was checked in listView. I know that's doing with bundle, but how to do the checkbox part? I have checkbox, but i don't know how to initialize it.
Here's the adapter part.
public class MyAdapter extends ArrayAdapter<MyAdapter> {
String names[];
int flags[];
Context mContext;
public MyAdapter(Context context, String[] languageNames, int[] countryFlags) {
super(context, R.layout.listview_items);
this.names = languageNames;
this.flags = countryFlags;
this.mContext = context;
}
#Override
public int getCount() {
return names.length;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listview_items, parent, false);
mViewHolder.mFlag = convertView.findViewById(R.id.imageView);
mViewHolder.mLanguage = convertView.findViewById(R.id.textView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mViewHolder.mFlag.setImageResource(flags[position]);
mViewHolder.mLanguage.setText(names[position]);
return convertView;
}
static class ViewHolder {
ImageView mFlag;
TextView mLanguage;
}}
You see there's only an image and textview. How to add checkbox to there?
Here's the listView part.
public class FragmentLanguage extends Fragment {
View mainView;
ListView listView;
Context mContext;
Button next;
MyAdapter adapter;
ArrayList<Integer> mItems = new ArrayList<>();
int[] images = {R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download,
R.drawable.download};
String[] languages = {"Armenian", "Russian", "US English", "Portugal",
"Spanish", "Georgian", "French", "Italian"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.listview, container, false);
listView = mainView.findViewById(R.id.list_view);
listView.setAdapter(adapter);
adapter = new MyAdapter(getActivity(), languages, images);
listView.setAdapter(adapter);
next = mainView.findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return mainView;
}}
1)How to add the checkbox part? 2) How to send images from this fragment to another fragment with bundle? Thanks. I'll write the bundle part here
next.setOnClickListener(new View.OnClickListener()
OPTION 1:
make a list of Int val listOfSelectedImages = arrayListOf()
checkBox.setOnClickListener {
if (checkBox.isChecked) {
//when checked add image(example R.id.image1) to list
} else {
//remove from the list
}
}
from one fragment to another:
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
val mFragment2 = Fragment2()
val bundle = Bundle()
bundle.putParcelableArrayList(KEY, listOfSelectedImages)
mFragment2.arguments = bundle
supportFragmentManager.beginTransaction().add(R.id.fragment_container, mFragment2)
.commit()
}
});
in fragment 2 go val list = getArguments.getParcelableArrayList(KEY)
I have to click a button to show ListView with image. Unfortunately, the application stops when I click on the button. I have also set the custom adapter too but still the same. Here is the code:
public class R1 extends Activity {
String[] signTitle;
String[] signDescription;
int images[]={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable. f,R.drawable.g,R.drawable.h};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.r1);
Resources res = getResources();
signTitle = res.getStringArray(R.array.titile);
signDescription = res.getStringArray(R.array.description);
ListView lv = (ListView) findViewById(R.id.listView);
Customlistadapter adapter = new Customlistadapter(this,signTitle,images,signDescription);
lv.setAdapter(adapter);}
customAdpater.java
public class Customlistadapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
public Customlistadapter(Context c, String[] title, int imgs[],String[] desc) {
super(c, R.layout.activity_show,R.id.textView1,title);
this.context = c;
this.images = imgs;
this.titleArray = title;
this.descriptionArray = desc;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.activity_show, parent,false);
ImageView myImage = (ImageView) row.findViewById(R.id.image);
TextView myTitle = (TextView) row.findViewById(R.id.textView1);
TextView myDescription = (TextView) row.findViewById(R.id.textView2);
myImage.setImageResource(images[position]);
myTitle.setText(titleArray[position]);
myDescription.setText(descriptionArray[position]);
return row;
}
}
If you could post the LogCat or at least point out the line which produces the error, we could help you better.
Also Initializing a ListView like this is highly inefficient.
Implementing a Viewholder Patter helps.
Optimized CustomListAdapter
public class CustomListAdapter extends ArrayAdapter<String>
{
Context context;
int[] images;
String[] titleArray;
String[] descriptionArray;
public CustomListAdapter(Context c, String[] title, int imgs[],String[] desc) {
super(c, R.layout.activity_show,R.id.textView1,title);
this.context = c;
this.images = imgs;
this.titleArray = title;
this.descriptionArray = desc;
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder viewHolder;
if(convertView == null)
{
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_show, parent,false);
viewHolder.title = (TextView)convertView.findViewById(R.id.textView1);
viewHolder.description = (TextView)convertView.findViewById(R.id.textView2);
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
if(titleArray[position] != null)
{
viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
viewHolder.title= (TextView) convertView.findViewById(R.id.textView1);
viewHolder.description = (TextView) convertView.findViewById(R.id.textView2);
viewHolder.image.setImageResource(images[position]);
viewHolder.title.setText(titleArray[position]);
viewHolder.description.setText(descriptionArray[position]);
}
}
return convertView;
}
private class ViewHolder
{
TextView title,description;
ImageView image;
}
}
I've been attempting to upgrade an old app I made in college, including adding a listview to show data from Last.FM. The idea is having a main text with subtext under it, but the adapter I put together just isn't working. I've tried a good number of options, and the most recent is giving me a "Attempt to invoke virtual method on a null object reference" error, a previous try got a "Cannot cast Activity to Application" error.
Relevant code sections:
Main Activity adapter section:
ListView lv;
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.summarylayout);
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT);
lv = (ListView) dialog.findViewById(R.id.itemList); //Listview name
artistAdapter = new itemAdapter(getApplicationContext(), //Changed from ArrayAdapter<Item>
R.layout.custom_listview, //R.layout.custom_listview if itemAdapter works
result.toArray(new Item[] {}));
lv.setAdapter(artistAdapter);
lv.setOnItemClickListener(new MyOIListener());
Button closeButton = (Button) dialog
.findViewById(R.id.closeBtn);
// if button is clicked, close the custom dialog
closeButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tracks = false;
dialog.dismiss();
}
});
The custom Adapter itself (Commented sections are previous attempts):
public class itemAdapter extends ArrayAdapter<Item>{
Context context;
int layoutResourceId;
Item data[] = null;
public itemAdapter(Context context, int layoutResourceId, Item[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
// ItemHolder holder = null;
Item rowIn = new Item();
TextView main;
TextView sub;
if(row == null)
{
//LayoutInflater inflater = ((Application)context).getLayoutInflater();
//row = inflater.inflate(layoutResourceId, parent, false);
//row = LayoutInflater.from(getActivity()).inflate(R.layout.custom_listview, parent, false);
LayoutInflater inflater = LayoutInflater.from(this.getContext());
row = inflater.inflate(layoutResourceId, parent, false);
//holder = new ItemHolder();
//rowIn = new Item();
}
else
{
rowIn = (Item) row.getTag();
}
main = (TextView)row.findViewById(R.id.mainText);
sub = (TextView)row.findViewById(R.id.subText);
row.setTag(rowIn);
Item temp = data[position];
main.setText(temp.getTitle());
sub.setText(temp.getExtra());
return row;
}
/* static class ItemHolder
{
TextView mainText;
TextView subText;
}*/
}
The "Item" class is simply a holder for text/URLs parsed from the Last.FM api request.
I realize this is a question asked often, but 3 days of attempts and searching hasn't helped me out much. Any help would be appreciated
You're passnig the applicationContext to your adapter. Pass your Activity by:
artistAdapter = new itemAdapter(MainActivity.this, //Changed from ArrayAdapter<Item>
R.layout.custom_listview, //R.layout.custom_listview if itemAdapter works
result.toArray(new Item[] {}));
Your getView method was wrong, actually you will face a null variable there when getting the data from the tag.
This one should work for you, it is using ViewHolder Pattern
public class itemAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
Item data[] = null;
public itemAdapter(Context context, int layoutResourceId, Item[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemHolder holder = null;
if (convertView == null) {
//LayoutInflater inflater = ((Application)context).getLayoutInflater();
//row = inflater.inflate(layoutResourceId, parent, false);
//row = LayoutInflater.from(getActivity()).inflate(R.layout.custom_listview, parent, false);
LayoutInflater inflater = ((Activity) this.context).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder = new ItemHolder();
holder.mainText = row.findViewById(R.id.mainText);
holder.subText = row.findViewById(R.id.subText);
convertView.setTag(holder);
} else {
holder = (ItemHolder) convertView.getTag();
}
Item temp = data[position];
if(temp != null) {
holder.mainText.setText(temp.getTitle());
holder.subText.setText(temp.getExtra());
}
return convertView;
}
static class ItemHolder {
TextView mainText;
TextView subText;
}
}
And as #vinitius said, pass your Activity context to the adapter.
For my android application i'm using the ViewHolder pattern.
Whenever i click on an imageview in my listview the row get updated but other rows are also getting updated.
Whenever i click the "like" button other images also receive the image that the are liked by the user. How is this possible?
Here is my viewholder code:
public class MyAdapter extends ArrayAdapter<MyModel> {
Context context;
int layoutResourceId;
List<MyModel> data = null;
public MyAdapter(Context context, int layoutResourceId,
List<MyModel> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final BlockHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new BlockHolder();
holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
holder.totalVotes = (TextView) row.findViewById(R.id.totalVotes);
holder.likeIcon = (ImageView) row.findViewById(R.id.likeIcon);
holder.dislikeIcon = (ImageView) row.findViewById(R.id.dislikeIcon);
holder.saveIcon = (ImageView) row.findViewById(R.id.saveIcon);
row.setTag(holder);
} else {
holder = (BlockHolder) row.getTag();
}
MyModel myBlock = data.get(position);
ImageLoader.getInstance().displayImage(myBlock.getImage(),
holder.imgIcon);
final int totalLikes = myBlock.getLikes() - myBlock.getDislikes();
holder.totalVotes.setText(totalLikes + " likes");
final ImageView like = holder.likeIcon;
holder.likeIcon.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ALSO UPDATES OTHER VIEWS...
holder.likeIcon.setBackgroundResource(R.drawable.thumbs_up_mouse);
}
});
return row;
}
private static class BlockHolder{
ImageView imgIcon;
TextView totalVotes;
ImageView dislikeIcon;
ImageView likeIcon;
ImageView saveIcon;
}