如何从数据库中检索我的数据到复选框列表视图类型?

时间:2014-12-13 07:32:31

标签: android

我试图将数据从数据库检索到复选框列表视图,但我无法这样做。下面是我已用于将数据从数据库检索到列表视图的代码。但我想要的是检索到复选框列表视图类型,以便我可以检查它。我怎么能这样做?

公共类StudentDetailActivity扩展Activity实现OnClickListener,OnItemClickListener {

private ListView uGraduateNamesListView;
private Button addNewUndergraduateButton;


private ListAdapter uGraduateListAdapter;

private ArrayList<UndergraduateDetailsPojo> pojoArrayList;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student_detail);


    uGraduateNamesListView = (ListView) findViewById(R.id.uGraduateListView);
    uGraduateNamesListView.setOnItemClickListener(this);

    addNewUndergraduateButton = (Button) findViewById(R.id.namesListViewAddButton);
    addNewUndergraduateButton.setOnClickListener(this);

    pojoArrayList = new ArrayList<UndergraduateDetailsPojo>();


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

    uGraduateNamesListView.setAdapter(uGraduateListAdapter);

}

@Override
public void onClick(View v) {
    Intent addNewUndergraduateIntent = new Intent(this, AddNewUndergraduateActivity.class);
    startActivity(addNewUndergraduateIntent);
}


public List<String> populateList(){

    List<String> uGraduateNamesList = new ArrayList<String>();
    AndroidOpenDbHelper openHelperClass = new AndroidOpenDbHelper(this);
    SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();
    Cursor cursor = sqliteDatabase.query(AndroidOpenDbHelper.TABLE_NAME_GPA, null, null, null, null, null, null);

    startManagingCursor(cursor);
    while (cursor.moveToNext()) {

        String ugName = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_NAME));
        String ugUniId = cursor.getString(cursor.getColumnIndex(AndroidOpenDbHelper.COLUMN_NAME_UNDERGRADUATE_UNI_ID));
        double ugGpa = cursor.getDouble(cursor.getColumnIndex(AndroidOpenDbHelper.COLLUMN_NAME_UNDERGRADUATE_GPA));

        UndergraduateDetailsPojo ugPojoClass = new UndergraduateDetailsPojo();
        ugPojoClass.setuGraduateName(ugName);
        ugPojoClass.setuGraduateUniId(ugUniId);
        ugPojoClass.setuGraduateGpa(ugGpa);

        pojoArrayList.add(ugPojoClass);

        uGraduateNamesList.add(ugName);
    }

    sqliteDatabase.close();

    return uGraduateNamesList;
}
@Override
protected void onResume() {
    super.onResume();
    pojoArrayList = new ArrayList<UndergraduateDetailsPojo>();
    uGraduateListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());        
    uGraduateNamesListView.setAdapter(uGraduateListAdapter);        
}

@Override
protected void onStart() {
    super.onStart();
    pojoArrayList = new ArrayList<UndergraduateDetailsPojo>();
    uGraduateListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());        
    uGraduateNamesListView.setAdapter(uGraduateListAdapter);    
}

public void onClick1 (View view)
{
Intent newIntent;
newIntent=new Intent(this,HomePageActivity.class);
startActivity(newIntent);
}

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

    Toast.makeText(getApplicationContext(), "Clicked on :" + arg2, Toast.LENGTH_SHORT).show();

    Intent updateDeleteUgraduateIntent = new Intent(this, UpdateDeleteUndergraduateActivity.class);

    UndergraduateDetailsPojo clickedObject =  pojoArrayList.get(arg2);

    Bundle dataBundle = new Bundle();
    dataBundle.putString("clickedUgraduateName", clickedObject.getuGraduateName());
    dataBundle.putString("clickedUgraduateUniId", clickedObject.getuGraduateUniId());
    dataBundle.putDouble("clickedUgraduateGpa", clickedObject.getuGraduateGpa());

    updateDeleteUgraduateIntent.putExtras(dataBundle);

    startActivity(updateDeleteUgraduateIntent);

}

}

1 个答案:

答案 0 :(得分:0)

您可以使用Android中提供的ListActivity。 您只需将数据管理为此ListActivity所需的格式。

您可以在按钮上执行操作,而不是菜单选项。

 package your_package_name;

 import android.app.ListActivity;
 import android.app.NotificationManager;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.Toast;



public class your_activity_name extends ListActivity {

private static final int checkedItem= 0;

private String[] databaseContent;

String checkedItems = new String(); // comma separated
String uncheckedItems = new String(); // comma separated

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);



    databaseContent = your_data_from_database // this array should contain your data from database.     
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_multiple_choice, databaseContent));           

    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
    boolean result = super.onCreateOptionsMenu(menu);       

    menu.add(0, checkedItem, 0, R.string."String you want to show");                

    return result;
}

@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) 
{       

    switch(item.getItemId()) 
    {     
        case checked:
        {
            int reqlength = getListAdapter().getCount();

            for (int i = 0; i < reqlength ; i++) 
            {
                if (getListView().isItemChecked(i)) {
                    checkedItems = checkedItems.concat(databaseContent[i]).concat(",");
                }
                else {
                    uncheckedItems = uncheckedItems.concat(databaseContent[i]).concat(",");                     
                }                   
            } 
            Thread thread = new Thread(){
                @Override
                public void run() {
                    if ( checkedItems.length() > 0 || 
                         uncheckedItems.length() > 0 
                        ) 
                    {
                        //Here is your desired output in checkedItems & uncheckedItems

                    }                                           
                }
            };
            thread.start();

            Toast.makeText(your_activity_name.this, R.string.your_string, Toast.LENGTH_SHORT).show();

            finish();               
            return true;
        }           

    }

    return super.onMenuItemSelected(featureId, item);       
}

@Override
protected void onPause() 
{
            super.onPause();
}

@Override
protected void onResume() 
{
    super.onResume();
        }

}

如需其他方式,请参阅链接 Android ListView With Checkbox choice without using Built-in AistActivity

希望这能解决您的问题。