获取列名的查询是什么?

时间:2015-10-06 11:35:00

标签: android sql database android-listview

|Title  | Subcategory | Details | 
|-------------------------------|
|Song   | TAMIL SONG  |  Yuvan  |    
|-------------------------------|
|Song   | English    |Tom       | 
|-------------------------------|
|Movie  | mal         | abcd    | 
|-------------------------------|
|Movie  | Telugu     |TomCuirse | 
|-------------------------------|
|Sounds  | mal         | abcd   |

我有一个像这样的列名列表。我想根据Title的选择显示Subcategory字段。我们如何为此编写查询以及如何将其附加到listview上的ArrayAdapter?数据库可以在列表视图中显示,textview为空,但列表视图可用

子类别

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.subcategory_layout);
    listView= (ListView) findViewById(R.id.listView2);

    //Intent i = getIntent();

    dbHelper = new SqlLiteDbHelper(this);
    try {
        dbHelper.openDataBase();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    sqLiteDatabase=dbHelper.getReadableDatabase();
    cursor=dbHelper.getsubcategory(sqLiteDatabase);
    subcategoryAdapter = new SubcategoryAdapter(getApplicationContext(),R.layout.sublist_item);
    listView.setAdapter(subcategoryAdapter);
    if (cursor.moveToFirst())
    {
        do {
            String subcategory;
            subcategory=cursor.getString(0);
            foodSupply= new FoodSupply(subcategory);
            subcategoryAdapter.add(foodSupply);

        }while (cursor.moveToNext());
    }enter code here

SubcategoryAdapter

public class SubcategoryAdapter extends ArrayAdapter {
List list = new ArrayList();
public SubcategoryAdapter(Context context, int resource) {
    super(context, resource);
}





    @Override
    public void add (Object object){
    super.add(object);
    list.add(object);
}
    static class LayoutHandler {
        TextView SUBCATEGORY;
    }

    @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;
    LayoutHandler layoutHandler;
    if (row == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.sublist_item, parent, false);
        layoutHandler = new LayoutHandler();
        layoutHandler.SUBCATEGORY = (TextView) row.findViewById(R.id.textView2);
        row.setTag(layoutHandler);
    } else {
        layoutHandler = (LayoutHandler) row.getTag();
    }
    FoodSupply foodSupply = (FoodSupply) this.getItem(position);
    layoutHandler.SUBCATEGORY.setText(foodSupply.getSubcategory());
    return row;
}enter code here

1 个答案:

答案 0 :(得分:1)

您需要使用'Where Params'来选择您希望从查询中返回的内容。像这样获取光标

Cursor cursor = db.query("Your_table_name", "Subcategory", "Title = ?", new String[] { "Song" }, "", "", "");

然后你迭代它:

while(cursor.moveToNext()) {
    String subCategory = cursor.getString(0);
}

之后,您可以将subCategory存储在列表中并将其插入ArrayAdapter。