android中的多列列表来自db

时间:2017-03-09 23:23:51

标签: java android sqlite

我一直在尝试让它工作大约2个小时,我只知道我是令人难以置信的愚蠢,但我不能让我的多列列表正常工作。首先它知道数据库中有两行,但第一行和第二行完全相同,因为它们不应该是第二行,其次上下文菜单也没有正常工作,一旦点击“查看”就崩溃应用程序它

Inventory_List.java

newList = [i=05 while i<=20 i=i+10]

SQLiteHandler.java

private void populateListView() {
    ListView ListView=(ListView)findViewById(R.id.listView1);
    list=new ArrayList<HashMap<String,String>>();


    db = new SQLiteHandler(getApplicationContext());

    list = db.getSomeInvDetails();
    ListViewAdapters adapter=new ListViewAdapters(this, list);
    ListView.setAdapter(adapter);
    registerForContextMenu(ListView);

}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Select The Action");
    menu.add(0, v.getId(), 0, "View");
    menu.add(0, v.getId(), 0, "Delete");
}
@Override
public boolean onContextItemSelected(MenuItem item){
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
    if(item.getTitle()=="View"){
        Intent i = new Intent(Inventory_List.this, Inventory_View.class);
        i.putExtra("id", list.get(info.position));
        startActivity(i);
    }
   else{
        return false;
    }
    return true;
}

@Override
public void onResume() {
    super.onResume();
    populateListView();
}

ListViewsAdapter

public ArrayList<HashMap<String, String>> getSomeInvDetails() {
    ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
    HashMap<String, String> user = new HashMap<String, String>();
    String selectQuery = "SELECT  * FROM " + TABLE_INV;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row

    if (cursor.moveToFirst()) {
        do {
        user.put("title", cursor.getString(1));
            user.put("stock", cursor.getString(5));
            list.add(user);
    }while(cursor.moveToNext());}
    cursor.close();
    db.close();
    // return user
    Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
    return list;

}

StackTrace错误

public class ListViewAdapters extends BaseAdapter{

public ArrayList<HashMap<String, String>> list;
Activity activity;
TextView txtFirst;
TextView txtSecond;
public ListViewAdapters(Activity activity,ArrayList<HashMap<String, String>> list){
    super();
    this.activity=activity;
    this.list=list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub



    LayoutInflater inflater=activity.getLayoutInflater();

    if(convertView == null){

        convertView=inflater.inflate(R.layout.colum_row, null);

        txtFirst=(TextView) convertView.findViewById(R.id.title);
        txtSecond=(TextView) convertView.findViewById(R.id.stock);

    }

    HashMap<String, String> map=list.get(position);
    txtFirst.setText(map.get(FIRST_COLUMN));
    txtSecond.setText(map.get(SECOND_COLUMN));

    return convertView;
}

2 个答案:

答案 0 :(得分:0)

您需要通过do while循环在每个循环中创建一个新用户。

使用上下文菜单时崩溃的堆栈跟踪是什么?

答案 1 :(得分:0)

if(item.getTitle()=="View"){
       ...
}

此块始终转到else,您应该使用euqals

if(item.getTitle().equals("View")){
       ...
}