2

The title might be confusing but that's my situation, I have a layout: get_my_sensors_activity.xml which includes this: app_bar_get_sensors.xml and that one includes: content_get_my_sensors.xml. In the last one: content_get_my_sensors.xml, I have a ListView and I want to inflate it with custom_item_sensors_list.xml which is my layout to customize the items.

How can I do that, right now, my getView is actually working "good" 'cause I know that is reaching every item of the list, but in the layout only paints the last one.

here is my code:

get_my_sensors_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<include
    layout="@layout/app_bar_get_sensors"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_get_sensors.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="and.greenggers.smartdoo.MainActivity">

<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_get_my_sensors"
        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/content_get_my_sensors" />

</android.support.design.widget.CoordinatorLayout>

content_get_my_sensors.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:id="@+id/sensorsLV"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:divider="@color/white" />

</RelativeLayout>

And this my activity:

public class GetMySensors extends AppCompatActivity 
 implements NavigationView.OnNavigationItemSelectedListener, 
   AdapterView.OnItemClickListener {

private String mUserId;
private String mUserToken;
private ListView mList;
private List<Sensor> mGlobalSensorList = new ArrayList<>();

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.get_my_sensors_activity);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_get_my_sensors);
    setSupportActionBar(toolbar);

    Utils.addDrawer(this);//Adds the drawer

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = 
    new (this,drawer,toolbar,R.string.navigation_drawer_open,
    R.string.navigation_drawer_close);           
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView)(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    mList = (ListView) findViewById(R.id.sensorsLV);
    mList.setOnItemClickListener(this);

    new GetMySensorsAsync().execute();

}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    ///
}

private void setSensorListAdapter() {
    CustomSensorList mAdS = new 
          CustomSensorList(this, R.layout.custom_item_sensors_list,
                                                 mGlobalSensorList);
    mList.setAdapter(mAdS);
}

private class GetMySensorsAsync extends AsyncTask<> {

    @Override
    protected GetMySensorsResp doInBackground(Void... params) {
        // consult the web service and get the data
        return getMySensorsResp;
    }

    @Override
    protected void onPostExecute(GetMySensorsResp getMySensorsResp) {
        mGlobalSensorList = getMySensorsResp.getSensorsDataSDT();
        setSensorListAdapter();
    }
}

private class CustomSensorList extends ArrayAdapter<Sensor> {

    private Context context;
    private List<Sensor> mSensorList;

    public CustomSensorList(Context context,
                    int resource, List<Sensor> objects) {
        super(context, resource, objects);
        this.context = context;
        this.mSensorList = objects;
    }

    @Override
    public int getCount() {
        return mSensorList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) 
        context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);  
        View view = inflater.inflate(R.layout.custom_item_sensors_list, 
        parent, false);

        Sensor mSensor = mSensorList.get(position);
        TextView sensorName = (TextView)   
                                       view.findViewById(R.id.sensor_name);
        TextView sensorStatus = (TextView) 
                                       view.findViewById(R.id.sensor_value);

        sensorName.setText(mSensor.getSensorDataName());
        sensorStatus.setText(mSensor.getSensorValue());

        Log.i("get view", mSensor.toString());

        return view;
    }

  }
}

Thanks, I have no clue of what could it be... I can only think in the nested layout and some trouble with getting the right parent in the getView.

Hope you can help me :)

UPDATE: this is the logcat, printing the getView and the onItemClick methods:

06-30 10:12:02.309 16450-16450/and.greenggers.smartdoo I/get view:
Sensor{SensorId='947', SensorDataId='1', SensorDataName='DooSmart - EM01',
UserSensorMac='5c:cf:7f:81:a2:28', SensorValue='1593294', 
UserSensorDate='2016-06-30T15:11:22'}

06-30 10:12:02.312 16450-16450/and.greenggers.smartdoo I/get view:
Sensor{SensorId='1', SensorDataId='1', SensorDataName='DooSmart - EM01', 
UserSensorMac='65162132165549516123', SensorValue='123', 
UserSensorDate='2016-06-29T23:11:34'}

06-30 10:12:10.854 16450-16450/and.greenggers.smartdoo I/click item: 
Sensor{SensorId='1', SensorDataId='1', SensorDataName='DooSmart - EM01', 
UserSensorMac='65162132165549516123', SensorValue='123', 
UserSensorDate='2016-06-29T23:11:34'}

SOLVED thanks to @MikeM I was missing the xmlns:app="http://schemas.android.com/apk/res-auto" and app:layout_behavior="@string/appbar_scrolling_view_behavior" in the layout content_get_my_sensors.xml... So fixed it is like this:

<?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"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior">

<ListView
    android:id="@+id/sensorsLV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:clickable="true"
    android:divider="@color/white" />

</RelativeLayout>
  • Change the ``'s `layout_height` to `match_parent`. – Mike M. Jun 30 '16 at 04:16
  • @Mike M. it didn't work :( – CarlosNavas91 Jun 30 '16 at 14:13
  • Are you sure it's not that you just can't see the other list items? It looks like the first one will overlap the `Toolbar`, so make sure the text color isn't just blending into the rest of the background. Can you click on the `ListView`? Are you sure you're not somehow ending up with `Sensor`s with empty values? – Mike M. Jun 30 '16 at 14:39
  • Hi @MikeM. I added the logcat, I can click the item but and its detecting the las item, the one that is showing – CarlosNavas91 Jun 30 '16 at 15:16
  • Oh, OK, you've only got two items. You can't see the first 'cause of the `AppBarLayout`. Look at [this post](http://stackoverflow.com/questions/32956071/add-views-below-toolbar-in-coordinatorlayout). You need to put the attribute shown in the accepted answer on the `RelativeLayout` that holds your `ListView`. And make sure to add `xmlns:app="http://schemas.android.com/apk/res-auto"`, too. – Mike M. Jun 30 '16 at 15:35
  • 1
    No way! lol, thanks a lot @MikeM. It's fixed :) – CarlosNavas91 Jun 30 '16 at 15:49
  • No problem. If you can, please mark that as helpful, so your question is marked as solved. I'm not sure if you still can, though, since you've edited it. Cheers! – Mike M. Jun 30 '16 at 15:51

1 Answers1

1

Change Your Layout Like this..

get_my_sensors_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity"
    android:elevation="7dp">

    <include
        layout="@layout/app_bar_get_sensors"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
       />

</android.support.v4.widget.DrawerLayout>

Only Change in your Drawer Layout Like this .it's Work fine...

Arjun saini
  • 4,223
  • 3
  • 23
  • 51