如何更改 getdata 方法以将其输入 Android 中的 listView

How can I change the getdata method to input it into the listView in Android

我希望将它存储在arraylist 中,然后在listView 中显示出来。有什么解决办法吗?如何设置适配器?我怎样才能 link 这两个并使用 arrayList 将其显示到列表视图中?

DBhelper.java

public ArrayList<String> getDataarray(){


    SQLiteQueryBuilder querybuilder=new SQLiteQueryBuilder();
    querybuilder.setTables(DATABASE_TABLES);

  String [] columns= new String[]{ KEY_ROWID,KEY_USERNAME,KEY_AGE,KEY_ClINIC,KEY_PHONE,KEY_EMAIL,KEY_DATESIGNUP
            ,KEY_CONDITIONID,KEY_DOCTORID,KEY_LOGINID,KEY_ACTIVITYNAME,KEY_NOTIFICATIONNAME,KEY_GROUPNAME,KEY_APPROVED
            };

    Cursor c= ourdatabase.query(DATABASE_TABLES, columns, null,null, null, null, null);
    String result="";

    ArrayList<String> resultarray = new ArrayList<String>();


    int iRow=c.getColumnIndex(KEY_ROWID);
    int iUserName=c.getColumnIndex(KEY_USERNAME);
    int iAge=c.getColumnIndex(KEY_AGE);
    int iClinic=c.getColumnIndex(KEY_ClINIC);
    int iPhone=c.getColumnIndex(KEY_PHONE);
    int iEmail=c.getColumnIndex(KEY_EMAIL);
    int iDateSignup=c.getColumnIndex(KEY_DATESIGNUP);
    int iConditionID=c.getColumnIndex(KEY_CONDITIONID);
    int iDoctorID=c.getColumnIndex(KEY_DOCTORID);
    int iLoginID=c.getColumnIndex(KEY_LOGINID);
    int iActivityName=c.getColumnIndex(KEY_ACTIVITYNAME);
    int iNotificationName=c.getColumnIndex(KEY_NOTIFICATIONNAME);
    int iGroupName=c.getColumnIndex(KEY_GROUPNAME);
    int iApproved=c.getColumnIndex(KEY_APPROVED);


     SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT value FROM " + DATABASE_TABLES, null);

        if(cursor.moveToFirst()) {
            do {
                resultarray.add(cursor.getString(cursor.getColumnIndex("value")));
            }while(cursor.moveToNext());
        }

        cursor.close();
        db.close();
        return resultarray;

}

ProfileFragment.java 这是我要在 listView 中检索出来的页面

public View getView( int position ,View convertView,ViewGroup parent){

    LayoutInflater inflater= (LayoutInflater) context.getSystemService(android.content.Context.LAYOUT_INFLATER_SERVICE);
    View row =inflater.inflate(R.layout.profile_list,parent,false);

    TextView myProfile=(TextView)row.findViewById(R.id.Profile);
    TextView myCondition=(TextView)row.findViewById(R.id.condition);

    myProfile.setText(ProfileArray[position]);
    myCondition.setText(resultarray[position]);
    return row;

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootview = inflater.inflate(R.layout.user,container,false);
    Resources res=getResources();
    Profile=res.getStringArray(R.array.Profile);
    //how can i change the line below to let it get the data from the database array? or is there any other method?
    condition=res.getStringArray(R.array.Profile_values);
    list.setAdapter(adapter);

    list=(ListView)rootview.findViewById(R.id.listView1);

     return  rootview;

    }

您可以进行如下操作

public String getData(){ 
ArrayList<Transaction> list = new ArrayList<Transaction>();
String [] columns= new String[]{KEY_ROWID,KEY_USERNAME,KEY_AGE,KEY_ClINIC,KEY_PHONE,KEY_EMAIL,KEY_DATESIGNUP ,KEY_CONDITIONID,KEY_DOCTORID,KEY_LOGINID,KEY_ACTIVITYNAME,KEY_NOTIFICATIONNAME,KEY_GROUPNAME,KEY_APPROVED };
Cursor c= ourdatabase.query(DATABASE_TABLES, columns, null,null, null, null, null); 
String result="";

int iRow=c.getColumnIndex(KEY_ROWID);
int iUserName=c.getColumnIndex(KEY_USERNAME);
int iAge=c.getColumnIndex(KEY_AGE);
int iClinic=c.getColumnIndex(KEY_ClINIC);
int iPhone=c.getColumnIndex(KEY_PHONE);
int iEmail=c.getColumnIndex(KEY_EMAIL);
int iDateSignup=c.getColumnIndex(KEY_DATESIGNUP);
int iConditionID=c.getColumnIndex(KEY_CONDITIONID);
int iDoctorID=c.getColumnIndex(KEY_DOCTORID);
int iLoginID=c.getColumnIndex(KEY_LOGINID);
int iActivityName=c.getColumnIndex(KEY_ACTIVITYNAME);
int iNotificationName=c.getColumnIndex(KEY_NOTIFICATIONNAME);
int iGroupName=c.getColumnIndex(KEY_GROUPNAME);
int iApproved=c.getColumnIndex(KEY_APPROVED);

if (cursor.moveToFirst()) {
        do {
        //create a object for above properties and set the values as follows.
        Transaction t=new Transaction();
        t.setRow(c.getString(iRow));
        .....
        .....
        list.add(t);
        } while (cursor.moveToNext());
 }
return list;
}

在您的 activity 中使用此列表作为列表视图自定义适配器的来源。

希望对您有所帮助。