简体   繁体   English

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

[英]Android OnclickItem listview more detailed info in another activity from SQLite database

I want to request data from a database. 我想从数据库请求数据。 In the list I want to Display only the title and if you click it I want to show more detailed info. 在列表中,我只想显示标题,如果单击它,我想显示更多详细信息。 How would I get the position of the ListItem and show the data that belongs to that one list item? 我将如何获取ListItem的位置并显示属于该列表项的数据? Here's my code: 这是我的代码:

FragmentRonde1.java 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 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;
}

}

I really hope someone can help me on this because I have no idea how to do it since I'm fairly new to this. 我真的希望有人可以帮我解决这个问题,因为我对此并不陌生,所以我不知道该怎么做。

You receive the item position as position parameter 您收到项目位置作为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;

    }
});

Then you can get all the data from the Cursor with item position X 然后,您可以从光标位置获得项目X的所有数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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