Android OnclickItem listview查看SQLite数据库中另一个活动的更详细信息

时间:2015-10-26 22:22:12

标签: java android sqlite simplecursoradapter onitemclicklistener

我想从数据库请求数据。在列表中我想只显示标题,如果你点击它我想显示更详细的信息。如何获取ListItem的位置并显示属于该列表项的数据? 这是我的代码:

FragmentRonde1.java

public class FragmentRonde1 extends android.support.v4.app.Fragment {

public ListView lv1;
public TextView tv1, tv2;
MyDbHandler db;
SimpleCursorAdapter dataAdapter;

public FragmentRonde1() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View rootview = inflater.inflate(R.layout.fragment_ronde1, container, false);
    ((MainActivity) getActivity()).setTitle("Workshops Ronde 1");

    db = new MyDbHandler(getActivity());

    db.InsertValues();
    final Cursor cursor=db.GetAllData();
    String from [] = new String[]{db.COLUMN_TITLE};
    int to [] = new int[] {R.id.workshopTitle1};
    dataAdapter = new SimpleCursorAdapter(getActivity(),R.layout.row_layout_workshop, cursor, from, to, 0);

    lv1 = (ListView)rootview.findViewById(R.id.Ronde1lv);
    lv1.setAdapter(dataAdapter);

    lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent listClicked = new Intent(getActivity(), ListClicked.class);
            startActivity(listClicked);

            tv1 = (TextView)rootview.findViewById(R.id.listClickedTitle);
            tv2 = (TextView)rootview.findViewById(R.id.listClickedDescription);

        }
    });

    return rootview;
    }
}

MyDbHandler.java

public class MyDbHandler extends SQLiteOpenHelper {

private static int DATABASE_VERSION = 6;
private static String DATABASE_NAME = "workshopDB";
private static String TABLE_WORKSHOPS = "workshops";

public static String COLUMN_ID = "_id";
public static String COLUMN_TITLE= "title";
public static String COLUMN_BESCHRIJVING = "beschrijving";
public static final String COLUMN_MGT = "mgt";
public static final String COLUMN_DOC = "doc";
public static final String COLUMN_OOP = "oop";
public static final String COLUMN_BEGELEIDER = "begeleider";
public static final String COLUMN_DEELNEMERS = "deelnemers";
public static final String COLUMN_RONDE1 = "ronde1";
public static final String COLUMN_RONDE2 = "ronde2";
public static final String COLUMN_RONDE3 = "ronde3";
public static final String COLUMN_LOKAAL1 = "lokaal1";
public static final String COLUMN_LOKAAL2 = "lokaal2";
public static final String COLUMN_LOKAAL3 = "lokaal3";

public MyDbHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS workshops(_id INT, title VARCHAR, beschrijving VARCHAR);");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE workshops;");
    db.execSQL("CREATE TABLE IF NOT EXISTS workshops(_id INT, title VARCHAR, beschrijving VARCHAR);");
}


public void InsertValues()
{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");
    db.execSQL("INSERT INTO workshops VALUES(1, 'Title Would be Here', 'Here would be the description');");


    db.close();
}

public Cursor GetAllData()
{
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM workshops;", null);
    return c;
}

}

我真的希望有人可以帮助我,因为我不知道怎么做,因为我对此很新。

1 个答案:

答案 0 :(得分:0)

您收到的商品位置为position参数

lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent listClicked = new Intent(getActivity(), ListClicked.class);
        startActivity(listClicked);

        tv1 = (TextView)rootview.findViewById(R.id.listClickedTitle);
        tv2 = (TextView)rootview.findViewById(R.id.listClickedDescription); 

        //This is the position of the item clicked            
        int pos = position;

        //You also can have the item ID
        long itemID = id;

    }
});

然后,您可以从项目位置X

获取Cursor中的所有数据