How to extends ArrayAdapter to print Parsed JSON? - java

I have tried and read so many answers but can't able to figure out how to properly code...
NewsAdapter.java
public class NewsAdapter extends ArrayAdapter{
private static final String DEBUG_TAG = "NEWSADAPTER";
private LayoutInflater inflater;
public NewsAdapter(Activity activity, ArrayList<String> items){
super(activity, R.layout.news_feed, items);
Log.d(DEBUG_TAG, "This is came from the world i know of" + items);
inflater = activity.getWindow().getLayoutInflater();
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
return inflater.inflate(R.layout.news_feed, parent, false);
}
}
news_feed.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="#+id/title"
android:text="#string/news_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="19sp" />
</LinearLayout>
And the output of above code is ...
How to get items value and how to setText ?

Do like this:
public class NewsAdapter extends ArrayAdapter{
private static final String DEBUG_TAG = "NEWSADAPTER";
private LayoutInflater inflater;
ArrayList<String> items;
public NewsAdapter(Activity activity, ArrayList<String> items){
inflater = activity.getWindow().getLayoutInflater();
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView
if(v == null)
v = inflater.inflate(R.layout.news_feed, parent, false);
TextView txtTitle = (TextView) v.findViewById(R.id.title);
txtTitle.setText(items.get(position));
return v;
}
}

Related

SearchView show me wrong matches

I have a SearchView and a ListView. My goal here is when I search for (for example) Toyota, I got result in the ListView with Toyota, and than when I click on it, Toyota save to another list.
Everything is working well, except the ListView. When I search for something, I get different result. For example if I search for Toyota, I got BMW or Mazda etc. but not Toyota. BUT, if I click on this result Toyota will be saved and not BMW or Mazda despite I click on them.
I think something with the ListView is not good, because in the background evertyhing is working well, but it's shown otherwise.
Here is my xml file:
<SearchView
android:id="#+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false">
<requestFocus />
</SearchView>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
I am using a custom row in my ListView, I don't think its affect any harm, but now I am even questioning my own existence :D So here it is the list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15sp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/name"
android:layout_centerVertical="true"
android:layout_marginLeft="28sp"
android:textColor="#73c993"/>
</RelativeLayout>
And the ListViewAdapter.java:
public class ListViewAdapter extends ArrayAdapter<String> {
ArrayList<String> list;
Context context;
public ListViewAdapter(Context context, ArrayList<String> items) {
super(context, R.layout.list_row, items);
this.context = context;
list = items;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_row, null);
TextView name = convertView.findViewById(R.id.name);
name.setText(list.get(position));
}
return convertView;
}
}
Finally, my HomeFragment.java:
public class HomeFragment extends Fragment implements SearchView.OnQueryTextListener {
SearchView editsearch;
static ListView listView;
static ListViewAdapter adapter;
static ArrayList<String> cars, selectedCars;
static Context context;
private HomeViewModel homeViewModel;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
return inflater.inflate(R.layout.fragment_home, container, false);
}
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
listView = (ListView) view.findViewById(R.id.list);
cars = new ArrayList<>(Arrays.asList("Toyota", "BMW", "Mazda"));
selectedCars= new ArrayList<>();
context = getContext();
adapter = new ListViewAdapter(context, cars);
listView.setAdapter(adapter);
editsearch = (SearchView) view.findViewById(R.id.search);
editsearch.setOnQueryTextListener(this);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String item = adapter.getItem(position);
if (!(selectedCars.contains(item))) {
addItem(item);
makeToast(item + " added.");
} else {
makeToast(item + " already added.");
}
}
});
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}

How to set a title on BottomSheetDialog with grid?

I have created a BottomSheetDialog with a GridView, but I know that BottomSheetDialog can't set a title using the method .setTitle(), so how can I set a title on the dialog?
Here's my code:
Activity:
customAdapter = new CustomAdapter(this);
GridView gridView = new GridView(this);
gridView.setAdapter(customAdapter);
gridView.setNumColumns(5);
BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(this);
bottomSheetDialog.setContentView(gridView);
bottomSheetDialog.show();
CustomAdapter:
private final LayoutInflater inflater;
private List<String> sizeCodes;
public CustomAdapter(Context context) {
this.sizeCodes = Collections.emptyList();
this.inflater = LayoutInflater.from(context);
}
public CustomAdapter addItems(List<SizeData> items) {
sizeCodes = new ArrayList<>();
for (SizeData item : items)
sizeCodes.add(item.getSize());
notifyDataSetChanged();
return this;
}
#Override
public int getCount() {
return sizeCodes.size();
}
#Override
public String getItem(int position) {
return sizeCodes.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = inflater.inflate(R.layout.item_size_picker, parent, false);
((TextView) convertView.findViewById(R.id.content)).setText(sizeCodes.get(position));
return convertView;
}
item_size_picker:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<TextView
android:id="#+id/content"
style="#style/Base.TextAppearance.AppCompat.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>

How can i create a custom ListView with my xml layout file?

Java code
list view layout #1
How can i insert change the graphics of my ListView adding this layout file to my list view? What should i write in my java code for making each row of my ListView just like my layout file. Thank you all guys !!
First of all you need to create the xml for the custom row you want(custom_row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="8dp"
android:id="#+id/text" /> </LinearLayout>
Then you need to create your custom adapter:
public class CustomAdapter extends BaseAdapter {
Context context;
List<String> textArray;
LayoutInflater inflater;
public RutinaAdapter(Context context, List<String> textarray) {
this.context = context;
inflater = LayoutInflater.from(context);
this.textArray = textarray;
}
#Override
public int getCount() {
return textArray.size();
}
#Override
public Object getItem(int position) {
return textArray.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewGroup vg;
if (convertView != null) {
vg = (ViewGroup) convertView;
} else {
vg = (ViewGroup) inflater.inflate(R.layout.custom_row, null);
}
String text = textArray.get(position);
final TextView text = ((TextView) vg.findViewById(R.id.text));
return vg;
} }
Then you need to add the adapter to the ListView:
list = (ListView) view.findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(getContext(),textArray);
list.setAdapter(adapter1);
Add info to you textArray, and notify the adapter data changed, and thats it.

ListView inside a ListItem displays only one entry

Inside my original Activity, I initialize an Adapter to populate a ListView and that goes smoothly. ArrayList populated with all the data gets passed as an argument to the Adapter and everything goes well.
Now inside that Adapter, or that ListView that gets populated, there's another ListView and another Adapter for it.
Again, I follow the same procedure and populate the Adapter with an ArrayList, but it seems as though only the first entry gets displayed in the nested ListView. I assume this is is because the parent ListView gets populated and the data for the child ListView only populates for one parents entry.
Anyway, here's the adapter initialization and the Adapter class:
PlayerSeasonsStatsAdapter pssAdapter = new PlayerSeasonsStatsAdapter(getContext(), alPlayerSeasonsStats);
vh.lvPlayerSeasonsStats.setAdapter(pssAdapter);
alPlayerSeasonsStats is an ArrayList containing data to populate the child ListView.
Here's the child Adapter:
public class PlayerSeasonsStatsAdapter extends ArrayAdapter{
private ArrayList<PlayerStatsTeamModelSeasons> playerSeasons = new ArrayList<PlayerStatsTeamModelSeasons>();
private LayoutInflater mInflater;
public PlayerSeasonsStatsAdapter(Context context, ArrayList<PlayerStatsTeamModelSeasons> playerSeasons) {
super(context, 0, playerSeasons);
this.playerSeasons = playerSeasons;
mInflater = LayoutInflater.from(context);
}
#Override
public PlayerStatsTeamModelSeasons getItem(int position) {
return playerSeasons.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.player_seasons_stats_row, parent, false);
vh = new ViewHolder();
vh.tvPSSRCompetition = (TextView) convertView.findViewById(R.id.tvPSSRCompetition);
vh.tvPSSRMatchesPlayed = (TextView) convertView.findViewById(R.id.tvPSSRMatchesPlayed);
vh.tvPSSRGoalsScored = (TextView) convertView.findViewById(R.id.tvPSSRGoalsScored);
vh.tvPSSRAssists = (TextView) convertView.findViewById(R.id.tvPSSRAssists);
vh.tvPSSRYellowCards = (TextView) convertView.findViewById(R.id.tvPSSRYellowCards);
vh.tvPSSRRedCards = (TextView) convertView.findViewById(R.id.tvPSSRRedCards);
vh.ivPSSRCompetitionLogo = (ImageView) convertView.findViewById(R.id.ivPSSRCompetitionLogo);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
PlayerStatsTeamModelSeasons pstmsModel = getItem(position);
vh.tvPSSRCompetition.setText(pstmsModel.pstmSeasonModel.name);
vh.tvPSSRMatchesPlayed.setText(pstmsModel.pstmsModel.matchesPlayed);
vh.tvPSSRGoalsScored.setText(pstmsModel.pstmsModel.goalsScored);
vh.tvPSSRAssists.setText(pstmsModel.pstmsModel.assists);
vh.tvPSSRYellowCards.setText(pstmsModel.pstmsModel.yellowCards);
int totalRedCards = Integer.parseInt(pstmsModel.pstmsModel.yellowRedCards) + Integer.parseInt(pstmsModel.pstmsModel.redCards);
vh.tvPSSRRedCards.setText(totalRedCards + "");
//loadLogoToView(vh.ivPSSRCompetitionLogo, pstmsModel.pstmSeasonModel.id, true);
return convertView;
}
public static void loadLogoToView(ImageView iv, String teamId, boolean transform) {
String image = Constant.IMAGES_ROOT +
Constant.LOGO_PATH +
teamId +
Constant.LOGO_EXTENSION;
RequestCreator img = Picasso.with(iv.getContext())
.load(image);
if (transform) img.transform(new RoundedCornersTransform(2));
img.into(iv);
}
static class ViewHolder {
ImageView ivPSSRCompetitionLogo;
TextView tvPSSRCompetition;
TextView tvPSSRMatchesPlayed;
TextView tvPSSRGoalsScored;
TextView tvPSSRAssists;
TextView tvPSSRYellowCards;
TextView tvPSSRRedCards;
}
What are my options here?
Your Adapter should be look like
public class PlayerSeasonsStatsAdapter extends ArrayAdapter<PlayerStatsTeamModelSeasons> {
private class ViewHolder {
TextView tvPSSRCompetition;
}
public PlayerSeasonsStatsAdapter(Context context, List<PlayerStatsTeamModelSeasons> balanceModels) {
super(context, R.layout.row_account_balance, balanceModels);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
PlayerStatsTeamModelSeasons model = getItem(position);
PlayerSeasonsStatsAdapter.ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new PlayerSeasonsStatsAdapter.ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_account_balance, parent, false);
viewHolder.tvPSSRCompetition = (TextView) convertView.findViewById(R.id.tvPSSRCompetition);
convertView.setTag(viewHolder);
} else {
viewHolder = (PlayerSeasonsStatsAdapter.ViewHolder) convertView.getTag();
}
viewHolder.tvPSSRCompetition.setText(model.getPSSRCompetition());
if (position%2== 0) {
convertView.setBackgroundColor(Color.parseColor("#F5EEE5"));
}else{
convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
return convertView;
}
}
Just Call below like
PlayerSeasonsStatsAdapter adapter = new PlayerSeasonsStatsAdapter(MainActivity.this, balanceList);
listView.setAdapter(adapter);
Listview Look like bellow
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
here is row_account_balance layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvPSSRCompetition"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|center_vertical|center_horizontal"
android:text="#string/agent_statement_sl"
android:textStyle="bold"
android:paddingLeft="2dp"
android:textSize="16sp"
android:paddingBottom="5dp"
android:paddingTop="5dp"/>
</LinearLayout>

ListView does not refresh

I'd like to create a ListView and add items dynamically. Here is my code:
CustomAdapter.java:
public class CustomAdapter extends ArrayAdapter<Item>{
Context context;
int layoutResourceId;
LinearLayout linearMain;
ArrayList<Item> data = new ArrayList<Item>();
public CustomAdapter(Context context, int layoutResourceId,
ArrayList<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;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);
Item myImage = data.get(position);
TextView label = new TextView(context);
label.setText(myImage.name);
linearMain.addView(label);
ImageView image = new ImageView(context);
int outImage = myImage.image;
image.setImageResource(outImage);
linearMain.addView(image);
}
return row;
}
}
Item.java:
public class Item {
int image;
String name;
public Item(int image, String name) {
super();
this.image = image;
this.name = name;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
GallerF.java
ArrayList<Item> imageArry = new ArrayList<Item>();
CustomAdapter adapter;
ListView dataList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_gallery, parent, false);
dataList = (ListView) v.findViewById(R.id.lista);
return v;
}
.....
public void CreateNewListRow(){
imageArry.add(new Item(R.drawable.ek_logo,name));
adapter = new CustomAdapter(getActivity(),R.layout.list,imageArry);
dataList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
list.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lineraMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
activity_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="hu.redbuttonebb.endi.fragmentek20.GalleryF"
android:background="#drawable/background"
android:id="#+id/linlayout">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="#+id/lista"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
</ScrollView>
</LinearLayout>
Now when I run the app it's create one row with the correct picture and name. But when I'd like to create one more then nothing happen. Where do I do it wrong?
Replace your file with this:
ArrayList<Item> imageArry = new ArrayList<Item>();
CustomAdapter adapter;
ListView dataList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_gallery, parent, false);
dataList = (ListView) v.findViewById(R.id.lista);
adapter = new CustomAdapter(getActivity(),R.layout.list,imageArry);
dataList.setAdapter(adapter);
return v;
}
.....
public void CreateNewListRow(){
imageArry.add(new Item(R.drawable.ek_logo,name));
imageArry.add(new Item(R.drawable.ek_logo,name));
imageArry.add(new Item(R.drawable.ek_logo,name));
imageArry.add(new Item(R.drawable.ek_logo,name));
imageArry.add(new Item(R.drawable.ek_logo,name));
adapter.notifyDataSetChanged();
}
Edit your list.xml with below code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lineraMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
Remove ScrollView, ListView do not require ScrollView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="hu.redbuttonebb.endi.fragmentek20.GalleryF"
android:background="#drawable/background"
android:id="#+id/linlayout">
<ListView
android:id="#+id/lista"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false" />
</LinearLayout>
Add this method to Adapter & extends Adapter to BaseAdapter
public class FriendsAdapter extends BaseAdapter {
Activity context;
ArrayList<String> data = new ArrayList<>();
LayoutInflater inflater;
public FriendsAdapter(Activity c) {
context = c;
inflater = LayoutInflater.from(this.context);
}
public void add(String item) {
try {
data.add(item);
} catch (Exception e) {
e.printStackTrace();
}
notifyDataSetChanged();
}
public void remove(int position) {
try {
data.remove(data.get(position));
} catch (Exception e) {
e.printStackTrace();
}
notifyDataSetChanged();
}
#Override
public int getCount() {
return data.size();
}
#Override
public String getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_friends, parent, false);
holder = new ViewHolder();
holder.tvUserName = (TextView) convertView.findViewById(R.id.tvUserName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
holder.tvUserName.setText(data.get(position));
} catch (Exception e) {
e.printStackTrace();
}
return convertView;
}
private class ViewHolder {
TextView tvUserName;
}}
Initializing Adapter
FriendsAdaoter adapter = new FriendsAdapter(this);
list.setAdapter(adapter);
adapter.add("ABCDEFGHIJKL");
You are creating a new Adapter every time you want to add an item. You need to create a method in your adapter and call that method when you add an item, like this:
public class CustomAdapter extends ArrayAdapter<Item>{
Context context;
int layoutResourceId;
LinearLayout linearMain;
ArrayList<Item> data = new ArrayList<Item>();
public CustomAdapter(Context context, int layoutResourceId,
ArrayList<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;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
linearMain = (LinearLayout) row.findViewById(R.id.lineraMain);
Item myImage = data.get(position);
TextView label = new TextView(context);
label.setText(myImage.name);
linearMain.addView(label);
ImageView image = new ImageView(context);
int outImage = myImage.image;
image.setImageResource(outImage);
linearMain.addView(image);
}
return row;
}
public void addItem(Item item) {
data.add(item);
notifyDataSetChanged();
}
and change your CreateNewListRow to this:
public void CreateNewListRow(){
adapter.addItem(new Item(R.drawable.ek_logo,name));
}
and change your onCreate to this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.activity_gallery, parent, false);
dataList = (ListView) v.findViewById(R.id.lista);
adapter = new CustomAdapter(getActivity(),R.layout.list,imageArry);
dataList.setAdapter(adapter);
return v;
}

Categories