-2

I'm trying to make a ListView for my Android project but unfortunately when I import these libraries:

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

It says these are "Unused import statement"! So I cant use "setListAdapter()" functions or other functions like that.

My code is

    package irstudents.amirsardari.www.ie_v10;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.widget.DrawerLayout;

import android.widget.ArrayAdapter; //It alerts that are Unused import Satatement
import android.widget.TextView; //It alerts that are Unused import 
import android.app.ListActivity; //It alerts that are Unused import 
import android.os.Bundle; //It alerts that are Unused import 

public class MainActivity extends AppCompatActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));

    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.my_profile);
                break;
            case 2:
                mTitle = getString(R.string.About);
                break;
            case 3:
                mTitle = getString(R.string.action_settings);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            ((MainActivity) activity).onSectionAttached(
                    getArguments().getInt(ARG_SECTION_NUMBER));
        }
    }

}

1 Answers1

3

CustomAdapter class :-

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


public class CustomAdapter extends BaseAdapter
{

    Context context;
    String[] rooms;
    public CustomAdapter(Context c)
    {
        context = c;
        Resources res = c.getResources();
        rooms = res.getStringArray(R.array.images);
    }
    @Override
    public int getCount() {
        return rooms.length;
    }

    @Override
    public Object getItem(int position) {
        return rooms[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view =  inflater.inflate(R.layout.single_row,parent,false);
        TextView txt = (TextView) view.findViewById(R.id.txt_item);
        String temp = rooms[position];
        txt.setText(temp);
        return view;
    }
}

MainActivity class :-

public class MainActivity extends ActionBarActivity
{


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new CustomAdapter(this));
     }

}

Add this to your string.xml :-

<string-array name="images">
        <item>Living Room</item>
        <item>Master Bedroom</item>
        <item>Children Bedroom</item>
        <item>Kitchen1</item>
        <item>Kitchen2</item>
        <item>Gallery</item>
</string-array>

Add this to your activity_main.xml :-

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:dividerHeight="0.1dp"
    android:divider="#666666"
    android:id="@+id/listView" />

single_row.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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:gravity="start"
        android:text="Large Text"
        android:typeface="serif"
        android:id="@+id/txt_item"
        android:padding="3dp"
        android:paddingLeft="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
Parag Kadam
  • 3,620
  • 5
  • 25
  • 51
  • It was ironically, because you should post lines of code with explanation! – Lalit Poptani Aug 10 '15 at 10:04
  • if you read the above comments you would get to know that the OP was trying to import something which he should have actually extended, this shows that the OP is pretty new to android and hence a few lines of explanatory code would not have been enough. – Parag Kadam Aug 10 '15 at 10:10
  • But just posting wall of codes will not make it clear what he is doing, he will only learn copy-paste rather than what is happening in your code – Lalit Poptani Aug 10 '15 at 10:12
  • 1
    When one gets the code which works, then he/she would analyze the code line by line and if the OP still does not understand it, in that case we are here to help. – Parag Kadam Aug 10 '15 at 10:16
  • I don't analyze the code line by line once I get the full code, what you say in that case? ;) – Lalit Poptani Aug 10 '15 at 10:18
  • 1
    Well that's your way of doing it , but many people I know understand the code better then the english explanation. – Parag Kadam Aug 10 '15 at 10:20
  • Always seek for the best, not what you understand, so better is to explain the code that we are posting! – Lalit Poptani Aug 10 '15 at 10:32
  • 1
    Anyway let us agree to disagree ! – Parag Kadam Aug 10 '15 at 10:35