Uso de ListViews en LinearLayouts en lugar de pantalla completa

I am new to android and I want the add subject button to be displayed only once and not on each item. How can i do this using SimpleCursorAdapter

Regards Waneya IqbalI want the add subject button to be displayed only once and not on each item

The code is as follows( see the showSubjectOnList() method ):

public class PopulatingSubject extends ListActivity {

public static String subjectName; private SoftCopyDatabase subjects;

private static int[] subTO = { R.id.subject };
private static String[] subFROM = { SUBJECT };


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    subjects = new SoftCopyDatabase(this);

}

@Override
public void onStart() {
    super.onStart();

    try {

        showSubjectsOnList();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public void onStop() {
    super.onStop();
    if (subjects.getReadableDatabase().isOpen()) {
        subjects.close();
    }
}

public void onDestroy(){
    super.onDestroy();
    if (subjects.getReadableDatabase().isOpen()) {
        subjects.close();
    }
}

private void showSubjectsOnList() {

    String sql = "SELECT " + _ID + "," + SUBJECT + " FROM " + TABLE_NAME
            + " GROUP BY " + SUBJECT + ";";
    Cursor cursor = subjects.getReadableDatabase().rawQuery(sql, null);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            R.layout.item_subject, cursor, subFROM, subTO);
    setListAdapter(adapter);
    startManagingCursor(cursor);

}
@Override
protected void onListItemClick(ListView listView, View view, int position,
        long id) {
    super.onListItemClick(listView, view, position, id);
    subjectName=getSubjectName(Long.toString(id));
    startActivity(new Intent(this, PopulatingLectures.class));
}



private String getSubjectName(String ID) {

    Cursor cursor = subjects.getReadableDatabase().query(TABLE_NAME,
            new String[] { SUBJECT }, "_ID=?", new String[] { ID }, null,
            null, null);
    cursor.moveToFirst();
    int subjectIndex = cursor.getColumnIndex(SUBJECT);
    String subjectName = cursor.getString(subjectIndex);
    cursor.close();
    return subjectName;
}   

}

The layout is as follows :

The picture of layout is:

enter image description here

preguntado el 27 de agosto de 11 a las 15:08

Can you give an example of the layout you're trying to accomplish? -

Actually what i want to achieve is to add a button in the listActivity but not on the List. If I add a button in my Activity which extends ListActivity, then this button is displayed in every item of the List because the SimpleCursorAdapter is populating the list using the ID of the XML. -

Post some code, a picture, exceptions.... -

@pankaj The problem is that I have to add a button in my Activity which extends ListActivity. But if I add the button, the button is displayed in every item of the List however I wanted the button to be shown only once. To Avoid this i tried to put listView in a separate LinearLayout, however a linearLayout can not be populated through a SimpleCursorAdapter ( The constructor method does not support LinearLayouts, it supports XML Layouts ). Now how can I add the button ? -

@Jack private void showSubjectsOnList() { String sql = "SELECT " + _ID + "," + SUBJECT + " FROM " + TABLE_NAME + " GROUP BY " + SUBJECT + ";"; Cursor cursor = subjects.getReadableDatabase().rawQuery(sql, null); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item_subject, cursor, subFROM, subTO); setListAdapter(adapter); startManagingCursor(cursor); } The R.layout.item used in SimpleCursorAdapter is an XML layout within which i have a linearLayout. On this LinearLayout I want to populate the ListView, otherwise button is displayed in all items. How? -

1 Respuestas

You have to basically create a Layout yourself either in code or in XML (XML is recommended). Then create a ListView within that Layout. You can create your button and add it to the same layout. Since you are creating your layout you can position the ListView and the Button in whatever way suits your needs.

You then create a reference to your ListView so that you can set an adapter to it.

Ejemplo:

Layout in XML file named "my_layout.xml":

<LinearLayout 
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Sign In" />

   <ListView
       android:id="@+id/my_listview"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" />

</LinearLayout>

Luego en código:

public class MyActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);
        ListView listView = (ListView) findViewById(R.id.my_listview);
        listView.setAdapter(/*Put your adapter reference here*/);
        listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // Handle list selections here
        }
    });
    }
}

Obviously there's no data but you can do that but setting your adapter where I have indicated above.

Respondido 27 ago 11, 20:08

@COdeAttack I tried to do that and created a layout within my_laout.xml with an id listViewLinearLayout. I then used this id (listViewLinearLayout) in simple cursor adapter as follows SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.id.listViewLinearLayout, cursor, subFROM, subTO); But the application crashes, simply because SimpleCursorAdapter requires R.layout.my_layout and not the id of a linearLayout within my_layout. I also tried to use the id of listView (the id is listView1) but this also did not work. - Waneya Iqbal

Ah ok so you've already had a go, that's good. If you can paste some relevant code/xml in your question above then that'll help us to find the problem or make a suggestion. - C0deAttack

How can i post a picture here? :s - Waneya Iqbal

Edit your original question, in the toolbar there'll be an icon to upload an image. If you're not sure which icon it is, hover over each one in turn and check the tool tip. - C0deAttack

Having re-read your comment; I think your problem might be the fact that you're using the ListView ID. You should be using the resource ID for the layout used for each row in that ListView. - C0deAttack

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.