error: NullPointerException - java

I'm trying to set an Adapter this way to display a ListView:
public class DrugAdapter extends ArrayAdapter {
Context context;
Resources resources;
private List<DrugModel> drug;
private LayoutInflater myInflater;
public DrugAdapter(Context context, ArrayList<DrugModel> listOfDrugs) {
super(context, R.layout.activity_main, listOfDrugs);
this.context = context;
this.drug = listOfDrugs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
resources = context.getResources();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.activity_main, parent, false);
TextView name = (TextView) convertView.findViewById(R.id.list_item_name);
TextView description = (TextView) convertView.findViewById(R.id.list_item_description);
name.setText(drug.get(position).getName()); // HERE IS WHERE THE ERROR MESSAGE OCCURS
Typeface tf = Typeface.createFromAsset(context.getAssets(), "arial.ttf");
name.setTypeface(tf);
description.setText(drug.get(position).getDescription());
return convertView;
}
}
But I'm getting this error message:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.mypharmacy.adapters.DrugAdapter.getView(DrugAdapter.java:47)
at android.widget.AbsListView.obtainView(AbsListView.java:2465)
at android.widget.ListView.makeAndAddView(ListView.java:1775)
at android.widget.ListView.fillDown(ListView.java:678)
activity_main.xml
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.mypharmacy.app.MainActivity">
<EditText
android:id="#+id/search_drugs_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/search_for_a_drug"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<requestFocus/>
</EditText>
<ListView
android:id="#+id/list_of_drugs"
android:layout_below="#id/search_drugs_edittext"
android:layout_width="fill_parent"
android:layout_height="425dp"
android:minHeight="?android:attr/listPreferredItemHeight" />
</RelativeLayout>
list_item.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:minHeight="70dp" >
<TextView
android:id="#+id/list_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name: init value"
android:textColor="#008019"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:id="#+id/list_item_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="description: init value"
android:textColor="#0080A9"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_below="#+id/list_item_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="54dp"
android:layout_marginTop="50dp" />
</RelativeLayout>
With which I couldn't be able to figure out what's wrong. How to fix this, please ? Why does the name variable takes the value of Null ?

I think that your problem is in this line.
convertView = inflater.inflate(R.layout.activity_main, parent, false);
You are probably inflating the wrong layout, if that is the case, even if you call this line:
convertView.findViewById(R.id.list_item_name);
and R.id.list_item_name is valid, that id is not in that layout. If you call to findViewById having inflated a different layout, you will receive a null item.
Hope it helps

Your activity_main.xml does not have a TextView with id list_item_name. The TextView name is null.
Change this
convertView = inflater.inflate(R.layout.activity_main, parent, false);
to
convertView = inflater.inflate(R.layout.list_item, parent, false);
Also consider using a ViewHolder pattern
http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Related

Fragment viewpager No adapter attached; skipping layout

I tried a lot of solutions but no solution has worked for me.
I am getting data from the server and showing it inside a recycleview. The data shows fine but shows this error in the logcat
Access denied finding property "persist.vendor.log.tel_dbg"
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
sometimes when I refresh again and again the recyclerview becomes empty.
I am using viewpager consisting of 2 fragments inside a parent fragment.
this recycleview is using in a nested scrollview
some time data not showing
Code snippet:
homebuyer_adapter_recycler=new homebuyer_adapter_recycle(getActivity(), items);
LinearLayoutManager home = new LinearLayoutManager(getActivity());
home.setOrientation(LinearLayoutManager.VERTICAL);
allitemsgrid.setLayoutManager(home);
allitemsgrid.setAdapter(r);
Here is more about
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent,
final int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.items_view,parent,
false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
ViewHolder viewHolder = new ViewHolder(v);
HERE IS MY item view
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
app:cardCornerRadius="6dp"
android:layout_marginBottom="3dp"
app:cardBackgroundColor="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:id="#+id/image_items"
android:scaleType="fitXY"
/>
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:background="#drawable/blackshade"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/items_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:fontFamily="sans-serif-smallcaps"
android:paddingLeft="20dp"
android:text="Message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Timing"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:fontFamily="#font/mylight"
android:layout_alignParentRight="true"
android:textSize="10dp"
android:id="#+id/shoptimming"/>
</RelativeLayout>
<TextView
android:id="#+id/name_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:alpha="0.6"
android:fontFamily="#font/arimo_bold"
android:paddingLeft="20dp"
android:text="Name"
android:textColor="#color/black"
android:textSize="17dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<TextView
android:id="#+id/minumum_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:drawablePadding="10dp"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:text="Minimum " />
<TextView
android:id="#+id/price_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif"
android:gravity="left"
android:paddingLeft="20dp"
android:text="charges"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here is my adapter class
public class homebuyer_adapter_recycle extends RecyclerView.Adapter<homebuyer_adapter_recycle.ViewHolder> {
private ArrayList<seller_information> listData;
private LayoutInflater layoutInflater;
int lastpostition=-1;
Context context;
public homebuyer_adapter_recycle(Context aContext, ArrayList<seller_information> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
context=aContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder
(#NonNull ViewGroup parent, final
int viewType) {
View v = LayoutInflater.from(parent.getContext()).
inflate(R.layout.items_view,parent, false);
ViewHolder viewHolder = new ViewHolder(v);
Log.i("inadapter","calling time");
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
phone_number_shop = listData.get(viewType).getPhonenumber();
homebuyer.Delivery_charges_shop =
listData.get(viewType).getDiliveryfee();
homebuyer.Shop_name = listData.get(viewType).getShopname();
homebuyer.minimum_order = listData.get(viewType).getMinorder();
//profession=items.get(i).getName();
// Toast.makeText(getActivity(),phn,Toast.LENGTH_SHORT).show();
all_and_cetegory_items items = new all_and_cetegory_items();
Bundle b = new Bundle();
b.putString("phone",homebuyer.phone_number_shop);
items.setArguments(b);
FragmentTransaction fragmentTransaction =
((AppCompatActivity)context).getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment, items);
fragmentTransaction.addToBackStack(null).commit();
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(listData.get(position).getShopname());
//
Picasso.with(context).load(listData.get(position)
.getShopimage()).into(holder.shopimage);
Glide.with(context).load(listData.get(position).getShopimage())
.into(holder.shopimage);
holder.dilivery.setText("Rs "+listData.get(position).getDiliveryfee()+"
Delivery fee");
holder.minorder.setText("Rs "+listData.get(position).getMinorder()+" minimum");
holder.items.setText(listData.get(position).getShopmessage());
holder.time.setText("Service Available
"+listData.get(position).getStartingtime()+" to
"+listData.get(position).getEndingtime());
Log.i("inadapter","calling time"+listData.get(position).getShopname());
}
#Override
public int getItemCount() {
return listData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView dilivery;
TextView items;
TextView minorder;
TextView time;
ImageView shopimage;
public ViewHolder(#NonNull View v) {
super(v);
name = (TextView) v.findViewById(R.id.name_item);
dilivery = (TextView) v.findViewById(R.id.price_item);
minorder = (TextView) v.findViewById(R.id.minumum_order);
items=(TextView)v.findViewById(R.id.items_all);
time=(TextView)v.findViewById(R.id.shoptimming);
shopimage=(ImageView) v.findViewById(R.id.image_items);
}
}
}
In error it is saying that the Adapter for RecylerView is not attached. So, try to add adapter to the layout using:
recyclerView.setAdapter(categoryAdapter);
I am assuming homebuyer_adapter_recycler is your adapter.
According to that, you haven't really set your adapter as the logcat is specifying.
Add the below code instead of allitemsgrid.setAdapter(r);
allitemsgrid.setAdapter(homebuyer_adapter_recycler);
If my assumption or suggestion is wrong, it is okay. It is probably because your question is not very clear and does not provide the necessary details. Please provide more details such as the adapter class code and what is allitemsgrid, homebuyer_adapter_recycler and r.

How to add layout.xml to another layout.xml programmatically

I have Fragment that contain Linear layout with orientation "horizontal" (ItemViewContainer).
I'm created another layout that container imageView and textView (ItemView).
I want to add the ItemView to the ItemViewContainer programmatically a few times.
How can i do that instead of adding the ItemView with from ItemViewContainer.
ItemView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/LLTopServiceRootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="#+id/CVServiceIMGHolder"
android:layout_width="50dp"
android:layout_height="50dp"
android:translationZ="5dp"
card_view:cardBackgroundColor="#color/fragment_background_color"
card_view:cardCornerRadius="100dp"
card_view:elevation="5dp">
<ImageView
android:id="#+id/IVServiceIMG"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:tint="#color/text_color"
card_view:srcCompat="#drawable/lights_icon" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/TVServiceName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/text_color"
android:layout_gravity="center_horizontal"
android:text="Service\nName"
android:textAlignment="center" />
</LinearLayout>
ItemViewContainer
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:orientation="vertical"
android:translationZ="5dp"
app:cardBackgroundColor="#color/fragment_background_color"
app:cardCornerRadius="5dp"
card_view:elevation="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
android:id="#+id/LLTopServiceContainer"
</LinearLayout>
</androidx.cardview.widget.CardView>
Fragment that contain layout with the ItemViewContainer:
public class HomeFragment extends Fragment {
private View view;
private TextView mTVPageTitle, mTVHomeNavigationTXT;
private LottieAnimationView mLAVHome;
private ButtonsManager bm;
private LinearLayout mLLTopServiceContainer;
private final String TAG = "HomeLifeCycle";
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_home, container, false);
bm = new ButtonsManager(getContext());
initView(view);
Log.d(TAG, "onCreateView: ");
return view;
}
private void initView(View view) {
if (getActivity() != null) {
mLAVHome = getActivity().findViewById(R.id.LAVHome);
mTVPageTitle = getActivity().findViewById(R.id.TVPageTitle);
mTVHomeNavigationTXT = getActivity().findViewById(R.id.TVHome);
mLLTopServiceContainer = view.findViewById(R.id.LLTopServiceContainer);
addSelectedIconStyle();
}
}
}
I want to add the itemView 4th times
You could do it dynamically like this way : Simple Android RecyclerView example
Or adding manually:
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout= (LinearLayout) findViewById(R.id.LLTopServiceContainer);
View view = (View) inflater.inflate(R.layout.item:view_name_file, linearLayout, true);

Interacting with one element in Android ListView affects multiple

I have a list view of search items fetched from various web pages like so:
There is a horizontal scroll view on the titles so that users can fully see the title. However, when I scroll on one of the items, another item about 6 down from the item I was interacting with is also scrolled to the right. How can I prevent this from happening?
Update: Here's my CustomAdapter Code:
public class CustomAdapter extends ArrayAdapter<Item> implements View.OnClickListener{
private ArrayList<Item> dataSet;
Context mContext;
// View lookup cache
private static class ViewHolder {
TextView itemTitle;
TextView itemStore;
TextView itemPrice;
ImageView storeLogo;
}
public CustomAdapter(ArrayList<Item> data, Context context) {
super(context, R.layout.row_item, data);
this.dataSet = data;
this.mContext=context;
}
#Override
public void onClick(View v) {
int position=(Integer) v.getTag();
Object object= getItem(position);
Item item=(Item)object;
switch (v.getId())
{
case R.id.store:
Snackbar.make(v, "This item is being sold at " + item.getStore() + "." , Snackbar.LENGTH_LONG)
.setAction("No action", null).show();
break;
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title);
//viewHolder.itemStore = (TextView) convertView.findViewById(R.id.type);
viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price);
viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.store);
result = convertView;
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
viewHolder.itemTitle.setText(item.getTitle());
viewHolder.itemPrice.setText("$" + item.getPrice());
viewHolder.storeLogo.setOnClickListener(this);
viewHolder.storeLogo.setTag(position);
// Return the completed view to render on screen
return convertView;
}
}
Here is my fragment_search.xml
<RelativeLayout 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="com.painlessshopping.mohamed.findit.Search$PlaceholderFragment">
<Button
android:text="Next Page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonUp"
android:onClick="buttonClicked"
android:elevation="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="49dp" />
<Button
android:text="Previous Page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonDown"
android:onClick="buttonClicked"
android:elevation="0dp"
android:layout_marginStart="62dp"
android:layout_alignParentBottom="true"
android:layout_toEndOf="#+id/buttonUp" />
<EditText
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_width="250dp"
android:hint="What can I help you find?"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<ListView
android:layout_width="match_parent"
android:id ="#+id/listView"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_height="375dp" />
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchBtn"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/editText" />
Here is my row_item.xml (For the custom adapter)
<?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="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:maxLines="1"
android:text="Sample Text for Android Horizontal Scroll View Goes Here"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/dgrey"/>
</HorizontalScrollView>
<ImageView
android:id="#+id/store"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:src="#drawable/ic_favorite_border_black_24dp" />
</LinearLayout>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/horizontalScrollView"
android:text="$39.99"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="20sp"
android:textColor="#color/colorAccent"
android:textStyle="bold"
/>
</LinearLayout>
because of performance in constructor we check and see if we are able , we will use the previous layout that has generated, and just try to replace their values, but in your case because you can not change the scrol bar of view so your new item and previous items will have the same scroll bar view. i hope i were clear. so you have to always generate new layout like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Item item = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
final View result;
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.row_item, parent, false);
viewHolder.itemTitle = (TextView) convertView.findViewById(R.id.title);
//viewHolder.itemStore = (TextView) convertView.findViewById(R.id.type);
viewHolder.itemPrice = (TextView) convertView.findViewById(R.id.price);
viewHolder.storeLogo = (ImageView) convertView.findViewById(R.id.store);
result = convertView;
convertView.setTag(viewHolder);
viewHolder.itemTitle.setText(item.getTitle());
viewHolder.itemPrice.setText("$" + item.getPrice());
viewHolder.storeLogo.setOnClickListener(this);
viewHolder.storeLogo.setTag(position);
// Return the completed view to render on screen
return convertView;
}
also maybe there were a better way that i don't know, but this will solve your problem at least.
UPDATE :
for more information about how listview work and performance tip this link seem's good

Android EditText number keyboard not popping up upon click

I have a ListView which derives from a custom row which I have created. This is all working fine, accept the fact that my EditText refuses to show the number keyboard.
Below is my 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:layout_gravity="center_vertical">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/chkPlayer" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/txtName"
android:layout_toRightOf="#+id/chkPlayer"
android:layout_alignBottom="#+id/chkPlayer"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/started"
android:layout_below="#+id/txtName"
android:layout_marginLeft="26dp"
android:id="#+id/chkStarted" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/captain"
android:layout_below="#+id/txtName"
android:layout_toRightOf="#+id/chkStarted"
android:id="#+id/chkCaptain" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gk"
android:layout_below="#+id/txtName"
android:layout_toRightOf="#+id/chkCaptain"
android:id="#+id/chkGK" />
<EditText
android:layout_width="50dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/txtTime"
android:layout_marginLeft="#dimen/margin10"
android:hint="#string/time"
android:layout_alignBottom="#+id/chkGK"
android:layout_toRightOf="#+id/chkGK" />
</RelativeLayout>
I have tried several additional approaches however simply cannot get it to work, below is my custom Array Adapter:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if(rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row_squad, parent, false);
holder = new Holder();
holder.txtName = (TextView)rowView.findViewById(R.id.txtName);
holder.chkPlayer = (CheckBox)rowView.findViewById(R.id.chkPlayer);
holder.txtTime = (EditText)rowView.findViewById(R.id.txtTime);
rowView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
holder.txtName.setText(playerNames.get(position));
holder.chkPlayer.setFocusable(false);
holder.chkStarted.setFocusable(false);
holder.chkCaptain.setFocusable(false);
holder.chkGK.setFocusable(false);
//holder.txtTime.setFocusable(true);
//InputMethodManager im = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
//im.showSoftInput(holder.txtTime, InputMethodManager.SHOW_IMPLICIT));
//EditText txtTime = (EditText)rowView.findViewById(R.id.txtTime);
return rowView;
}
Try to use like this
android:inputType="numberDecimal"

Button disappears at runtime, visible in Eclipse design view

My app is for reading news from one site. The news parse from RSS feed and display as list of elements that include title and date. The main layout is ListView, and the layout post_entry for messages (news) looks like this:
<?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="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:id="#+id/post_title">
</TextView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
android:paddingLeft="5dp"
android:paddingBottom="5dp"
android:id="#+id/post_pubDate">
</TextView>
</LinearLayout>
One TextView for the title, and another is for date.
Adapter for this view looks like this:
public class PostAdapter extends ArrayAdapter<PostItem> {
public ArrayList<PostItem> messages;
public LayoutInflater inflater;
public PostAdapter(Activity context, int resource,
ArrayList<PostItem> objects) {
super(context, resource, objects);
messages = objects;
inflater = LayoutInflater.from(context);
}
static class ViewHolder {
public TextView titleView;
public TextView pubDateView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.post_entry, null, true);
holder = new ViewHolder();
holder.titleView = (TextView) convertView
.findViewById(R.id.post_title);
holder.pubDateView = (TextView) convertView
.findViewById(R.id.post_pubDate);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleView.setText(messages.get(position).title);
holder.pubDateView.setText(messages.get(position).date);
return convertView;
}
}
I want to add the button for refresh in the main activity.
After in the main activity looks like that:
<?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="fill_parent"
android:orientation="vertical"
>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.8" />
<Button
android:id="#+id/refresh"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Refresh"
android:onClick="Update"/>
</LinearLayout>
In graphical mode in Eclipse I see the list of items and the button below it.
Everything seems ok, but when I run my app there is no button on the screen. I see just list of the news.
Do you know why can this be? And how to add the button below the list?

Categories

Resources