How to Implement a Listview Without Listactivity? (Use Only Activity)

ListView without ListActivity

I actually have an app with a listview below some TextViews and above two buttons, let me grab my code!

Here's my activity with listview below the textviews and above the buttons (with quite a bit removed for brevity):

public class BNYDirectoryResults extends Activity{

public static String[] stuff;
ListView list;
BNYAdapter adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resultscreen);

TextView headDisplay = (TextView)findViewById(R.id.results);
TextView headCount = (TextView)findViewById(R.id.resultsTotal);
TextView headPages = (TextView)findViewById(R.id.pages);

//Set up the results list, see BNYAdapter
list = (ListView)findViewById(R.id.list);
adapter = new BNYAdapter (this, BNYDirectory.ReturnResults);
list.setAdapter(adapter);

//Sets up header information (pages, total results)
//Just some stuff to change the TextViews

//Passes EmployeeID and Phone Number to AND opens the details page
list.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
String EID = BNYDirectory.ReturnResults[position][0];
String phoneNumber = BNYDirectory.ReturnResults[position][2];

BNYDirectoryTransaction.doDetails(EID, phoneNumber);
Intent i = new Intent(view.getContext(), BNYDirectoryDetails.class);
startActivity(i);

}
});
}
}

Here's the XML file for it:


    <TextView
android:id="@+id/results"
android:text="Name"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
<TextView
android:id="@+id/resultsTotal"
android:text="Phone"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
<TextView
android:id="@+id/pages"
android:text="AIM"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:textStyle="bold"/>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/name"
android:text="Name"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/phone"
android:text="Phone"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/aim"
android:text="AIM"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="@+id/dept"
android:text="Dept"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/prevButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Previous"
android:layout_weight="1"/>
<Button
android:id="@+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
android:layout_weight="1"/>

</LinearLayout>
</LinearLayout>

and here's the custom adapter for the list:

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class BNYAdapter extends BaseAdapter {

private Activity activity;
private String[][] results;
private static LayoutInflater inflater=null;

public BNYAdapter(Activity a, String[][]info) {
activity = a;
results = info;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
return results.length;
}

public Object getItem(int position) {
return position;
}

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

public static class ViewHolder{
public TextView name;
public TextView phone;
public TextView aim;
public TextView dept;
}

public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.name=(TextView)vi.findViewById(R.id.nameItem);
holder.phone=(TextView)vi.findViewById(R.id.phoneItem);
holder.aim=(TextView)vi.findViewById(R.id.aimItem);
holder.dept=(TextView)vi.findViewById(R.id.deptItem);
vi.setTag(holder);
}
else
holder=(ViewHolder)vi.getTag();

holder.name.setText(BNYDirectory.ReturnResults[position][1]);
holder.phone.setText(BNYDirectory.ReturnResults[position][2]);
holder.aim.setText(BNYDirectory.ReturnResults[position][3]);
holder.dept.setText(BNYDirectory.ReturnResults[position][4]);
return vi;
}

}

and all together that makes a page like Look, a list with other stuff around it!

How to Create a Custom Listview without extending List Activity?

You are not bound to use ListActivity in order to have a ListView in your layout. All the samples using ListActivity are there because people are lazy ;)

So just use a regular Activity and then you simply have to add the ListView to the layout you are setting as content view. This could look something like this:

<?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/listView"
android:layout_width="match_parent"
android:layout_height="0"
android:layout_weight="1" />
<Button
android:id="@+id/somethingButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Do something" />
<Button
android:id="@+id/elseButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Do something else" />
</LinearLayout>

Then in like any other Activity in your OnCreate method you do:

SetContentView(Resource.Layout.myLayout); // myLayout.axml

var listView = FindViewById<ListView>(Resource.Id.listView);
listView.Adapter = new MyAdapter(context, itemsSource);

var somethingButton = FindViewById<Button>(Resource.Id.somethingButton);
var elseButton = FindViewById<Button>(Resource.Id.elseButton);

... more code ...

ListView is just a layout just like any other, so there is nothing magic about it. MyAdapter in this sample is ofcourse something you implement, which takes your data and presents it in the desired image and text layout.

For more information about ListView and Adapter please refer to the excellent documentation which Xamarin provides.

Is it possible to use a ListView without ListActivity in Mono Android?

I've just found solution. I had error in this line:

view.Adapter = new ArrayAdapter<string>(this, Resource.Layout.CreateUser, somadata);

This topic helps me a lot.

Here is a further list of layouts that
you can use.

Sources of predefined layouts are here.

So, there is two options how to fix error and make ListView work without ListActivity in Mono Android.

First:

Just copy/paste layout of standard resource named simple_list_item_1:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>

Second (I'm sure it's a best way):

Just use standard resource. In Mono Android it named Android.Resource.Layout.SimpleListItem1
So my updated version of OnCreate which works perfect:

    protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.CreateUser);

JavaList<string> somadata = new JavaList<string> { "111", "222", "333" };

ListView view = FindViewById<ListView>(Resource.Id.list);
view.Adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, somadata);
}

android listview don't apprear when implement it with Activity, not listActivity

Sample Image

check the attached image. This is the screen shot of the samsung galxy tab with your code only and its working.

Below is the complete code i tested.

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class TestSamplesActivity extends Activity {
String foods[] = { "Betza", "Betza1", "Betza2", "Betza3", "Betza4" };
ListView lvFoods;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.foodsmenu);
initialize();
}

private void initialize() {
// TODO Auto-generated method stub
lvFoods = (ListView) findViewById(R.id.lvFoods);
lvFoods.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, foods));

}
}

foodsmenu.xml.

  <?xml version="1.0" encoding="utf-8"?>

<ListView
android:id="@+id/lvFoods"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#333333"
android:fillViewport="true" >
</ListView>

<LinearLayout
android:layout_width="40dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff"
android:orientation="vertical" >

<TextView
android:id="@+id/tvA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="A"
android:textColor="#025f7c"
android:textSize="13dp" />

<TextView
android:id="@+id/tvB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="B"
android:textColor="#025f7c"
android:textSize="13dp" />

<TextView
android:id="@+id/tvC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="C"
android:textColor="#025f7c"
android:textSize="13dp" />

<TextView
android:id="@+id/tvD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="D"
android:textColor="#025f7c"
android:textSize="13dp" />

</LinearLayout>

</LinearLayout>

manifest

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".TestSamplesActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

compare with your code if anything is missed.

Display a menu in listview without using listactivity

use android.R.layout.simple_list_item_1 instead of R.id.mylist. This parameter is reserved for the id of layout that will be used to inflate each list item, not for id of a list view

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

alternatively you could use your custom layout id, but it must cosist solely of TextView with id attribute set to android:id="@android:id/text"



Related Topics



Leave a reply



Submit