简体   繁体   中英

Multicolumn list in android from db

I've been trying to get this to work for about 2 hours now and I just know I'm being unbelievably dumb but I can't get my multi column list to work correctly. Firstly it knows there is two rows in the db but the first and second row are the exact same for some reason when they shouldn't be and secondly the context menu is not working correctly either and crashes the app once I click "view" on it.

Inventory_List.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();
}

SQLiteHandler.java

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;

}

ListViewsAdapter

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;
}

StackTrace error

03-10 08:12:38.665 13660-13660/com.example.mypc.inventory W/Bundle: Key id expected String but value was a java.util.HashMap.  The default value <null> was returned.
03-10 08:12:38.666 13660-13660/com.example.mypc.inventory W/Bundle: Attempt to cast generated internal exception:
                                                                    java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String
                                                                        at android.os.BaseBundle.getString(BaseBundle.java:995)
                                                                        at android.content.Intent.getStringExtra(Intent.java:6243)
                                                                        at com.example.mypc.inventory.Inventory_View.onCreate(Inventory_View.java:59)
                                                                        at android.app.Activity.performCreate(Activity.java:6679)
                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                        at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                        at android.os.Looper.loop(Looper.java:154)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
03-10 08:12:38.671 13660-13660/com.example.mypc.inventory D/SQLiteHandler: Fetching user from Sqlite: {}
03-10 08:12:38.675 13660-13660/com.example.mypc.inventory D/AndroidRuntime: Shutting down VM
03-10 08:12:38.676 13660-13660/com.example.mypc.inventory E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.mypc.inventory, PID: 13660
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mypc.inventory/com.example.mypc.inventory.Inventory_View}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                                at com.example.mypc.inventory.Inventory_View.onCreate(Inventory_View.java:61)
                                                                                at android.app.Activity.performCreate(Activity.java:6679)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:154) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

You need to create a new user every loop through your do while loop.

What is the stack trace of the crash when you use context menu?

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

This block always goes to else , you should use euqals instead.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM