5

I started developing for Android yesterday, so I am a complete newbie. I am using ADT bundle that works with Eclipse. What I want to do is I have got an array of strings. I would like to display them in a scroll supported view. I found ListView can do that but all tutorials that I found are extremely complex and explaining nested views.

I tried using ListView but I cannot even see the dummy view. Here is what I have tried: activity_main.xml:

<ListView
        android:id="@+id/wifiScanResList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wifiScanButton" >
</ListView>

in MainActivity Class:

private ListView wifiListView;

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

    getMenuInflater().inflate(R.menu.main, menu);
    wifiListView = (ListView) findViewById(R.id.wifiScanResList);
    wifiListView.setEnabled(true);

    return true;
}

I have got two questions in regards to my problem now.

First one is, am I using the right view for my needs? If not what should I use?

Second one is, what is the reason that dummy view does not appear? Also how can I simply add strings into ListView?

1
  • why would there be a dummy list? Commented Dec 23, 2013 at 19:51

2 Answers 2

12

first you need to create a ArrayList that would store all the strings.

String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
    "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
    "Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
    "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2",
    "Android", "iPhone", "WindowsMobile" };

final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < values.length; ++i) {
  list.add(values[i]);
}

then you would need to create an adapter from this arraylist

ArrayAdapter adapter = new ArrayAdapter<String>(this,  
      android.R.layout.simple_list_item_1, 
      list);

then you would need to set this adapter on the listview

listview.setAdapter(adapter);

please see this link for a simple list view implementation:

http://androidexample.com/Create_A_Simple_Listview_-_Android_Example/index.php?view=article_discription&aid=65&aaid=90

Sign up to request clarification or add additional context in comments.

4 Comments

Err why are you using a CursorAdapter when you clearly want an ArrayAdapter?
The answer above should help. You did not use any adapter, so the list would not be displayed. Use this link as an example, its very simple and it is the same thing you asked. Simple listview with strings. www.ezzylearning.com/tutorial.aspx?tid=1659127
I have only changed ArrayAdapter initialisation to: ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_main, list); Now getting a runtime error as ArrayAdapter requires the resource ID to be a TextView.
@SarpKaya see the edit, you can use android.R.layout.simple_list_item_1
4

Here is my simple way of displaying a list.

1 ) Create a class called MySimpleArrayAdapter

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final ArrayList<String> values;

    public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
        super(context, R.layout.rowlayout, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public void notifyDataSetChanged() {
        // TODO Auto-generated method stub
        super.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = null;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.rowlayout, parent, false);

        // Displaying a textview 
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        textView.setText(values.get(position));


        return rowView;
    }

2) Create an xml file in the layout called rowlayout.xml. We have textview because we want to display items but you could display an item with an image. Use ImageView tag

<?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="wrap_content" >

    <TextView
        android:id="@+id/label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="30sp" >
    </TextView>


</LinearLayout>

3) Back to your main activity xml file (activity_main). We have your wifiscan list

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"> 
  <ListView
        android:id="@+id/wifiScanResList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wifiScanButton" >

 </ListView>
</LinearLayout>

4) Lastly, in your main activity we set the adapter to your list

public class MainActivity extends Activity {

  // The adapter that we gonna use
  MySimpleArrayAdapter adapter;

  // List of wifi results
  ArrayList<String> wifi_results= new ArrayList<String>();

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

        adapter = new MySimpleArrayAdapter(this, wifi_results);
        setListAdapter(adapter);

        getListView().setOnItemClickListener(new OnItemClickListener() {
        @Override
     public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

          // DO something if the user clicked on the item
         }
           });
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.