0

我有一个来自 SQLite 的数据的 ListView 可以正常工作。现在我想在这个列表中添加更多内容。我想向每一行添加 EditText 和 CheckBox。像这样的东西:


item1 复选框

编辑文本


item2 复选框

编辑文本


这是我的列表类:

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) {


}
}

我需要一些建议或示例来说明我该怎么做。

谢谢你的帮助。

4

2 回答 2

1

您基本上需要的是自定义 SimpleCursorAdapter。

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

您将不得不根据您的要求对其进行修改。

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);
于 2012-06-07T08:06:53.437 回答
1

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

所以让基本的例子:

声明和初始化ListView

this.contactList = new ListView(this);
this.contactList = (ListView) findViewById(R.id.contactList);
this.contactList.setAdapter(new ContactsAdapter());

ContactsAdapter是您自己创建的适配器。

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; 
        }
    }

rowsWrapper 是提到设计模式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不存在时重复创建,View然后再返回。

更多关于SimpleCursorAdapter

于 2012-06-07T08:07:01.887 回答