THGTechGuy
THGTechGuy

Reputation: 59

Android ListView only Showing One Item

Im trying to create a listview containing all currently installed User Apps. The problem im facing is that the ListView is Only showing one entry and i cant figure out what im doing wrong.

I use the same code elsewhere (With a JSON Result) and it works fine.

Here is my Activity 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"
android:background="@color/colorPrimary"
tools:context=".AppsList">

<ListView
    android:id="@+id/applistview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_alignParentTop="true" />

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Close" />

</RelativeLayout>

My 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:background="@color/colorPrimary" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/colorPrimary"
    android:layout_centerHorizontal="true"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="6" >

        <ImageView
            android:id="@+id/app_icon"
            android:layout_width="45dp"
            android:layout_height="45dp"
            android:layout_weight="1"
            android:src="@drawable/frissonhead" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:orientation="vertical"
            android:weightSum="2">

            <TextView
                android:id="@+id/app_name"
                android:layout_width="258dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:text="Soon To Clear"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/colorAccent"
                android:textSize="20dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/app_package"
                android:layout_width="258dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:text="Soon To Clear"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="@color/colorAccent"
                android:textSize="12dp" />

        </LinearLayout>


        <CheckBox
            android:id="@+id/app_check"
            android:layout_width="25dp"
            android:layout_height="match_parent"
            android:layout_weight="1.02"
            android:checked="false"
            android:gravity="center" />

    </LinearLayout>

</LinearLayout>

</RelativeLayout>

And my Activity Java

package com.fixmypcscotland.childsafe;

import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AppsList extends AppCompatActivity {

SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
ListView list;

ArrayList<HashMap<String, String>> oslist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    setContentView(R.layout.activity_apps_list);
    sharedPreferences = getSharedPreferences("CHILD_MODE", MODE_PRIVATE);
    editor = sharedPreferences.edit();
    list = (ListView)findViewById(R.id.applistview);
    oslist = new ArrayList<HashMap<String, String>>();
    final PackageManager pm = getPackageManager();
    //get a list of installed apps.
    List<ApplicationInfo> packages = 
 pm.getInstalledApplications(PackageManager.GET_META_DATA);

    HashMap<String, String> map = new HashMap<String, String>();

    for (ApplicationInfo packageInfo : packages) {
        String appkg = packageInfo.packageName;
        String apn = pm.getApplicationLabel(packageInfo).toString();
        map.put("APPPKG", appkg);
        map.put("APPNAME", apn);
    }
    oslist.add(map);


    ListAdapter adapter = new SimpleAdapter(AppsList.this, oslist, R.layout.activity_apps_list_item, new String[]{"APPPKG", "APPNAME"}, new int[]{R.id.app_package, R.id.app_name});

    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            editor.putString("bid", oslist.get(+position).get("APPPKG"));
            editor.commit();
Toast.makeText(getApplicationContext(), oslist.get(+position).get("APPPKG"), Toast.LENGTH_LONG).show();

            }
        });
    }
}

Any help is appreciated. Its driving me crazy.

Upvotes: 1

Views: 1731

Answers (3)

shahid17june
shahid17june

Reputation: 1577

Change your List Item with this one.

    <?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="7dp"
    android:background="@color/colorPrimary" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/colorPrimary"
        android:layout_centerHorizontal="true"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="6" >

            <ImageView
                android:id="@+id/app_icon"
                android:layout_width="45dp"
                android:layout_height="45dp"
                android:layout_weight="1"
                android:src="@drawable/frissonhead" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="4"
                android:orientation="vertical"
                android:weightSum="2">

                <TextView
                    android:id="@+id/app_name"
                    android:layout_width="258dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:text="Soon To Clear"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/colorAccent"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/app_package"
                    android:layout_width="258dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:gravity="center_vertical"
                    android:text="Soon To Clear"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="@color/colorAccent"
                    android:textSize="12dp" />

            </LinearLayout>


            <CheckBox
                android:id="@+id/app_check"
                android:layout_width="25dp"
                android:layout_height="match_parent"
                android:layout_weight="1.02"
                android:checked="false"
                android:gravity="center" />

        </LinearLayout>

    </LinearLayout>

</RelativeLayout>

And Learn about Adapter click here you may get hint from here.

Upvotes: 1

Luiz Fernando Salvaterra
Luiz Fernando Salvaterra

Reputation: 4182

You are passing the wrong data to your adapter, so you have to change this line:

ListAdapter adapter = new SimpleAdapter(AppsList.this, oslist, R.layout.activity_apps_list_item, oslist, new int[]{R.id.app_package, R.id.app_name});

Also, I must to warn you to see the documentation about the SimpleAdapter to pass the right parameters to it:

The first parameter is a Context where the View associated with this SimpleAdapter is running

The second parameter is a Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to".

The third is a list of column names that will be added to the Map associated with each item.

And the last one is The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

And then, fix your for loop:

for (ApplicationInfo packageInfo : packages) {
    String appkg = packageInfo.packageName;
    String apn = pm.getApplicationLabel(packageInfo).toString();
    map.put("APPPKG", appkg);
    map.put("APPNAME", apn);

oslist.add(map);
}

Upvotes: 0

for (ApplicationInfo packageInfo : packages) {
    String appkg = packageInfo.packageName;
    String apn = pm.getApplicationLabel(packageInfo).toString();
    map.put("APPPKG", appkg);
    map.put("APPNAME", apn);

**oslist.add(map);**
}

Upvotes: 0

Related Questions