2012-06-07 61 views
0

我有一個來自SQLite的數據正常工作的ListView。現在我想添加更多的東西到這個列表中。對於每一行我想添加EditText和CheckBox。事情是這樣的:來自SQLite的ListView每個行都帶有EditText和CheckBox


物品1複選框

的EditText


ITEM2複選框

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


} 
} 

我需要一些建議或例子,我該怎麼做。

感謝您的任何幫助。

回答

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); 
1

你必須創建自己的類,從適配器,你需要的東西,例如SimpleCursorAdapter,然後用XML文件中的特定,其內容將納入到你的ListView並覆蓋getView()方法controling在ListView每個項目的延伸。 建議創建並使用設計模式Holder,它表示任意對象,用於存放每行的子部件,然後您將完全控制項目。

因此,讓基本的例子:

宣言和ListView

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

Init這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 elemets,你這裏有您想要在ListView部件。 這需要編寫更多的實現,但它更快,當你不使用例如設計模式Holder時更少的能量消耗,因爲如果沒有它,ListView中的行將不會在View中重複創建,然後返回。

更多關於SimpleCursorAdapter

相關問題