ListView不会从SQLite

时间:2015-10-19 01:37:27

标签: java android xml sqlite listview

我正在学习本教程:https://www.youtube.com/watch?v=3k3CunDZpFk&list=PLshdtb5UWjSrEUEKlfHwqQtYu2HxtCwu_&index=8

尝试从SQLite数据库填充自定义ListView,但不会填充。我运行应用程序时出现ZERO错误。它只是不会填充。我已多次浏览视频 - 双倍,三重检查代码无济于事。我还确保数据实际上已插入数据库 - 它是。我用SQLite浏览器检查了它。

我正在尝试使用ListView填充MainActivity.java中的display_character_row.xmlchar_list_border_style.xml由自定义边框public class BackgroundTask extends AsyncTask <String,CharacterDisplay,String> { Context ctx; CharacterAdapter characterAdapter; Activity activity; ListView listView; BackgroundTask(Context ctx) { this.ctx = ctx; activity = (Activity) ctx; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... params) { String method = params[0]; DatabaseOperations databaseOperations = new DatabaseOperations(ctx); if (method.equals("add_info")) { String name = params[1]; SQLiteDatabase db = databaseOperations.getWritableDatabase(); databaseOperations.putInfo(db, name); return "One row inserted"; } else if (method.equals("get_info")) { listView = (ListView) activity.findViewById(R.id.char_list); SQLiteDatabase db = databaseOperations.getReadableDatabase(); Cursor cursor = databaseOperations.getInfo(db); characterAdapter = new CharacterAdapter(ctx, R.layout.display_character_row); String sName; while (cursor.moveToNext()) { sName = cursor.getString(cursor.getColumnIndex(TableData.TableInfo.NAME)); CharacterDisplay characterDisplay = new CharacterDisplay(sName); publishProgress(characterDisplay); } return "get_info"; } return null; } @Override protected void onProgressUpdate(CharacterDisplay... values) { characterAdapter.add(values[0]); } @Override protected void onPostExecute(String result) { if (result.equals("get_info")) { listView.setAdapter(characterAdapter); } else { Toast.makeText(ctx,result, Toast.LENGTH_LONG).show(); } } } 修改。

我希望一些更有经验的眼睛可以看看并帮助我。

感谢您的时间。

以下是一切:

BackgroundTask.java

public class DatabaseOperations extends SQLiteOpenHelper {

private static final int database_version = 1;
private static final String database_name = "character_info.db";

private static final String CREATE_QUERY = "CREATE TABLE " +
        TableData.TableInfo.TABLE_NAME + "("+
        TableData.TableInfo.NAME
//            + " TEXT,"+
/*            // Attributes Begin
        TableData.TableInfo.HEALTH + " TEXT,"+
        TableData.TableInfo.STRENGTH + " TEXT,"+
        TableData.TableInfo.DEXTERITY + " TEXT,"+
        TableData.TableInfo.INTELLIGENCE + " TEXT,"+
        TableData.TableInfo.CHARISMA + " TEXT,"+
        TableData.TableInfo.MANA + " TEXT,"+
        // Skills Begin
        TableData.TableInfo.MELEE + " TEXT,"+
        TableData.TableInfo.RANGED + " TEXT,"+
        TableData.TableInfo.UNARMED + " TEXT,"+
        TableData.TableInfo.AGILITY + " TEXT,"+
        TableData.TableInfo.STEALTH + " TEXT,"+
        TableData.TableInfo.RIDING + " TEXT,"+
        TableData.TableInfo.WILDERNESS + " TEXT,"+
        TableData.TableInfo.RESTORATION + " TEXT,"+
        TableData.TableInfo.FORGING + " TEXT,"+
        TableData.TableInfo.ACTING + " TEXT,"+
        TableData.TableInfo.CONVERSATION + " TEXT,"+
        TableData.TableInfo.ANIMAL_HUSBANDRY + " TEXT,"+
        TableData.TableInfo.EARTH + " TEXT,"+
        TableData.TableInfo.AIR + " TEXT,"+
        TableData.TableInfo.WATER + " TEXT,"+
        TableData.TableInfo.FIRE*/
        + " TEXT );";

DatabaseOperations (Context ctx) {
    super(ctx, database_name, null, database_version);
    Log.d("Database operations", "Database created");
}

@Override
public void onCreate(SQLiteDatabase sbd) {
    sbd.execSQL(CREATE_QUERY);
    Log.d("Database operations", "Table created");
}

@Override
public void onUpgrade(SQLiteDatabase sdb, int oldVersion, int newVersion) {

}

@Override
public void onOpen (SQLiteDatabase sbd) {

}

public Cursor getInfo(SQLiteDatabase db) {
    String [] projections = {TableData.TableInfo.NAME};
    Cursor cursor = db.query(TableData.TableInfo.TABLE_NAME, projections, null, null, null, null, null);
    return cursor;
}

public void putInfo (SQLiteDatabase sdb, String name) {
    ContentValues  cv = new ContentValues();
    cv.put(TableData.TableInfo.NAME, name);
    sdb.insert(TableData.TableInfo.TABLE_NAME, null, cv);
    Log.d("Database operations", "One row inserted");
}
}

DatabaseOperations.java

public class CharacterAdapter extends ArrayAdapter {
List list = new ArrayList();

static class CharacterHolder {
    TextView tx_name;
}

public CharacterAdapter(Context context, int resource) {
    super(context, resource);
}

public void add(Character object) {
    list.add(object);
    super.add(object);
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    CharacterHolder characterHolder;
    if (row == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.display_character_row, parent, false);
        characterHolder = new CharacterHolder();
        characterHolder.tx_name = (TextView) row.findViewById(R.id.char_name);
        row.setTag(characterHolder);
    } else {
        characterHolder = (CharacterHolder) row.getTag();
    }

    CharacterDisplay characterDisplay = (CharacterDisplay) getItem(position);
    characterHolder.tx_name.setText(characterDisplay.getdName().toString());
    return row;
}
}

CharacterAdapter.java

public class CharacterDisplay {

private String dName;

public CharacterDisplay(String dName) {
    this.setdName(dName);
}

public String getdName() {
    return dName;
}

public void setdName(String dName) {
    this.dName = dName;
}
}

CharacterDisplay.java

public class MainActivity extends AppCompatActivity
    implements CreateNewChar_Fragment.OnFragmentInteractionListener {

public void onFragmentInteraction(Uri uri){

}

public void showCreateNewChar_Fragment() {
    android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    CreateNewChar_Fragment createNewChar_fragment = new CreateNewChar_Fragment();
    createNewChar_fragment.show(ft, "createnewchar_fragment");
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute("get_info");

}

@Override
protected void onResume() {

    BackgroundTask backgroundTask = new BackgroundTask(this);
    backgroundTask.execute("get_info");

    super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        startActivity(new Intent(this, Settings.class));
        return true;
    } else if (id == R.id.tabs) {

    }

    if (id == R.id.createcharacter) {
        showCreateNewChar_Fragment();
        return true;
    }

    if (id == R.id.rulebook) {
        startActivity(new Intent(this, Rulebook.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

MainActivity.java

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/char_list" />

</RelativeLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="75dp" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="75dp"
    android:id="@+id/char_name"
    android:text=""
    android:textStyle="bold"
    android:gravity="center"
    android:textAppearance="?android:textAppearanceLarge"
    android:background="@drawable/char_list_border_style"
    android:layout_margin="5dp" />

</RelativeLayout>

display_charater_row.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<stroke
    android:width="3dp"
    android:color="#000000" />

<corners
    android:radius="10dp" />

<padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />

</shape>

char_list_border_style.xml

UnsafeMutablePointer<Character>

0 个答案:

没有答案