Android - update ListView in onCreate does not work - android

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.

Related

Android - listView not showing

I try to make a list view with content from realm, but problem is that nothing shows when I open activity in emulator, just white screen.
Code is
public class ListActivity extends AppCompatActivity {
ListView list;
Realm realm;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_items);
realm = Realm.getDefaultInstance();
RealmResults<ArticleList> results = realm.where(ArticleList.class).equalTo("category", "Танковые сражения").findAll();
ListActivityListAdapter adapter = new ListActivityListAdapter(this, results);
list = (ListView) findViewById(R.id.listactivity_listview);
list.setAdapter(adapter);
button = (Button) findViewById(R.id.button);
}
}
public class ListActivityListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final RealmResults<ArticleList> articleList;
public ListActivityListAdapter(Activity context, RealmResults<ArticleList> articleList) {
super(context, R.layout.listview_listactivity_item);
this.context = context;
this.articleList = articleList;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.listview_listactivity_item, null, true);
TextView title = (TextView) rowView.findViewById(R.id.listview_item_title);
TextView subtitle = (TextView) rowView.findViewById(R.id.listview_item_subtitle);
ImageView image = (ImageView) rowView.findViewById(R.id.listview_item_imageView);
ArticleList item = articleList.get(position);
title.setText(item.getTitle());
subtitle.setText(item.getSubtitle());
String imageName = item.getImage();
File imageFile = ImageStorage.getImage(imageName, getContext());
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeFile(context.getFilesDir() + "/tanks/" + imageName + ".jpg");
} catch (Exception e) {
e.printStackTrace();
}
image.setImageBitmap(bitmap);
return rowView;
}
}
activity_list_of_items.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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.me.demo2.ListActivity"
tools:showIn="#layout/listview_main_list">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listactivity_listview"
android:layout_gravity="center_horizontal|top"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="164dp" />
listview_listactivity_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="213dp"
android:id="#+id/listview_item_imageView"
android:layout_gravity="center_horizontal|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listview_item_title"
android:paddingTop="100dp"
android:layout_gravity="center_horizontal|top" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listview_item_subtitle"
android:paddingTop="150dp"
android:layout_gravity="center_horizontal|top" />
What did I do wrong ?
I think you're missing the getCount() method in your ArrayAdapter. Also, dont forget about #Override annotation for all the method you're overriding.
Try adapter.notifyDataChanged() after list.setAdapter(adapter);

ListView with BaseAdapter and LinearLayout?

I'm trying create a BaseAdapter to my ListView. The problem is when I do create a LinearLayout inside other LinearLayout the listener OnItemClickListener doesn't works. If I put the components outside of LinearLayout works fine.
How could I do this works ?
ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/lvEntregasPendente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
></ListView>
</LinearLayout>
Adapter XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffe3b3"
android:layout_margin="5dp"
android:padding="2dp"
android:id="#+id/llEntregaPendenteVendas">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvVenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Venda"
android:textColor="#color/action_bar"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrega em: "
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/tvDataEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_weight="1"
/>
<CheckBox
android:id="#+id/cbEntregue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/action_bar"
android:text="Entregue"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvAtrasoEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Atraso de: 20 dias"
android:textColor="#FF0000"
android:padding="5dp"
android:visibility="visible"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter
public class EntregaPendenteListAdapter extends BaseAdapter {
private Context context;
private List<Venda> lista;
private DateControl dateControl;
private EntregaPendenteFrag rpf;
private Venda venda;
public EntregaPendenteListAdapter(Context context, List<Venda> lista, EntregaPendenteFrag rpf) {
this.context = context;
this.lista = lista;
this.rpf = rpf;
dateControl = new DateControl();
}
/** limpa a lista */
public void clearList(){
lista.clear();
notifyDataSetChanged();
}
/** altera lista */
public void changeList(List<Venda> lista){
this.lista = lista;
notifyDataSetChanged();
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Venda venda = lista.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.entregas_pendente_adapter, parent, false);
holder.llEntregaPendenteVendas = (LinearLayout) convertView.findViewById(R.id.llEntregaPendenteVendas);
holder.tvVenda = (TextView) convertView.findViewById(R.id.tvVenda);
holder.tvDataEntrega = (TextView) convertView.findViewById(R.id.tvDataEntrega);
holder.tvAtrasoEntrega = (TextView) convertView.findViewById(R.id.tvAtrasoEntrega);
holder.cbEntregue = (CheckBox)convertView.findViewById(R.id.cbEntregue);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.tvVenda.setText("Venda: " + FormataCodigo.getCodFormat(venda.getId()));
if(venda.getData_entrega() != null){
holder.tvDataEntrega.setText(new SimpleDateFormat("dd-MM-yyyy").format(venda.getData_entrega()));
if(dateControl.getDiasVencido(venda.getData_entrega()) > 0){
holder.tvAtrasoEntrega.setText("Atraso de: " + new DateControl().getDiasVencido(venda.getData_entrega()) + "dias");
holder.tvAtrasoEntrega.setVisibility(View.VISIBLE);
}
}
if((position % 2) == 0){
holder.llEntregaPendenteVendas.setBackgroundColor(Color.parseColor("#ffe3b3"));
}else{
holder.llEntregaPendenteVendas.setBackgroundColor(Color.WHITE);
}
return convertView;
}
private static class ViewHolder{
LinearLayout llEntregaPendenteVendas;
TextView tvVenda;
TextView tvDataEntrega;
TextView tvAtrasoEntrega;
CheckBox cbEntregue;
}
}
Activity
//listview
lvEntregasPendente = (ListView)view.findViewById(R.id.lvEntregasPendente);
lvEntregasPendente.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ITEM->", position + "");
}
It seems your CheckBox is stealing the focus.
Try setting these properties on it:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

I can't see ListFragment android?

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.

How to set item height in custom listView

I implemented a custom listView as seen here:
http://www.tutecentral.com/android-custom-navigation-drawer/ but I can't get a correct height for my items in the list. Both header and section layout are way too large (height).
Here is my xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/profileLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="0dp"
>
<com.facebook.widget.ProfilePictureView
android:id="#+id/drawer_profilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
<TextView
android:id="#+id/drawer_profileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/button_logout"
android:text="Logout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/sectionLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
>
<TextView
android:id="#+id/drawer_sectionName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_marginTop="0dp"
>
<TextView
android:id="#+id/drawer_headerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:background="#DADADC" ></View>
</LinearLayout>
Any suggestion ?
[EDIT] Here is my customDrawerAdapter getview code:
public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {
private final JSONObject userProfile;
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
public CustomDrawerAdapter(Context context, int layoutResourceID,
List<DrawerItem> listItems, JSONObject userProfile) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
this.userProfile = userProfile;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.profileName = (TextView) view
.findViewById(R.id.drawer_profileName);
drawerHolder.profilePicture = (ProfilePictureView) view.findViewById(R.id.drawer_profilePicture);
drawerHolder.sectionName = (TextView) view.findViewById(R.id.drawer_sectionName);
drawerHolder.headerName = (TextView) view.findViewById(R.id.drawer_headerName);
drawerHolder.buttonLogout = (Button) view.findViewById(R.id.button_logout);
drawerHolder.buttonLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ParseUser.logOut();
Intent intent = new Intent(getContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
}
});
drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);
drawerHolder.sectionLayout = (LinearLayout) view.findViewById(R.id.sectionLayout);
drawerHolder.headerLayout = (LinearLayout) view.findViewById(R.id.headerLayout);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = this.drawerItemList.get(position);
if (dItem.isHeader()) {
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.headerName.setText(dItem.getHeaderName());
} else if (dItem.getProfilePicture() == 0){
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionName.setText(dItem.getSectionName());
} else {
drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.INVISIBLE);
try {
if (userProfile.getString("facebookId") != null) {
String facebookId = userProfile.get("facebookId")
.toString();
drawerHolder.profilePicture.setProfileId(facebookId);
} else {
// Show the default, blank user profile picture
drawerHolder.profilePicture.setProfileId(null);
}
if (userProfile.getString("name") != null) {
drawerHolder.profileName.setText(userProfile.getString("name"));
} else {
drawerHolder.profileName.setText("");
}
} catch (JSONException e) {
Log.d(CarpoolMeApp.TAG,
"Error parsing saved user data.");
}
}
return view;
}
You can just set the height manually for both the headerName and the sectionName Textviews
android:layout_height="2dp"
like you did with the View in your headerLayout
You can specify layouts gravities, it is most possible reason of issue (too much height).
Also you can use LinearLayout as root layout instead of RelativeLayout.
If the exact height needed, specify it in the layout_height parameter.

UI in Graphical Layout and in device are not the same [Android]

ListView in Graphical Layout is fit on appropriate height but when i imported into android device, ListView is reduced in a small field. i want ListView are the same in Graphical Layout in xml file and in real device screen
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10sp"
android:id="#+id/header"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/id"
android:layout_weight="1.25"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/amount"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/alert"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/detail"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10sp"></ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.5">
<Button
android:id="#+id/addQueue"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:text="#string/addQueue"/>
<Button
android:id="#+id/submit"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:text="#string/submit"/>
<Button
android:id="#+id/reset"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/addQueue"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="#string/resetQueue" />
</RelativeLayout>
I have tried to modify your layout. Try this may be it will be helpful for you.. just create a new xml layout file and check it is working for you or not.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/addQueue"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/submit"
android:text="addQueue" />
<Button
android:id="#+id/submit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="submit" />
<Button
android:id="#+id/reset"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="resetQueue" />
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.25"
android:gravity="center"
android:text="text1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text4" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/addQueue"
android:layout_alignParentLeft="true"
android:layout_below="#+id/header" >
</ListView>
</RelativeLayout>
My custom adapter class is here. :
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
String[] elements = null;
private ArrayList<Boolean> itemChecked = null;
public my_custom_adapter(Context context, int type, String[] elements2)
{
super(context, type, elements2);
this.elements = elements2;
this.context = context;
itemChecked = new ArrayList<Boolean>();
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("master_check_change")) {
boolean check_value = intent.getBooleanExtra("check_value", false);
set_checked(check_value);
notifyDataSetChanged();
}
}
};
context.registerReceiver(receiver, new IntentFilter("master_check_change"));
set_checked(false);
}
// AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
private void set_checked(boolean is_checked)
{
for (int i=0; i < elements.length; i++) {
itemChecked.add(i, is_checked);
}
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public CheckBox cb;
public ImageView iv;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.tv.setText(elements[position]);
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked.set(position, isChecked);
}
});
if(position < itemChecked.size()) {
holder.cb.setChecked(itemChecked.get(position));
}
}
return rowView;
}
}
My activity class is here where i am create custom adapter object,
public class Test_stflowActivity extends Activity {
public CheckBox master_cb = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp_layout);
String[] elements = new String[10];
for (int i = 0; i < 10; i++) {
elements[i] = "elements " + i;
}
CheckBox master_cb = new CheckBox(getApplicationContext());
master_cb.setText("Check All");
//HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
ListView lv = (ListView) findViewById(R.id.listView1);
//HERE I AM CREATING CUSTOM ADAPTER OBJECT.
my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
lv.addHeaderView(master_cb);
lv.setAdapter(adapter);
master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Intent my_intent = new Intent("master_check_change");
my_intent.putExtra("check_value", isChecked);
sendBroadcast(my_intent);
}
});
}
}

Categories

Resources