来自SQLite的ListView,每行包含EditText和CheckBox

时间:2012-06-07 08:00:39

标签: android listview row

我有一个ListView,其中包含来自SQLite的数据,它可以正常工作。现在我想在此列表中添加更多内容。我希望每行添加EditText和CheckBox。像这样:


item1 CheckBox

的EditText


item2 CheckBox

的EditText


这是我的列表类:

public class SeleccionarRelacionPregResp extends Activity implements, OnItemClickListener {

private ListView listaTodo;

private ListAdapter uGraduateListAdapter;
private String bundledCodigoPuntoDeControl;

private ArrayList<UndergraduateDetailsPojo> pojoArrayList;
protected DokesimApplication app;
String codigocaja;
Button btnPruebasGuardar;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.seleccionartodo);

    listaTodo = (ListView) findViewById(R.id.ListaTodo);
    listaTodo.setOnItemClickListener(this);

    pojoArrayList = new ArrayList<UndergraduateDetailsPojo>();

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

    listaTodo.setAdapter(uGraduateListAdapter);

    PuntosDeControl pControlSeleccionado = new PuntosDeControl();
    app = (DokesimApplication) getApplicationContext();
    pControlSeleccionado = app.getpuntocontrol();   

    codigocaja = pControlSeleccionado.codigocaja;


}


public List<String> populateList() {

    List<String> uGraduateNamesList = new ArrayList<String>();

    AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);

    SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

    Cursor cursor = sqliteDatabase.query("RelacionPregResp", null,
            " relprcodigocaja = '" + codigocaja + "'",
            null, null, null, null);

    startManagingCursor(cursor);

    while (cursor.moveToNext()) {

        String numeropregunta = cursor.getString(cursor
                .getColumnIndex(AndroidOpenDbHelper.numeropregunta));
        String relprcodigocaja = cursor.getString(cursor
                .getColumnIndex(AndroidOpenDbHelper.relprcodigocaja));
        String codigopregunta = cursor.getString(cursor
                .getColumnIndex(AndroidOpenDbHelper.codigopregunta));
        String pregunta = cursor.getString(cursor
                .getColumnIndex(AndroidOpenDbHelper.pregunta));
        String codigorespuesta = cursor.getString(cursor
                .getColumnIndex(AndroidOpenDbHelper.codigorespuesta));
        String respuesta = cursor.getString(cursor
                .getColumnIndex(AndroidOpenDbHelper.respuesta));
        String valor = cursor.getString(cursor
                .getColumnIndex(AndroidOpenDbHelper.valor));
        String tipodeguia = cursor.getString(cursor
                .getColumnIndex(AndroidOpenDbHelper.tipodeguia));

        UndergraduateDetailsPojo ugPojoClass = new UndergraduateDetailsPojo();

        ugPojoClass.setNumeroPregunta(numeropregunta);
        ugPojoClass.setRelPrCodigoCaja(relprcodigocaja);
        ugPojoClass.setCodigoPregunta(codigopregunta);
        ugPojoClass.setPregunta(pregunta);
        ugPojoClass.setCodigoRespuesta(codigorespuesta);
        ugPojoClass.setRespuesta(respuesta);
        ugPojoClass.setValor(valor);
        ugPojoClass.setTipoDeGuia(tipodeguia);

        pojoArrayList.add(ugPojoClass);

        uGraduateNamesList.add(pregunta);


    }

    sqliteDatabase.close();

    return uGraduateNamesList;
}

@Override
protected void onResume() {
    super.onResume();
    uGraduateListAdapter = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, populateList());
    listaTodo.setAdapter(uGraduateListAdapter);
}

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {


}
}

我需要一些建议或示例,我该怎么做。

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

您最需要的是Custom SimpleCursorAdapter。

我粘贴的示例程序在每个列表行中都有两个TextView。

您必须根据您的要求进行修改。

package org.sample;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyAdapter extends SimpleCursorAdapter
{
    private Context context;
    private Cursor cursor;
    private int layout;

    public MyAdapter(Context context, int layout, Cursor cursor, String[] from,
            int[] to, int flags)
    {
        super(context, layout, cursor, from, to, flags);
        this.context = context;
        this.cursor = cursor;
        this.layout = layout;
    }

    @Override
    public int getCount()
    {
        if (cursor != null)
            return cursor.getCount();
        else
            return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup)
    {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (view == null)
        {
            view = inflater.inflate(layout, null);
        }
        cursor.moveToPosition(position);
        String title = cursor.getString(cursor
                .getColumnIndex(DBHelper.BOOK_TITLE));
        String author = cursor.getString(cursor
                .getColumnIndex(DBHelper.BOOK_AUTHOR));


        TextView titleTxt = (TextView) view.findViewById(R.id.bTitle);
        TextView authorTxt = (TextView) view.findViewById(R.id.bAuthor);

        titleTxt.setText(title);
        authorTxt.setText(author);

        return view;
    }


}

准备好将光标放在MainActivity中后,您可以编写以下代码。

myAdapter = new MyAdapter(context, R.layout.row, cursor, new String[]{ DBHelper.BOOK_TITLE, DBHelper.BOOK_AUTHOR }, new int[]{ R.id.bTitle, R.id.bAuthor }, 0);
listView.setAdapter(myAdapter);

答案 1 :(得分:1)

您必须创建自己的类,从适配器扩展您需要的类,例如SimpleCursorAdapter,然后特定于XML文件,哪些元素将包含在您的ListView中并覆盖{ {1}}控制getView()中每个项目的方法。 建议创建和使用设计模式ListView,它表示包含每行子窗口小部件的任意对象,然后您将完全控制项目。

所以让基本的例子:

Holder

的声明和初始化
ListView

this.contactList = new ListView(this); this.contactList = (ListView) findViewById(R.id.contactList); this.contactList.setAdapter(new ContactsAdapter()); 是您自己创建的适配器。

ContactsAdapter

rowsWrapper被提到设计模式public ContactsAdapter() { super(getApplicationContext(), R.layout.listview, cursor, new String[] {"name", "email", "phone"}, new int[] {R.id.name, R.id.email}); } @Override public View getView(int position, View convertView, ViewGroup parent) { rowsWrapper = null; LayoutInflater inflater = getLayoutInflater(); if (convertView == null) { convertView = inflater.inflate(R.layout.listview, null, false); rowsWrapper = new ListWidgetWrapper(convertView); convertView.setTag(rowsWrapper); } else { rowsWrapper = (ListWidgetWrapper) convertView.getTag(); } Cursor tempC = cursor; tempC.moveToPosition(position); setActivitySettings(); rowsWrapper.getNameColumn().setText(tempC.getString(1)); rowsWrapper.getEmailColumn().setText(tempC.getString(2)); tempC = null; return convertView; } }

Holder

所以我在这里有public class ListWidgetWrapper { private View inView; private TextView nameColumn = null, emailColumn = null, phoneColumn = null; public ListWidgetWrapper(View inView) { this.inView = inView; } public int getNameId() { return inView.findViewById(R.id.name).getId(); } public int getEmailId() { return inView.findViewById(R.id.email).getId(); } public TextView getNameColumn() { if (this.nameColumn == null) { this.nameColumn = (TextView) inView.findViewById(R.id.name); } return this.nameColumn; } public TextView getEmailColumn() { if (this.emailColumn == null) { this.emailColumn = (TextView) inView.findViewById(R.id.email); } return this.emailColumn; } } 元素,你有TextView中想要的小部件。 这需要编写更多的实现,但是当你不使用例如设计模式ListView时,它会更快,更少的能量消耗,因为没有它,Holder中的行将不会重复创建在ListView然后回去。

有关SimpleCursorAdapter

的更多信息