I create a ListFragment for show Text & Image my code hase not any error but when i run it , it get me crash & when I see Log it don't get me any Message ?
My StartActivity.class :
public class StartActivity extends ActionBarActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,new Setting_Fragment()).commit();
}
}
My activity_start.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:background="#drawable/back"
tools:context="in.project.StartActivity" >
<ImageView
android:id="#+id/imgHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:background="#e42769"
android:src="#drawable/head" />
<LinearLayout
android:id="#+id/LFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="6" >
<ImageView
android:id="#+id/img_Setting_note"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_setting_note" />
<ImageView
android:id="#+id/img_Calendar"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_calendar" />
<ImageView
android:id="#+id/img_Mail"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_mail" />
<ImageView
android:id="#+id/img_Review_note"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_review_note" />
<ImageView
android:id="#+id/img_Help"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_help" />
<ImageView
android:id="#+id/img_Setting"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/selector_setting" />
</LinearLayout>
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/LFooter"
android:layout_below="#+id/imgHeader" />
</RelativeLayout>
My SettingArrayAdapter.class :
public class SettingArrayAdapter extends ArrayAdapter<Instructure_setting>{
private Context context;
private List<Instructure_setting> objects;
public SettingArrayAdapter(Context context, int resource, List<Instructure_setting> objects) {
super(context, resource);
this.context = context;
this.objects = objects;
}
#SuppressLint({ "ViewHolder", "InflateParams" })
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Instructure_setting _Data = objects.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.listview_setting_fragment, null);
ImageView image = (ImageView) view.findViewById(R.id.img_message);
image.setImageResource(_Data.getImageResource());
TextView tv = (TextView) view.findViewById(R.id.txt_message);
tv.setText(_Data.gettxtMessage());
return view;
}
}
My Setting_Fragment.class :
public class Setting_Fragment extends ListFragment{
List<Instructure_setting> DATA = new Setting_Fragment_Data().getMessages();
public Setting_Fragment()
{
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SettingArrayAdapter adapter = new SettingArrayAdapter(getActivity(),
R.layout.listview_setting_fragment,
DATA);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.setting_fragment, container, false);
return rootView;
}
}
My setting_fragment.xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".StartActivity" >
<ListView
android:id="#+id/lstview_Setting_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
My Setting_Fragment_Data.class :
public class Setting_Fragment_Data {
private List<Instructure_setting> _Messages = new ArrayList<Instructure_setting>();
public List<Instructure_setting> getMessages() {
return _Messages;
}
public Setting_Fragment_Data() {
_Messages.add(new Instructure_setting("AAA", R.drawable.period_color));
_Messages.add(new Instructure_setting("BBB", R.drawable.password_color));
_Messages.add(new Instructure_setting("CCC", R.drawable.cleare_data_color));
_Messages.add(new Instructure_setting("DDD", R.drawable.chanel_connect_color));
_Messages.add(new Instructure_setting("EEE", R.drawable.backup_color));
_Messages.add(new Instructure_setting("FFF", R.drawable.review_note_color));
_Messages.add(new Instructure_setting("GGG", R.drawable.notification_color));
_Messages.add(new Instructure_setting("HHH", R.drawable.sync_server_color));
}
}
My Instructure_setting.class :
public class Instructure_setting {
// constants for field references
public static final String TXT_Message = "TextMessage";
public static final String IMAGE_RESOURCE = "imageResource";
// private fields
private String txtMessage;
private int imageResource;
// getters and setters
public String gettxtMessage() {
return txtMessage;
}
public void settxtMessage(String txtMessage) {
this.txtMessage = txtMessage;
}
public int getImageResource() {
return imageResource;
}
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
// Used when creating the data object
public Instructure_setting(String msg, int imageResource) {
this.txtMessage = msg;
this.imageResource = imageResource;
}
// Create from a bundle
public Instructure_setting(Bundle b) {
if (b != null) {
this.txtMessage = b.getString(TXT_Message);
this.imageResource = b.getInt(IMAGE_RESOURCE);
}
}
// Package data for transfer between activities
public Bundle toBundle() {
Bundle b = new Bundle();
b.putString(TXT_Message, this.txtMessage);
b.putInt(IMAGE_RESOURCE, this.imageResource);
return b;
}
// Output txtMessage data
#Override
public String toString() {
return txtMessage;
}
}
This is my listview_setting_fragment.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="wrap_content"
android:paddingBottom="10dp">
<ImageView
android:id="#+id/img_message"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:contentDescription="placeholder" />
<TextView
android:id="#+id/txt_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/img_message"
android:text="placeholder"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
NOTICE : I am using from appcompat_v7 .
When you use ListFragment or ListActivity then ListView id in xml must be :
android:id="#id/android:list
Change your ListView id in xml :
<ListView
android:id="#id/android:list"
Instead of
<ListView
android:id="#+id/lstview_Setting_fragment"
Note : Try to set List Adapter in onCreateView() instead of onCreate() in Fragment.
Related
I tried to load a mapView in the listView for each item using baseAdaptor everything seems to be fine but maps(mapviews) are loading this way(the picture below is a single item of the list):
adapter class:
public class InfoAdapter extends BaseAdapter {
protected MapView mMapView;
GoogleMap googleMap;
Activity activity;
Bundle bundle;
Context context;
LayoutInflater layoutInflater;
ArrayList<Info> infos;
public TripsAdapter(Activity activity, Context context, ArrayList<Info> infos, Bundle bundle) {
this.context = context;
this.activity = activity;
this.layoutInflater = LayoutInflater.from(activity);
this.infos = infos;
this.bundle = bundle;
}
#Override
public int getCount() {
return infos.size();
}
#Override
public Object getItem(int i) {
return infos.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = layoutInflater.inflate(R.layout.list_infos_item, viewGroup, false);
view.findViewById(R.id.btnShare).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//share it
}
});
}
mMapView = (MapView) view.findViewById(R.id.fragment_embedded_map_view_mapview);
mMapView.onCreate(bundle);
return view;
}
}
the list_infos_item.xml is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="13dip">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dip"
android:padding="8.5dip"
android:elevation="5dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="5"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="#+id/fragment_embedded_map_view_mapview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"/>
<LinearLayout
android:visibility="gone"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2">
</LinearLayout>
<Button
android:id="#+id/btnShare"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:layout_marginTop="12dip"
android:background="#color/yellow"
android:text="Share it!"
android:textAlignment="center"
android:textColor="#color/black"
android:textSize="#dimen/default_text_size" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
what is happening?how can I fix the problem?
I currently have an adapter that takes two images and a text view to add to a listView. I want to change this so that I can add a horizontalScrollView inside each list item that contains the images. Would there be any way to do this?
This my current working adapter as described above with two imageView's and a textView
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;
private PopupWindow popUpWindow;
private LayoutInflater inflater;
public MyAdapter(Context context,List<Bean> list){
mContext=context;
mList=list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_orders, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView2);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView2);
holder.imageView2 = (ImageView) convertView.findViewById(R.id.imageView3);
holder.information= (Button) convertView.findViewById(R.id.button5);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
//set text and url
final View finalConvertView = convertView;
holder.information.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) inflater.inflate(R.layout.information_popup, null);
popUpWindow = new PopupWindow(container, 800,400,true);
popUpWindow.showAtLocation(finalConvertView.findViewById(R.id.orders), Gravity.CENTER, 0,0);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popUpWindow.dismiss();
return true;
}
});
}
});
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(450,450).into(holder.imageView);
Picasso.with(mContext).load(mList.get(position).getUrl2()).resize(450,450).into(holder.imageView2);
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
ImageView imageView2;
Button information;
}
}
Getter and Setter Class
public class Bean {
String text;
String url;
String url2;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl2() {
return url2;
}
public void setUrl2(String url2) {
this.url2 = url2;
}
}
Activity Layout XML
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".OrdersActivity"
android:background="#ffffff">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/orderListView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:dividerHeight="2dp"/>
</android.support.design.widget.CoordinatorLayout>
Content Layout XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".OrdersActivity"
android:id="#+id/orders">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/imageView2"
android:src="#drawable/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/one"
android:id="#+id/imageView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/imageView2"
android:layout_alignTop="#+id/imageView2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/informaci_n"
android:id="#+id/button5"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/entregado"
android:id="#+id/button3"
android:layout_below="#+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_entregado"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I ended up figuring it out. In case anyone else wants to do this, this is the code I used. I just added a horizontalScrollView and I added a linearLayout with a horizontal direction inside the horizontalScrollView and then I added the images inside that linearLayout. This was my code.
MainActivity
public class MainActivity extends AppCompatActivity {
ListView list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String a1 = "https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Soccer_ball.svg/2000px-Soccer_ball.svg.png";
String a2 = "https://s-media-cache-ak0.pinimg.com/736x/91/27/24/912724cb45a6a2ba468024a5285b01e7.jpg";
String[] descriptionArray = {"Description 1", "Description 2"};
String[] photoArray = {a1,a2,a1,a2,a1,a2,a1};
String[] photoArray2 = {a2,a1,a2,a1,a2,a1,a2};
String[] photoArray3 = {a1,a2,a1,a2,a1,a2,a1};
String [] photoArray4 = {a2,a1,a2,a1,a2,a1,a2};
String[] photoArray5 = {a1,a2,a1,a2,a1,a2,a1};
String[] photoArray6 = {a2,a1,a2,a1,a2,a1,a2};
String[] photoArray7 = {a1,a2,a1,a2,a1,a2,a1};
list = (ListView) findViewById(R.id.listView);
List<Bean> myList = new ArrayList<>();
for(int i = 0; i < descriptionArray.length; i++) {
Bean bean = new Bean();
bean.setText(descriptionArray[i]);
bean.setUrl(photoArray[i]);
bean.setUrl2(photoArray2[i]);
bean.setUrl3(photoArray3[i]);
bean.setUrl4(photoArray4[i]);
bean.setUrl5(photoArray5[i]);
bean.setUrl6(photoArray6[i]);
bean.setUrl7(photoArray7[i]);
myList.add(bean);
}
MyAdapter adapter = new MyAdapter(MainActivity.this, myList);
list.setAdapter(adapter);
}
}
My Adapter
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List<Bean> mList;
public MyAdapter(Context context,List<Bean> list){
mContext=context;
mList=list;
}
#Override
public int getCount() {
return mList.size();
}
#Override
public Object getItem(int position) {
return mList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
//use convertView recycle
if(convertView==null){
holder=new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.content_main, parent, false);
holder.textView= (TextView) convertView.findViewById(R.id.textView);
holder.imageView= (ImageView) convertView.findViewById(R.id.imageView);
holder.imageView2 = (ImageView) convertView.findViewById(R.id.imageView2);
holder.imageView3 = (ImageView) convertView.findViewById(R.id.imageView3);
holder.imageView4 = (ImageView) convertView.findViewById(R.id.imageView4);
holder.imageView5 = (ImageView) convertView.findViewById(R.id.imageView5);
holder.imageView6 = (ImageView) convertView.findViewById(R.id.imageView6);
holder.imageView7 = (ImageView) convertView.findViewById(R.id.imageView7);
holder.horizontalScrollView= (HorizontalScrollView) convertView.findViewById(R.id.horizontalScrollView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mList.get(position).getText());
Picasso.with(mContext).load(mList.get(position).getUrl()).resize(200,200).into(holder.imageView);
Picasso.with(mContext).load(mList.get(position).getUrl2()).resize(200,200).into(holder.imageView2);
Picasso.with(mContext).load(mList.get(position).getUrl3()).resize(200,200).into(holder.imageView3);
Picasso.with(mContext).load(mList.get(position).getUrl4()).resize(200,200).into(holder.imageView4);
Picasso.with(mContext).load(mList.get(position).getUrl5()).resize(200,200).into(holder.imageView5);
Picasso.with(mContext).load(mList.get(position).getUrl6()).resize(200,200).into(holder.imageView6);
Picasso.with(mContext).load(mList.get(position).getUrl7()).resize(200,200).into(holder.imageView7);
return convertView;
}
class ViewHolder{
TextView textView;
ImageView imageView;
ImageView imageView2;
ImageView imageView3;
ImageView imageView4;
ImageView imageView5;
ImageView imageView6;
ImageView imageView7;
HorizontalScrollView horizontalScrollView;
}
}
Bean Class(Getter and Setter Class)
public class Bean {
String text;
String url;
String url2;
String url3;
String url4;
String url5;
String url6;
String url7;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl2() {
return url2;
}
public void setUrl2(String url2) {
this.url2 = url2;
}
public String getUrl3() {
return url3;
}
public void setUrl3(String url3) {
this.url3 = url3;
}
public String getUrl4() {
return url4;
}
public void setUrl4(String url4) {
this.url4 = url4;
}
public String getUrl5() {
return url5;
}
public void setUrl5(String url5) {
this.url5 = url5;
}
public String getUrl6() {
return url6;
}
public void setUrl6(String url6) {
this.url6 = url6;
}
public String getUrl7() {
return url7;
}
public void setUrl7(String url7) {
this.url7 = url7;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
android:background="#ffffff">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/list_container" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/horizontalScrollView"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="#+id/xml_full_img_linear_below_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView7" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
list_container.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="53dp"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:dividerHeight="2dp"/>
</RelativeLayout>
Not exactly what you asked, but i think this question can help you.
HorizontalScrollView inside ListView: minor vertical scroll stops horizontal scroll
I have the following code:
public class MainActivity extends Activity {
private ListView list;
private static final String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "BOE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
String books[] = {"Mercedes", "Skoda", "Volkswagen", "Audi", "Citroen", "plik"};
BookArrayAdapter bma = new BookArrayAdapter(this, books);
bma.setNotifyOnChange(true);
list.setAdapter(bma);
if (!new File(path).exists()) {
new File(path).mkdirs();
}
for (int i=0; i<books.length; i++) {
View v = list.getAdapter().getView(i, null, null);
String name = ((TextView)v.findViewById(R.id.row_text)).getText() + "";
for (File f:new File(path).listFiles()) {
if (f.isFile()) {
Button b = (Button) v.findViewById(R.id.row_button);
if (name.equals(f.getName().substring(0, f.getName().length()-4))) {
b.setText("Open");
} else {
b.setText("Download");
}
bma.notifyDataSetInvalidated();
bma.notifyDataSetChanged();
Log.d("AC", b.getText().toString());
}
}
}
}
public class BookArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public BookArrayAdapter(Context context, String[] values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.row, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.row_text);
textView.setText(values[position]);
rowView.findViewById(R.id.row_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((Button)v).getText().toString().equals("Open")) {
//open
} else {
//Download
}
}
});
return rowView;
}
}
}
and I have problems in for loop in onCreate. this Log shows correct Download/Open texts depenging on Files in file system. But the graphical representation of this list view does not changing. Here's my layout.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"
tools:context="${relativePackage}.${activityClass}" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
<RelativeLayout
android:id="#+id/main_linear_blackout"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="match_parent"
android:background="#CC000000" >
<ProgressBar
android:id="#+id/main_progress_startup"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
and my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/row_button"
android:orientation="vertical" >
<TextView
android:id="#+id/row_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="#+id/row_progress"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="invisible" />
</LinearLayout>
<Button
android:id="#+id/row_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/row_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row_button"
android:layout_alignBottom="#+id/row_button"
android:layout_alignLeft="#+id/row_button"
android:layout_alignRight="#+id/row_button"
android:layout_alignTop="#+id/row_button"
android:gravity="center"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible" />
</RelativeLayout>
how to update it? What is wrong?
EDIT:
I moved editing single row to getView(...) overrided method on my adapter class. I don't really know why my first idea was wrong, but now it works.
listView.setAdapter() is crashing my program. Anyone have any idea why the crash might be happening?
Logcat error:
AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
Activity:
public class SavedVideosFragment extends Fragment {
View rootview;
MySQLiteHelper mydb;
private SavedVideoAdapter mSavedVideoAdapter;
ListView listView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.saved_video_fragment_layout,container,false);
getSavedVideo();
listView = (ListView)rootview.findViewById(R.id.savedVideoListView);
return rootview;
}
public void getSavedVideo() {
mydb = new MySQLiteHelper(getActivity());
ArrayList savedVideoIDs = mydb.getAllSavedVideo();
mSavedVideoAdapter = new SavedVideoAdapter(getActivity(), savedVideoIDs, mydb);
listView.setAdapter(mSavedVideoAdapter);
}
}
Adapter:
public class SavedVideoAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<Object> mData = new ArrayList<Object>();
MySQLiteHelper mydb;
private LayoutInflater mInflater;
public SavedVideoAdapter(Context context, ArrayList<Object> data, MySQLiteHelper database) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = data;
mydb = database;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public String getItem(int position) {
Object obj = mData.get(position);
return "";
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Cursor res = res = mydb.getData((Integer)mData.get(position));
convertView = mInflater.inflate(R.layout.saved_video_adapter, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.thumbnail);
TextView activity = (TextView) convertView.findViewById(R.id.activity);
TextView activityDate = (TextView) convertView.findViewById(R.id.activityDate);
ImageView cancel = (ImageView) convertView.findViewById(R.id.x);
activity.setText(res.getColumnIndex("title"));
return convertView;
}
}
ListView 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">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/savedVideoListView"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Adapter Layout xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toRightOf="#id/thumbnail">
<TextView
android:id="#+id/activity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
<TextView
android:id="#+id/activityDate"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_below="#+id/activity"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Description"
android:textSize="12sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="?android:attr/listPreferredItemHeight"
android:layout_height="fill_parent"
android:layout_alignParentRight="true">
<ImageView
android:id="#+id/x"
android:layout_width="?android:attr/listPreferredItemHeight"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:src="#drawable/x" />
</RelativeLayout>
</RelativeLayout>
AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.NullPointerException
Because listView is not initialized.
Call getSavedVideo() method after initializing listView :
listView = (ListView)rootview.findViewById(R.id.savedVideoListView);
getSavedVideo();
After googling alot i never found any helpful answer, so i'm posting my issue here. My problem is as follows :
I've a nested fragment (MainActivity calls SettingsFragment and that fragment calls another fragment named as ContactFragment) that uses a listview in it. I'm setting data in adapter but and call adapter.notifyDataSetChanged(); it adds an item with in ListView but show only images not TextViews. I debugged my code line by line each variable has its values but list items are not displaying it. Below is my Code.
ContactsFragment extends Fragment {
public static List<ContactItem> contactList;
private ContactsItemAdapter adapter;
private ListView lvContacts;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
lvContacts = (ListView) getView().findViewById(R.id.lvContacts);
contactList = new ArrayList<ContactItem>();
contactList.add(new ContactItem(R.drawable.ic_action_overflow, "OverFlow", "Testing", SOS.CONTACT_TYPE_SMS));
adapter = new ContactsItemAdapter(getActivity(), R.layout.contact_item, contactList);
lvContacts.setAdapter(adapter);
}
Following is a Click Event for adding an item to the list
private void btnAddOnClick(int type){
contactList.add(new ContactItem(R.drawable.ic_action_call, "ABC", "123", "SMS"));
adapter.notifyDataSetChanged();
}
}
Following is fragment_contacts.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"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ListView
android:id="#+id/lvContacts"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
</ListView>
</LinearLayout>
By Calling btnAddOnClick adds item in listview but empty like this
here you can see it shows call icon but not showing text views values. Please tell me what i'm doing wrong.
Here is adapter
public class ContactsItemAdapter extends ArrayAdapter<ContactItem> {
private LayoutInflater mInflater;
public ContactsItemAdapter(Context context, int resource,
List<ContactItem> contactList) {
super(context, resource, contactList);
mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView tvName;
TextView tvValue;
ImageView ivIcon;
ContactItem rowData = ContactsFragment.contactList.get(position);
if (null == convertView) {
convertView = mInflater.inflate(R.layout.contact_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
tvName = holder.getName();
tvName.setText(rowData.getName());
tvValue = holder.getValue();
tvValue.setText(rowData.getValue());
ivIcon = holder.getIcon();
ivIcon.setImageResource(rowData.getIcon());
return convertView;
}
public class ViewHolder {
private View mRow;
private TextView tvName = null;
private TextView tvValue = null;
private ImageView ivIcon = null;
public ViewHolder(View row) {
mRow = row;
}
public TextView getName() {
if (null == tvName) {
tvName = (TextView) mRow.findViewById(R.id.tvName);
}
return tvName;
}
public TextView getValue() {
if (null == tvValue) {
tvValue = (TextView) mRow.findViewById(R.id.tvValue);
}
return tvValue;
}
public ImageView getIcon() {
if (null == ivIcon) {
ivIcon = (ImageView) mRow.findViewById(R.id.icon);
}
return ivIcon;
}
}
}
ContactItem Class
public class ContactItem {
private int icon;
private String name;
private String value;
private String contactType;
public ContactItem() {
icon = R.drawable.ic_action_call;
name = "";
value = "";
contactType = "";
}
public ContactItem(int icon, String nameString, String valueString,
String type) {
this.icon = icon;
this.name = nameString;
this.value = valueString;
this.contactType = type;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getContactType() {
return contactType;
}
public void setContactType(String contactType) {
this.contactType = contactType;
}
}
contact_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="55dp"
android:background="#color/holo_gray_light"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon"
/>
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray" />
<ImageView
android:id="#+id/ivCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_action_cancel" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/divider"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/ivCancel"
android:layout_toRightOf="#+id/icon"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.49"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:ellipsize="end"
android:text="Nauman Zubair"/>
<TextView
android:id="#+id/tvValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.49"
android:paddingRight="40dp"
android:ellipsize="end"
android:gravity="center_vertical"
android:text="+92 123 1234567"/>
</LinearLayout>
</RelativeLayout>
Your code should work, I think the problem is your contact_item.xml layout. Somehow your design is hiding the text. Use a simpler layout design to see if it shows the text, try getting rid of the LinearLayout:
<?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="55dp"
android:background="#color/holo_gray_light"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/desc_list_item_icon" />
<View
android:id="#+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#android:color/darker_gray" />
<ImageView
android:id="#+id/ivCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:src="#drawable/ic_action_cancel" />
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/ivCancel"
android:layout_toRightOf="#+id/icon"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:ellipsize="end"
android:text="Nauman Zubair"/>
<TextView
android:id="#+id/tvValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/ivCancel"
android:layout_toRightOf="#+id/icon"
android:paddingRight="40dp"
android:ellipsize="end"
android:gravity="center_vertical"
android:text="+92 123 1234567"/>
</RelativeLayout>