I used AsyncTask to fulfill my ListView.
public class SIPSettingsFragment extends ListFragment implements View.OnClickListener, AsyncResponse {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sipsettings, container, false);
new DownloadJSON().execute();
return rootView;
}
public class DownloadJSON extends AsyncTask<Void, Void, Void> {
/*
some code
*/
#Override
protected void onPostExecute(Void result){
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
listView = (ListView) getActivity().findViewById(android.R.id.list);
adapter = new SimpleAdapter(
getActivity(),
usersList,
R.layout.sipuser_list_item,
new String[] { TAG_USERNAME, TAG_ADDR, TAG_STATE },
new int[] { R.id.username, R.id.addr, R.id.state}
);
((SimpleAdapter) adapter).notifyDataSetChanged();
setListAdapter(adapter);
Log.d("lab", "Done");
}
And my xml
fragment_sipsettings.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/userListLayout"
android:layout_gravity="center_horizontal">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</LinearLayout>
sipuser_list_item.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username: "
android:layout_alignParentLeft="true"
android:id="#+id/usernameSIP" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/username"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/usernameSIP" />
<Button
android:id="#+id/deleteSIPUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete user"
android:layout_alignParentRight="true"/>
</RelativeLayout>
List with buttons are display correctly. How to implement button OnClickListerer for button from sipuser_list_item.xml?
Edit:
I solve my question by extend SimpleAdapter and override getView() in AsyncTask #Krupal Shah and #cylon
You can either extend SimpleAdapter or use Anonymous inner class like below, In the extending or anonymous class, you have to override getView() method. Inside getView method, you can find button by id and set click listener on that.
SimpleAdapter k=new SimpleAdapter(
getActivity(),
usersList,
R.layout.sipuser_list_item,
new String[] { TAG_USERNAME, TAG_ADDR, TAG_STATE },
new int[] { R.id.username, R.id.addr, R.id.state}
)
{
#Override
public View getView (int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
Button btn=(Button)v.findViewById(R.id.sipuser_list_item);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// click of the button
}
});
return view;
}
};
Try to overwrite the getView() method of the SimpleAdapter or write your own adapter that extends the BaseAdapter class.
adapter = new SimpleAdapter(...) {
#Override
public View getView (int position, View convertView, ViewGroup parent){
View view = super.getView(position, convertView, parent);
// get your button here
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(....); // set your onclick listener
return view;
}
}
Related
I'm new in android programming, my problem is this.
I have a ListView named(items), and 4 different fragments(tableFragment,bedFragment,DeskFragment,ChairFragment) where I implement this list. In the list I have 2 buttons with different roles. I want one button (favorites or ar doesn't matter) to make a transaction from one fragment where I have the listview to a new fragment detailsFragment and put info(like image, and textview from the list) in the details_fragment.xml . I appreciate any effort, thanks!
items.java
public class items{
// Name of the object
private String lName;
// Costs
private String lPrice;
// Details
private String lDetails;
// Image resource id
private int lImageId;
// Buttons resource id
private int lButtonF;
private int lButtonAr;
// Constructor
public items(String ObjectName,String ObjectPrice,int ImageResourceId,int ButtonFavorites,int ButtonAr,String DetailsItem){
lName=ObjectName;
lPrice=ObjectPrice;
lImageId=ImageResourceId;
lButtonAr=ButtonAr;
lButtonF=ButtonFavorites;
lDetails=DetailsItem;
}
// Getters
public String getlName() { return lName; }
public String getlPrice(){ return lPrice; }
public int getlImageId(){ return lImageId; }
public int getlButtonF() { return lButtonF; }
public int getlButtonAr() { return lButtonAr; }
public String getlDetails() { return lDetails; }
}
itemsAdapter.java
public class itemsAdapter extends ArrayAdapter<items>{
private static final String LOG_TAG = itemsAdapter.class.getSimpleName();
public itemsAdapter(Activity context, ArrayList<items> item){
super(context,0,item);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if(listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
items currentItems=getItem(position);
TextView nameTextView=(TextView)listItemView.findViewById(R.id.name_item);
nameTextView.setText(currentItems.getlName());
TextView priceTextView=(TextView)listItemView.findViewById(R.id.price_item);
priceTextView.setText(currentItems.getlPrice());
ImageView iconImageView=(ImageView)listItemView.findViewById(R.id.icon_item);
iconImageView.setImageResource(currentItems.getlImageId());
TextView detailsView=(TextView)listItemView.findViewById(R.id.details_item);
detailsView.setText(currentItems.getlDetails());
// Favorites and Ar buttons
Button buttonFavoritesItem=(Button)listItemView.findViewById(R.id.favorites_item);
buttonFavoritesItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button buttonArItem=(Button)listItemView.findViewById(R.id.ar_item);
buttonArItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
return listItemView;
}
}
tableFragment.java
public class tableFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tables_fragment, container, false);
ArrayList<items> tables=new ArrayList<items>();
tables.add(new items("Table1","23$",R.drawable.table1,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
tables.add(new items("Table2","43$",R.drawable.table2,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
tables.add(new items("Table3","54$",R.drawable.table3,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
tables.add(new items("Table4","34$",R.drawable.table4,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
tables.add(new items("Table5","65$",R.drawable.table5,R.id.ar_item,R.id.favorites_item,"Best table on the market, you can drink coffe or study."));
itemsAdapter itAdapter=new itemsAdapter(this.getActivity(),tables);
ListView listView=(ListView)view.findViewById(R.id.listview_tables);
listView.setAdapter(itAdapter);
return view;
}
}
detailsFragment.java
public class detailsFragment extends Fragment {
public detailsFragment(){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details_fragment, container, false);
return view;
}
}
details_fragment.xml
<FrameLayout
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_gravity="bottom"
android:id="#+id/details_container">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/image_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textStyle="bold" />
<TextView
android:id="#+id/name_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Details"
android:textStyle="bold" />
<TextView
android:id="#+id/details_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/ar_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAMERA AR" />
<Button
android:id="#+id/favorites_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="star" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
If you want to move data(image and textview data) between fragments, everything has to go through the base activity of both the fragments. Transferring data between 2 fragments directly is not allowed, i think(do correct me if wrong).
Creating a tabbed fragment activity, in my home_fragment i am trying to display a horizontal and a vertical list view, but its not displaying the list, below is my code of home_fragment for onCreateView() method --
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v=inflater.inflate(R.layout.fragment_home, container, false);
horizontalList = (RecyclerView)v.findViewById(R.id.horizontal_recycler);
verticalList = (RecyclerView)v.findViewById(R.id.recyle_view);
horizontalList.setHasFixedSize(true);
verticalList.setHasFixedSize(true);
//set horizontal LinearLayout as layout manager to creating horizontal list view
LinearLayoutManager horizontalManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
horizontalList.setLayoutManager(horizontalManager);
horizontalAdapter = new HorizontalListAdapter(getActivity());
horizontalList.setAdapter(horizontalAdapter);
//set vertical LinearLayout as layout manager for vertial listview
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
verticalList.setLayoutManager(layoutManager);
verticalAdapter = new VerticalListAdapter(getActivity());
verticalList.setAdapter(verticalAdapter);
return v;
}
Is there any changes to be made here.
HorizontalListAdapter.java --
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder>{
private Activity activity;
public HorizontalListAdapter(Activity activity) {
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
}
#Override
public int getItemCount() {
return 10;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
public ViewHolder(View view) {
super(view);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
}
}
}
item_horizontal_list.xml--
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/mountain" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
Not displaying horizontal and vertical list in fragment
in your onBindViewHolder you are no displaying any data check out your adapter class
You need to set any data inside your onBindViewHolder like this
Sample code
public class HorizontalListAdapter extends RecyclerView.Adapter<HorizontalListAdapter.ViewHolder> {
private Activity activity;
public HorizontalListAdapter(Activity activity) {
this.activity = activity;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.item_horizontal_list, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(HorizontalListAdapter.ViewHolder viewHolder, final int position) {
viewHolder.linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Position clicked: " + position, Toast.LENGTH_SHORT).show();
}
});
viewHolder.edtName.setText("NILU");
viewHolder.imageView.setImageResource(R.mipmap.ic_launcher);
}
#Override
public int getItemCount() {
return 10;
}
/**
* View holder to display each RecylerView item
*/
protected class ViewHolder extends RecyclerView.ViewHolder {
private LinearLayout linearLayout;
TextView edtName, edtPhone, edtPass, edtAdrress;
ImageView imageView;
public ViewHolder(View view) {
super(view);
linearLayout = (LinearLayout) view.findViewById(R.id.layout);
edtName = view.findViewById(R.id.edtUname);
imageView = view.findViewById(R.id.imageView);
}
}
}
Changes in your layout you need to set id to your imageview and textview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/mountain" />
<TextView
android:id="#+id/edtUname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/lorem_ipsum" />
</LinearLayout>
</android.support.v7.widget.CardView>
here is the good tutorial of Android working with Card View and Recycler View
I have a list view from which the user must choose two teams by checking the boxes and then validate by pressing the OK button. The problem is I want to make it so that there can only be two checked boxes at any time
Example :
if the user picks team 1 and 2 then the boxes for 1 and 2 should be checked but if the user then picks team 3, 1 should un-check automatically.
I've already managed to isolate and store the position of the box that needs to be unchecked but I don't know what to do with it.
Thanks!!
heres the listView
<Button
android:id="#+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
/>
<ListView
android:id="#+id/listEquipe"
android:layout_below="#+id/btnConfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
and the following layout :
<?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="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/txtIdEquipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<CheckedTextView
android:id="#+id/txtNomEquipe"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:padding="5dp"
android:textSize="12pt"
android:textColor="#000000"
/>
</LinearLayout>
This is one way to achieve it (I used stupid data in the adapter):
public class MainActivity extends AppCompatActivity {
private List<Integer> selectedItems = new ArrayList<>();
private String[] teams = new String[]{ "Team A", "Team B", "Team C"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView lv = (ListView) findViewById(R.id.listEquipe);
final BaseAdapter adapter = new BaseAdapter() {
#Override
public int getCount() {
return teams.length;
}
#Override
public Object getItem(int position) {
return teams[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.item, null);
((TextView) view.findViewById(R.id.txtIdEquipe)).setText((String) getItem(position));
CheckedTextView ctv = (CheckedTextView) view.findViewById(R.id.txtNomEquipe);
ctv.setChecked(selectedItems.contains(position));
return view;
}
};
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(selectedItems.contains(position)) {
selectedItems.remove(position);
}
else {
selectedItems.add(position);
if(selectedItems.size() > 2) {
selectedItems.remove(0);
}
}
adapter.notifyDataSetChanged();
}
});
}
}
I try to show listView in fragment. But method setListAdapter - is not resolved.
I think, that i must to get id of listView (android.R.id.list);
and then : lv.setAdapter(mAdapter);but its dont work too.
public class MyEmployeeFragment extends Fragment {
private CustomAdapter sAdapter;
private List<User> userList;
ProgressDialog mProgressDialog;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
userList = new ArrayList<User>();
sAdapter = new CustomAdapter(getActivity(),userList);
setListAdapter(sAdapter);
new CustomAsync().execute();
}
private class CustomAdapter extends ArrayAdapter<User> {
private LayoutInflater inflater;
public CustomAdapter(Context context, List<User> objects) {
super(context, 0, objects);
inflater = LayoutInflater.from(context);
}
class ViewHolder {
TextView id;
TextView name;
TextView lastName;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vH;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item_employee, null);
vH = new ViewHolder();
vH.id = (TextView) convertView.findViewById(R.id.tv_employee_id);
vH.name = (TextView) convertView.findViewById(R.id.tv_employee_name);
vH.lastName = (TextView) convertView.findViewById(R.id.tv_employee_last_name);
convertView.setTag(vH);
} else
vH = (ViewHolder) convertView.getTag();
final User user = getItem(position);
vH.id.setText(user.getId());
vH.name.setText(user.getName());
vH.lastName.setText(user.getLastName());
return convertView;
}
}
private class CustomAsync extends AsyncTask<Void,Void,List<User>>
{}
}
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:orientation="horizontal" >
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:divider="#B7B7B7"
/>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/empty"
android:layout_above="#+id/progressBar2"
android:layout_alignRight="#+id/progressBar2"
android:layout_alignEnd="#+id/progressBar2"
android:layout_marginBottom="83dp">
</TextView>
</RelativeLayout>
setListAdapter is a method of ListFragment while your fragment extends Fragment.
http://developer.android.com/reference/android/app/ListFragment.html#setListAdapter(android.widget.ListAdapter)
So you either extend ListFragment
or
Extend Fragment inflate a layout with ListView, initialize ListView in onCreateView of Fragment and then use listView.setAdapter(adapter).
In case you want to extend Fragment
<ListView
android:id="#+id/list"
Then
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.yourxml, container, false);
ListView lv = (ListView)rootView.findViewById(R.id.list);
lv.setAdapter(youradapter);
return rootView;
}
setListAdapter() is a method in ListFragment..For this you need to extend ListFragmnet instead of Fragment.
Change this line
public class MyEmployeeFragment extends Fragment
into
public class MyEmployeeFragment extends ListFragment
You need define the ListView first:
ListView list = (ListView) findById(android.R.id.list);
And later you need put the adapter:
list.setAdapter(sAdapter);
Or change
extends Fragment
to
extends ListFragment
extends ListFragment should be working fine.
Below is a sample that works for me..
public class Chicks extends ListFragment implements OnItemClickListener {
}
I am having a weird problem and can't seem to find whats causing it. Every view component in my ArrayAdapter is being located in the getView method except my button which is causes a NullPointerException.
Adapter:
public class ItemAdapter extends ArrayAdapter<Item>{
private Context context;
private int resource;
private List<Item> list;
public ItemAdapter(Context context, int resource, List<Item> list)
{
super(context, resource, list);
this.context = context;
this.resource = resource;
this.list = list;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
convertView = LayoutInflater.from(context).inflate(resource, parent,false);
TextView name = (TextView) convertView.findViewById(R.id.itemName);
ImageView itemImg = (ImageView) convertView.findViewById(R.id.itemImg);
Button addToCart = (Button) convertView.findViewById(R.id.button1);
final Item item = list.get(position);
name.setText(item.getName());
UrlImageViewHelper.setUrlDrawable(itemImg, item.getPicture());
addToCart.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view)
{
Toast.makeText(context, "Clicked", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
}
Fragment with listview
#InjectView(R.id.itemList) private ListView listView;
#Inject private ItemDao dao;
private ArrayAdapter<Item> adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_shop, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
dao.open();
List<Item> items = dao.findAll();
dao.close();
adapter = new ItemAdapter(getActivity(),R.layout.list_product_item, items);
listView.setAdapter(adapter);
}
list_product_item
<?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"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/itemImg"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginRight="5dp"
android:scaleType="centerCrop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/itemStoreName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Now the call to convertView.findViewById(R.id.button1) returns a null instance. What is the cause of this and how can I fix it.
do you have right the name of the layout?
adapter = new GroceryItemAdapter(getActivity(), R.layout.list_textview, items);
vs.
list_product_item
(I am assuming this is the file name for R.layout.list_product_item)
Instead of getting the layout inflater using the from method try initializing the inflater in the constructor..
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
in the getView
convertView = mInflater.inflate(your layout, null);