I am trying to use list views and I have managed to create one and it works perfectly. However it creates itself in a new screen. I’d like to know how I can use an already created list view in the main menu (defined by main.xml).
My Java Code:
public class Option2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent startNewActivityOpen = new Intent(Option2Activity.this, ListViewActivity.class);
startActivityForResult(startNewActivityOpen, 0);
}
}
public class ListViewActivity extends ListActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
ListView lv = (ListView)findViewById(R.id.listview_main);
// Currently unused...
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.list_titles);
TypedArray icons = res.obtainTypedArray(R.array.list_icons);
setListAdapter(new Adapter(ctx, R.layout.list_item, options, icons));
//I tried using lv.setAdapter(new Adapter(ctx, R.layout.list_item, options, icons));
//but it didn't work. 'setListAdapter' is creating a new listview in another screen
}
}
public class Adapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public Adapter(Context ctx, int viewResourceId, String[] strings, TypedArray icons)
{
super(ctx, viewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = viewResourceId;
}
@Override
public int getCount()
{
return mStrings.length;
}
@Override
public String getItem(int position)
{
return mStrings[position];
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(mViewResourceId, null);
Button ib = (Button)convertView.findViewById(R.id.list_button);
ib.setText(mStrings[position]);
ib.setCompoundDrawables((mIcons.getDrawable(position)), null, null, null);
//this doesn't work either, it is supposed to set an image in the button
//if anyone knows how to help me I'd appreciate but it isn't the main issue
TextView tv = (TextView)convertView.findViewById(R.id.list_text);
tv.setText(mStrings[position]);
return convertView;
}
}
THE XML
Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:src="@drawable/newsimage" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:maxHeight="50dp"
android:maxWidth="50dp"
android:src="@drawable/newsimage" />
<TextView
android:id="@+id/textView1"
android:layout_marginTop="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:text="Option 2 Menu"
android:textSize="25dp" />
</RelativeLayout>
<ListView
android:id="@+id/listview_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/newspaper"
android:scaleType="fitXY"
/>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/list_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:padding="6dp"
/>
<Button
android:id="@+id/list_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I think this is all the relevant code. Thank you in advance. I also would like to include that a piece of this code was referenced from other developers which I searched in forums.
FIXED
public class Option2Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.listview_main);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] options = res.getStringArray(R.array.list_titles);
TypedArray icons = res.obtainTypedArray(R.array.list_icons);
lv.setAdapter(new Adapter(ctx, R.layout.list_item, options, icons));
}
}
Not exactly like the latest user’s reply. But it worked. There is no need for the ListViewActivity.
Thank you forum.
Remove
ListViewActivityactivity just use the activity I given below,Try this,