Unable to creae List of ListView.

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Unable to creae List of ListView.

Postby pan_mani » Mon Apr 20, 2009 10:19 am

Hi All,

I am new to android application.So my question may seems small to you guys. But for me it is really serious and big.
I have a list of items and on item selection it is showing another list of items. It goes till 4 level in same fashion.
With following code implementation I am able to achieve this but whenever back key is pressed instead of coming to previous listview it is killing my application.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <span style="font-weight: bold">MGUI.jav</span>a
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


public class MGUI extends ListActivity {

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] mStrings = new String[]{"A", "B","C"};
this.setListAdapter(new ArrayAdapter<String>(this,R.layout.mylist, mStrings));
}

protected void onListItemClick(ListView listView, View view, int position, long id)
{
super.onListItemClick(listView, view, position, id);

String[] mStrings = new String[]{"A1", "A2","A3"};
this.setListAdapter(new ArrayAdapter<String>(this,R.layout.mylist, mStrings));
}
}

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  My mylist.XML
Parsed in 0.000 seconds, using GeSHi 1.0.8.4



<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"
/>


This behaviour seems ok with button event as I am drawing all list on same view.

So,thinking in this way I have changed my code .

Now I have taken two classes.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MyList.java
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


public class MyList extends ListActivity {

protected static final int SUB_ACTIVTY_REQUEST_CODE = 99;
private String[] mStrings ={"A", "B", "C"};

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.list1);

this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings ));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
selectedItem = position;

Intent intent = new Intent(MyList.this, List2.class);
super.startActivityForResult(intent, SUB_ACTIVTY_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
}
}



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. List2.java
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


public class List2 extends Activity {

private String[] mStrings ={"A1", "A2", "A3"};
@Override
protected void onCreate(Bundle icicle) {

super.onCreate(icicle);
this.setContentView(R.layout.mlist);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings ));

}
}


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. list1.xml
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dip"
android:text="@string/list_title"/>
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. mlist.xml
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" />

In this case I am able to load 1st view,but once a item is being selected, application get crashed.

I can understand I am missing some basics.Please help me.

Thanks,
mani
pan_mani
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Apr 20, 2009 9:26 am

Top

Postby Sowmya » Tue Apr 21, 2009 11:34 am

Hi,
You have to add ListView to the Second list's XML(mlist.xml) too

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8" ?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5. android:orientation="vertical" android:layout_width="fill_parent"
  6.  
  7. android:layout_height="fill_parent" >
  8.  
  9. <ListView
  10.  
  11. android:id="@id/android:list"
  12.  
  13. android:layout_width="fill_parent"
  14.  
  15. android:layout_height="fill_parent"
  16.  
  17. android:layout_weight="1"
  18.  
  19. android:drawSelectorOnTop="false"/>
  20.  
  21. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4
Sowmya
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Fri Mar 06, 2009 7:49 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 10 guests