In ListviewAdapter I dont know why getview() function not call - java

i want know why getview() function is not call
maybe override is wrong?
i want make custom listlayout
what's wrong in listviewadapter?
public class ListViewAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Word> items;
public ListViewAdapter(Context mContext, ArrayList<Word> items) {
this.mContext = mContext;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wordlist_layout, viewGroup, false);
}
TextView wordview1 = view.findViewById(R.id.wordView1);
System.out.println("wordlist layout = " + items.get(i).w1);
wordview1.setText(items.get(i).w1);
return view;
}
// this is my Main activity code
private ArrayList<Word> items;
private ListViewAdapter listviewadapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.submit_Button);
btn.setOnClickListener(view -> {
ListView word_listview = findViewById(R.id.Wordle_Word_List);
items = new ArrayList<Word>();
items.add(new Word('a'));
listviewadapter = new ListViewAdapter(getApplicationContext(), items);
word_listview.setAdapter(listviewadapter);
});
}
i dont know what is missing in my code
In ListviewAdapter I dont know why getview() function not call

I think your problem is the condition in its body, not the whole function. There is no need to check if it is null or not. you can simply inflate the view as mentioned in this article:
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.wordlist_layout, viewGroup, false);
TextView wordview1 = view.findViewById(R.id.wordView1);
System.out.println("wordlist layout = " + items.get(i).w1);
wordview1.setText(items.get(i).w1);
return view;
}

#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(mContext).inflate(R.layout.wordlist_layout, viewGroup, false);
TextView wordview1 = view.findViewById(R.id.wordView1);
wordview1.setText(items.get(i).w1);
return view;
}

Related

Get name of the rowItem in listView onClick

I am trying to implement a simple listView with onClick functionality. On clicking the item in the listView, I want to show the name of the item in a 'toast message, but I am unable to do that. I have used the method suggested in this link and link but couldn't get the desired result. I have added my code below :
MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItem = new AppList();
final ListView userInstalledApps = (ListView)findViewById(R.id.appListView);
List<AppList> installedApps = getInstalledApps();
CustomAppAdapter installedAppAdapter = new CustomAppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView appName = (TextView)adapterView.findViewById(R.id.list_app_name);
Toast.makeText(getApplicationContext(),
"Clicked "+ adapterView.getItemAtPosition(i),Toast.LENGTH_LONG).show();
}
});
}
CustomAppAdapter.java
public class CustomAppAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
private List<AppList> listStorage;
Context context;
public CustomAppAdapter(Context context, List<AppList> listStorage) {
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listStorage = listStorage;
}
#Override
public int getCount() {
return listStorage.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder listViewHolder;
if(convertView == null){
listViewHolder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.installed_app_list, parent, false);
listViewHolder.textInListView = (TextView)convertView.findViewById(R.id.list_app_name);
listViewHolder.imageInListView = (ImageView)convertView.findViewById(R.id.app_icon);
convertView.setTag(listViewHolder);
}else{
listViewHolder = (ViewHolder)convertView.getTag();
}
listViewHolder.textInListView.setText(listStorage.get(position).getName());
listViewHolder.imageInListView.setImageDrawable(listStorage.get(position).getIcon());
return convertView;
}
static class ViewHolder{
TextView textInListView;
ImageView imageInListView;
}
}
Below is the screenshot of the listView :
On trying this method:
Toast.makeText(getApplicationContext(),
"Clicked "+ adapterView.getItemAtPosition(i),Toast.LENGTH_LONG).show();
Suppose I clicked on the 2nd item in the listView so I get a toast message as Clicked 2. But I want that it shows me Clicked PhonePe
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rowItem = new AppList();
final ListView userInstalledApps = (ListView)findViewById(R.id.appListView);
List<AppList> installedApps = getInstalledApps();
CustomAppAdapter installedAppAdapter = new CustomAppAdapter(MainActivity.this, installedApps);
userInstalledApps.setAdapter(installedAppAdapter);
userInstalledApps.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView appName = (TextView)adapterView.findViewById(R.id.list_app_name);
Toast.makeText(getApplicationContext(),
"Clicked "+ installedApps.get(i).getName(),Toast.LENGTH_LONG).show();
}
});
}
Try this.

how to access the elements of another layout?

I created listView. enter image description here
And my adapter, which has a button, textView.enter image description here
I want to when i click on an button was removed element from the list.
How in the class MainActiviti access to the elements of another layout?
public class AdapterItem extends BaseAdapter {
Context context;
LayoutInflater lInflater;
ArrayList<ItemInList> items;
public AdapterItem(Context context, ArrayList<ItemInList> items){
this.context = context;
this.items = items;
lInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return items.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
ItemInList itemInList = getItemList(position);
((TextView) view.findViewById(R.id.id_item)).setText(itemInList.id);
((TextView) view.findViewById(R.id.name)).setText(itemInList.name);
((ImageButton) view.findViewById(R.id.imageButton)).setImageResource(itemInList.imageButton);
return view;
}
ItemInList getItemList(int position) {
return ((ItemInList) getItem(position));
}
}
ItemInList.java
public class ItemInList {
String name;
String id;
int imageButton;
boolean deleteChek;
public ItemInList(String name, String id, int imageButton, boolean deleteChek){
this.name = name;
this.id = id;
this.imageButton = imageButton;
this.deleteChek = deleteChek;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<ItemInList> arrayItemInLists = new ArrayList<ItemInList>();
AdapterItem adapterItem;
ListView listViewl;
Button delete;
Button checkAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fillData();
listViewl = (ListView) findViewById(R.id.listView);
delete = (Button) findViewById(R.id.delete_select);
checkAll = (Button) findViewById(R.id.select_all);
adapterItem = new AdapterItem(this, arrayItemInLists);
listViewl.setAdapter(adapterItem);
}
public void fillData() {
for (int i = 1; i <= 20; i++) {
arrayItemInLists.add(new ItemInList("String" + i," " + i, R.drawable.delete,false));
}
}
You need just remove an item from your DATA (in your case - arrayItemInLists) and call notifyDataSetChanged(). In fact you can do that inside an Adapter.
private OnClickListener listener= new OnClickListener() {
#Override
public void onClick(View v) {
int position = ((Integer) v.getTag());
}
};
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
ItemInList itemInList = getItemList(position);
((TextView) view.findViewById(R.id.id_item)).setText(itemInList.id);
((TextView) view.findViewById(R.id.name)).setText(itemInList.name);
((ImageButton) view.findViewById(R.id.imageButton)).setImageResource(itemInList.imageButton);
((ImageButton) view.findViewById(R.id.imageButton)).setTag(position);
((ImageButton) view.findViewById(R.id.imageButton)).setOnClickListener(listener);
return view;
}

How to implement a ListView in a Fragment in Android

I have Fragment.java and its XML. In XML I put ListView and I want to update the ListView in Fragment and add some String.
I am trying to make a custom adapter. When I run the ListView in Activity with custom adapter all works great, but when I put the same code in Fragment nothing happens. I am using EventBus3.
This is my Activity send Event
EventBus.getDefault().post(new SendDataHelper(nameOfItem[myItemInt]));
This is my Fragment:
public class MyOrderFragment extends Fragment {
TextView name;
ListView listOrder;
private Context context;
List<cources> collegeList;
public MyOrderFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
}
#Subscribe
public void onMessageEvent(SendDataHelper event){
Toast.makeText(getActivity(), event.getText(), Toast.LENGTH_SHORT).show();
String text = event.getText();
name.setText(text);
cources college;
collegeList = new ArrayList<cources>();
college = new cources();
college.name = "sharon";
collegeList.add(college);
listOrder.setVisibility(View.VISIBLE);
ListAdapter adapter = new ListAdapter(collegeList, context);
listOrder.setAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_order_fragment, container, false);
name = (TextView) rootView.findViewById(R.id.textView);
listOrder = (ListView) rootView.findViewById(R.id.listOrder);
return rootView;
}
}
This is my adapter
public class ListAdapter extends BaseAdapter
{
Context context;
List<cources> valueList;
public ListAdapter(List<cources> listValue, Context context)
{
this.context = context;
this.valueList = listValue;
}
#Override
public int getCount()
{
return this.valueList.size();
}
#Override
public Object getItem(int position)
{
return this.valueList.get(position);
}
#Override
public long getItemId(int position)
{
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewItem viewItem = null;
if(convertView == null)
{
viewItem = new ViewItem();
LayoutInflater layoutInfiater = (LayoutInflater)this.context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
//LayoutInflater layoutInfiater = LayoutInflater.from(context);
convertView = layoutInfiater.inflate(R.layout.list_adapter_view, null);
viewItem.txtNamePlanche = (TextView)convertView.findViewById(R.id.Tx_name_planche);
viewItem.txtPriceBaget = (TextView)convertView.findViewById(R.id.Tx_price_baget);
viewItem.txtPricePlate = (TextView)convertView.findViewById(R.id.Tx_price_plate);
convertView.setTag(viewItem);
}
else
{
viewItem = (ViewItem) convertView.getTag();
}
viewItem.txtNamePlanche.setText(valueList.get(position).name);
viewItem.txtPriceBaget.setText(valueList.get(position).price_baget);
viewItem.txtPricePlate.setText(valueList.get(position).price_plate);
return convertView;
}
}
This is my ItemView
import android.widget.TextView;
class ViewItem
{
TextView txtNamePlanche;
TextView txtPricePlate;
TextView txtPriceBaget;
}
This is cources:
public class cources
{
public String name;
public String price_plate;
public String price_baget;
}
Change extends Fragment to extends ListFragment

how to write code for onitemClickListener for customized listview using BaseAdapter?

here is the code for custom listview using BaseAdapter in android its working fine:
public class CustomListAdapter extends BaseAdapter {
private ArrayList<TaskClass> _listData;
Context _c;
public CustomListAdapter(Context context, ArrayList<TaskClass> listData) {
_listData = listData;
_c = context;
}
#Override
public int getCount() {
return _listData.size();
}
#Override
public Object getItem(int position) {
return _listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
View v = convertView;
if (v == null)
{
LayoutInflater layoutInflator = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflator.inflate(R.layout.listview_row, null);
}
TextView titleText = (TextView)v.findViewById(R.id.holdTitleText);
TextView catText = (TextView)v.findViewById(R.id.holdCatText);
TextView descText = (TextView)v.findViewById(R.id.holdDescText);
TextView dateText = (TextView)v.findViewById(R.id.holdDateText);
//CheckBox checkBoxForEachItem = (CheckBox)v.findViewById(R.id.)
TaskClass taskClassInstance = _listData.get(position);
titleText.setText(taskClassInstance.getTitle());
catText.setText(taskClassInstance.getTaskCategory());
descText.setText(taskClassInstance.getDescription());
dateText.setText(taskClassInstance.getTaskDate());
return v;
}
}
and in activity i m binding listview with custom adapter :
listViewInstance.setAdapter(new CustomListAdapter(getApplicationContext(),taskClasslistInstance));
where "taskclasslistinstance" is my arraylist conatining data from DB its working fine
now i need to write function for listitemclick so that when user click on any listitem i can get rowid of that listitem record from Database.so after getting rowwid I can delete records from listview and from db and can edit informations
Well, we don't know about your TaskClass, but I expect you want something like this:
listViewInstance.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
TaskClass taskClass = taskClasslistInstance.get(pos);
Log.d(TAG, "Clicked on: " + taskClass)
// Do stuff with taskClass
}});
Here a small example from my project:
list = (ListView) rootView.findViewById(R.id.list);
adapter = new LazyAdapterAlbum(getActivity(), songs);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(), AlbumActivity.class);
Bundle args = new Bundle();
args.putString(AlbumActivity.ALBUM_NAME, adapter.getItem(position).getName());
i.putExtras(args);
startActivity(i);
}
});
You have to set an onClickListener on your ListView.
you can also set in getView method like below:
public View getView(int position,View convertView,ViewGroup parent) {
View v = convertView;
if (v == null)
{
LayoutInflater layoutInflator = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = layoutInflator.inflate(R.layout.listview_row, null);
}
TextView titleText = (TextView)v.findViewById(R.id.holdTitleText);
TextView catText = (TextView)v.findViewById(R.id.holdCatText);
TextView descText = (TextView)v.findViewById(R.id.holdDescText);
TextView dateText = (TextView)v.findViewById(R.id.holdDateText);
//CheckBox checkBoxForEachItem = (CheckBox)v.findViewById(R.id.)
TaskClass taskClassInstance = _listData.get(position);
titleText.setText(taskClassInstance.getTitle());
catText.setText(taskClassInstance.getTaskCategory());
descText.setText(taskClassInstance.getDescription());
dateText.setText(taskClassInstance.getTaskDate());
v.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Perform an action when this list item is clicked
}
});
return v;
}

Add custom layout to listview row

I have tried for a while now but I still can't seem to get it right. I can add a custom header to a listview but I can't add a custom row layout to a list. I want each row in the list to have the layout of the file "listitemrow.xml" but I can't seem to quite understand how to do that despite reading multiple tutorials.
Here is my code
public class PortfolioFragMent extends android.app.ListFragment{
private String[] ShareholdingNames;
private ListView mainListView ;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ListView listView = new ListView(getActivity());
super.onCreate(savedInstanceState);
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.listviewheader, listView, false);
listView.addHeaderView(header, null, false);
ViewGroup listrow = (ViewGroup)inflater.inflate(R.layout.listitemrow, listView, false);
//listView.add(listitemrow); I want to do something like this
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1);
setListAdapter(array);
ShareholdingNames= ShareholdingNames();
for (String str : ShareholdingNames)
array.add(str);
listView.setAdapter(array);
return listView;
}
private String[] ShareholdingNames(){
ShareholdingNames = new String[Portfolio.getPortfolio().count()];
for(int i=0; i < Portfolio.getPortfolio().count(); i++){
ShareholdingNames[i]= Portfolio.getPortfolio().getShareHolding(i).getName();
}
return ShareholdingNames;
}
public void onListItemClick(ListView l, View v, int position, long id) {
Intent intent = new Intent(getActivity().getApplicationContext(),
DetailShareHoldingActivity.class);
intent.putExtra("new_variable_name","value");
intent.putExtra("bookPositionInList",position);
startActivity(intent);
}
}
To do so you need to add your own custom adapter and add the line
ViewGroup listrow = (ViewGroup)inflater.inflate(R.layout.listitemrow, listView, false);
into the adapter's getView method..
public class ListAdapter extends ArrayAdapter<Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
// TODO Auto-generated constructor stub
}
private List<Item> items;
public ListAdapter(Context context, int resource, List<Item> items) {
super(context, resource, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.listitemrow, listView, false);
}
...
return v;
}
}

Categories