ANDROID创建一个可点击的ListView

时间:2015-09-27 16:49:04

标签: android listview

  

因此,简而言之,我想点击列表中的某个项目,打开包含该项目更多数据的页面。   我知道也许我问得太多,但我需要你的帮助。

各位大家好,我想创建个人应用程序,多年来我记得几何问题,所以我做了一个数据库,然后我做了一个适配器将项目放在一个表中相同的布局:https://gyazo.com/f907281860906d6301bd98d896351650 (在此表中,您只看到图中的名称和个人注释),但事实是,当我将数据放在表单中时 https://gyazo.com/b14e0877e23612ac6d77bf328305651f 我把它们放在图的名称上,个人笔记也插入数据,这样当你在该表中查看时,我点击"打开"按钮无法看到问题的数据,有没有办法做到这一点?在这里,您将获得DBHelper,元素的布局,ListDataAdapter和Main活动,必须阅读这些框。

DBHelper

package com.example.gioele.mathup;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by Gioele on 13/07/2015.
 */
public class LocalDB1Helper extends SQLiteOpenHelper {
//--------------inizializzazione/creazione database------------------------------------------------------------
    private static final String DATABASE_NAME = "linguaggiodb";
    private static final int DATABASE_VERSIONS = 4;
    private static final String CREATE_QUERY =
    "CREATE TABLE "+ LocalDB2.tabella1.TABLE_NAME+" (" + LocalDB2.tabella1.USER_LINGUAGGIO + " TEXT,"+
            LocalDB2.tabella1.USER_FIGURA1 + " TEXT,"+
            LocalDB2.tabella1.USER_FIGURA2 + " TEXT,"+
            LocalDB2.tabella1.USER_NICKNAME_PROBLEM + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_1 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_2 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_3 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_4 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_5 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_6 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_7 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_8 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_9 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_10 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_11 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_12 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_13 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_14 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_15 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_16 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_17 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_18 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_19 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_20 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_21 + " TEXT,"+
            LocalDB2.tabella1.SCRITTA_22 + " TEXT,"+
            LocalDB2.tabella1.R1 + " TEXT,"+
            LocalDB2.tabella1.R2 + " TEXT,"+
            LocalDB2.tabella1.R3 + " TEXT,"+
            LocalDB2.tabella1.R4 + " TEXT,"+
            LocalDB2.tabella1.R5 + " TEXT,"+
            LocalDB2.tabella1.R6 + " TEXT,"+
            LocalDB2.tabella1.R7 + " TEXT,"+
            LocalDB2.tabella1.R8 + " TEXT,"+
            LocalDB2.tabella1.R9 + " TEXT,"+
            LocalDB2.tabella1.R10 + " TEXT,"+
            LocalDB2.tabella1.R11 + " TEXT,"+
            LocalDB2.tabella1.R12 + " TEXT,"+
            LocalDB2.tabella1.R13 + " TEXT,"+
            LocalDB2.tabella1.R14 + " TEXT,"+
            LocalDB2.tabella1.R15 + " TEXT,"+
            LocalDB2.tabella1.R16 + " TEXT,"+
            LocalDB2.tabella1.R17 + " TEXT,"+
            LocalDB2.tabella1.R18 + " TEXT,"+
            LocalDB2.tabella1.R19 + " TEXT,"+
            LocalDB2.tabella1.R20 + " TEXT,"+
            LocalDB2.tabella1.R21 + " TEXT,"+
            LocalDB2.tabella1.R22 + " TEXT,"+
            LocalDB2.tabella1.RR1 + " TEXT,"+
            LocalDB2.tabella1.RR2 + " TEXT,"+
            LocalDB2.tabella1.RR3 + " TEXT,"+
            LocalDB2.tabella1.RR4 + " TEXT,"+
            LocalDB2.tabella1.RR5 + " TEXT,"+
            LocalDB2.tabella1.RR6 + " TEXT,"+
            LocalDB2.tabella1.RR7 + " TEXT,"+
            LocalDB2.tabella1.RR8 + " TEXT,"+
            LocalDB2.tabella1.RR9 + " TEXT,"+
            LocalDB2.tabella1.RR10 + " TEXT,"+
            LocalDB2.tabella1.RR11 + " TEXT,"+
            LocalDB2.tabella1.RR12 + " TEXT,"+
            LocalDB2.tabella1.RR13 + " TEXT,"+
            LocalDB2.tabella1.RR14 + " TEXT,"+
            LocalDB2.tabella1.RR15 + " TEXT,"+
            LocalDB2.tabella1.RR16 + " TEXT,"+
            LocalDB2.tabella1.RR17 + " TEXT,"+
            LocalDB2.tabella1.RR18 + " TEXT,"+
            LocalDB2.tabella1.RR19 + " TEXT,"+
            LocalDB2.tabella1.RR20 + " TEXT,"+
            LocalDB2.tabella1.RR21 + " TEXT,"+
            LocalDB2.tabella1.RR22 + " TEXT );";
            ;





    public LocalDB1Helper(Context context)
    {
        super(context,DATABASE_NAME,null,DATABASE_VERSIONS);
        Log.e("DATABASE OPERATIONS","Database create / opened.....");
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_QUERY);
        Log.e("DATABASE OPERATIONS", "Table created");

    }
//------------comandi: leggi, scrivi----------------------------------------------------------------------
    //scrivi
    public void addInformations(String linguaggio,
                                String user_figura1,
                                String user_figura2,
                                String user_nickname_problem,
                                String scritta1,
                                String scritta2,
                                String scritta3,
                                String scritta4,
                                String scritta5,
                                String scritta6,
                                String scritta7,
                                String scritta8,
                                String scritta9,
                                String scritta10,
                                String scritta11,
                                String scritta12,
                                String scritta13,
                                String scritta14,
                                String scritta15,
                                String scritta16,
                                String scritta17,
                                String scritta18,
                                String scritta19,
                                String scritta20,
                                String scritta21,
                                String scritta22,

                                String r_1,
                                String r_2,
                                String r_3,
                                String r_4,
                                String r_5,
                                String r_6,
                                String r_7,
                                String r_8,
                                String r_9,
                                String r_10,
                                String r_11,
                                String r_12,
                                String r_13,
                                String r_14,
                                String r_15,
                                String r_16,
                                String r_17,
                                String r_18,
                                String r_19,
                                String r_20,
                                String r_21,
                                String r_22,

                                String rr_1,
                                String rr_2,
                                String rr_3,
                                String rr_4,
                                String rr_5,
                                String rr_6,
                                String rr_7,
                                String rr_8,
                                String rr_9,
                                String rr_10,
                                String rr_11,
                                String rr_12,
                                String rr_13,
                                String rr_14,
                                String rr_15,
                                String rr_16,
                                String rr_17,
                                String rr_18,
                                String rr_19,
                                String rr_20,
                                String rr_21,
                                String rr_22,


                                SQLiteDatabase db)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(LocalDB2.tabella1.USER_LINGUAGGIO,linguaggio);
        contentValues.put(LocalDB2.tabella1.USER_FIGURA1,user_figura1);
        contentValues.put(LocalDB2.tabella1.USER_FIGURA2,user_figura2);
        contentValues.put(LocalDB2.tabella1.USER_NICKNAME_PROBLEM,user_nickname_problem);
        contentValues.put(LocalDB2.tabella1.SCRITTA_1,scritta1);
        contentValues.put(LocalDB2.tabella1.SCRITTA_2,scritta2);
        contentValues.put(LocalDB2.tabella1.SCRITTA_3,scritta3);
        contentValues.put(LocalDB2.tabella1.SCRITTA_4,scritta4);
        contentValues.put(LocalDB2.tabella1.SCRITTA_5,scritta5);
        contentValues.put(LocalDB2.tabella1.SCRITTA_6,scritta6);
        contentValues.put(LocalDB2.tabella1.SCRITTA_7,scritta7);
        contentValues.put(LocalDB2.tabella1.SCRITTA_8,scritta8);
        contentValues.put(LocalDB2.tabella1.SCRITTA_9,scritta9);
        contentValues.put(LocalDB2.tabella1.SCRITTA_10,scritta10);
        contentValues.put(LocalDB2.tabella1.SCRITTA_11,scritta11);
        contentValues.put(LocalDB2.tabella1.SCRITTA_12,scritta12);
        contentValues.put(LocalDB2.tabella1.SCRITTA_13,scritta13);
        contentValues.put(LocalDB2.tabella1.SCRITTA_14,scritta14);
        contentValues.put(LocalDB2.tabella1.SCRITTA_15,scritta15);
        contentValues.put(LocalDB2.tabella1.SCRITTA_16,scritta16);
        contentValues.put(LocalDB2.tabella1.SCRITTA_17,scritta17);
        contentValues.put(LocalDB2.tabella1.SCRITTA_18,scritta18);
        contentValues.put(LocalDB2.tabella1.SCRITTA_19,scritta19);
        contentValues.put(LocalDB2.tabella1.SCRITTA_20,scritta20);
        contentValues.put(LocalDB2.tabella1.SCRITTA_21,scritta21);
        contentValues.put(LocalDB2.tabella1.SCRITTA_22,scritta22);
        contentValues.put(LocalDB2.tabella1.R1,r_1);
        contentValues.put(LocalDB2.tabella1.R2,r_2);
        contentValues.put(LocalDB2.tabella1.R3,r_3);
        contentValues.put(LocalDB2.tabella1.R4,r_4);
        contentValues.put(LocalDB2.tabella1.R5,r_5);
        contentValues.put(LocalDB2.tabella1.R6,r_6);
        contentValues.put(LocalDB2.tabella1.R7,r_7);
        contentValues.put(LocalDB2.tabella1.R8,r_8);
        contentValues.put(LocalDB2.tabella1.R9,r_9);
        contentValues.put(LocalDB2.tabella1.R10,r_12);
        contentValues.put(LocalDB2.tabella1.R11,r_12);
        contentValues.put(LocalDB2.tabella1.R12,r_12);
        contentValues.put(LocalDB2.tabella1.R13,r_12);
        contentValues.put(LocalDB2.tabella1.R14,r_12);
        contentValues.put(LocalDB2.tabella1.R15,r_15);
        contentValues.put(LocalDB2.tabella1.R16,r_16);
        contentValues.put(LocalDB2.tabella1.R17,r_17);
        contentValues.put(LocalDB2.tabella1.R18,r_18);
        contentValues.put(LocalDB2.tabella1.R19,r_19);
        contentValues.put(LocalDB2.tabella1.R20,r_20);
        contentValues.put(LocalDB2.tabella1.R21,r_21);
        contentValues.put(LocalDB2.tabella1.R22,r_22);
        contentValues.put(LocalDB2.tabella1.RR1,rr_1);
        contentValues.put(LocalDB2.tabella1.RR2,rr_2);
        contentValues.put(LocalDB2.tabella1.RR3,rr_3);
        contentValues.put(LocalDB2.tabella1.RR4,rr_4);
        contentValues.put(LocalDB2.tabella1.RR5,rr_5);
        contentValues.put(LocalDB2.tabella1.RR6,rr_6);
        contentValues.put(LocalDB2.tabella1.RR7,rr_7);
        contentValues.put(LocalDB2.tabella1.RR8,rr_8);
        contentValues.put(LocalDB2.tabella1.RR9,rr_9);
        contentValues.put(LocalDB2.tabella1.RR10,rr_10);
        contentValues.put(LocalDB2.tabella1.RR11,rr_11);
        contentValues.put(LocalDB2.tabella1.RR12,rr_12);
        contentValues.put(LocalDB2.tabella1.RR13,rr_13);
        contentValues.put(LocalDB2.tabella1.RR14,rr_14);
        contentValues.put(LocalDB2.tabella1.RR15,rr_15);
        contentValues.put(LocalDB2.tabella1.RR16,rr_16);
        contentValues.put(LocalDB2.tabella1.RR17,rr_17);
        contentValues.put(LocalDB2.tabella1.RR18,rr_18);
        contentValues.put(LocalDB2.tabella1.RR19,rr_19);
        contentValues.put(LocalDB2.tabella1.RR20,rr_20);
        contentValues.put(LocalDB2.tabella1.RR21,rr_21);
        contentValues.put(LocalDB2.tabella1.RR22,rr_22);


        db.insert(LocalDB2.tabella1.TABLE_NAME, null, contentValues);
        Log.e("DATABASE OPERATIONS", "One row inserted");

    }
    //leggi
    public Cursor getInformations(SQLiteDatabase db) {
        Log.e("DATABASE OPERATIONS", "AVVIATO");
        Cursor cursor;
        final String[] uno = {LocalDB2.tabella1.USER_LINGUAGGIO,
                LocalDB2.tabella1.USER_FIGURA1,
                LocalDB2.tabella1.USER_FIGURA2,
                LocalDB2.tabella1.USER_NICKNAME_PROBLEM,
                LocalDB2.tabella1.SCRITTA_1,
                LocalDB2.tabella1.SCRITTA_2,
                LocalDB2.tabella1.SCRITTA_3,
                LocalDB2.tabella1.SCRITTA_4,
                LocalDB2.tabella1.SCRITTA_5,
                LocalDB2.tabella1.SCRITTA_6,
                LocalDB2.tabella1.SCRITTA_7,
                LocalDB2.tabella1.SCRITTA_8,
                LocalDB2.tabella1.SCRITTA_9,
                LocalDB2.tabella1.SCRITTA_10,
                LocalDB2.tabella1.SCRITTA_11,
                LocalDB2.tabella1.SCRITTA_12,
                LocalDB2.tabella1.SCRITTA_13,
                LocalDB2.tabella1.SCRITTA_14,
                LocalDB2.tabella1.SCRITTA_15,
                LocalDB2.tabella1.SCRITTA_16,
                LocalDB2.tabella1.SCRITTA_17,
                LocalDB2.tabella1.SCRITTA_18,
                LocalDB2.tabella1.SCRITTA_19,
                LocalDB2.tabella1.SCRITTA_20,
                LocalDB2.tabella1.SCRITTA_21,
                LocalDB2.tabella1.SCRITTA_22,
                LocalDB2.tabella1.R1,
                LocalDB2.tabella1.R2,
                LocalDB2.tabella1.R3,
                LocalDB2.tabella1.R4,
                LocalDB2.tabella1.R5,
                LocalDB2.tabella1.R6,
                LocalDB2.tabella1.R7,
                LocalDB2.tabella1.R8,
                LocalDB2.tabella1.R9,
                LocalDB2.tabella1.R10,
                LocalDB2.tabella1.R11,
                LocalDB2.tabella1.R12,
                LocalDB2.tabella1.R13,
                LocalDB2.tabella1.R14,
                LocalDB2.tabella1.R15,
                LocalDB2.tabella1.R16,
                LocalDB2.tabella1.R17,
                LocalDB2.tabella1.R18,
                LocalDB2.tabella1.R19,
                LocalDB2.tabella1.R20,
                LocalDB2.tabella1.R21,
                LocalDB2.tabella1.R22,
                LocalDB2.tabella1.RR1,
                LocalDB2.tabella1.RR2,
                LocalDB2.tabella1.RR3,
                LocalDB2.tabella1.RR4,
                LocalDB2.tabella1.RR5,
                LocalDB2.tabella1.RR6,
                LocalDB2.tabella1.RR7,
                LocalDB2.tabella1.RR8,
                LocalDB2.tabella1.RR9,
                LocalDB2.tabella1.RR10,
                LocalDB2.tabella1.RR11,
                LocalDB2.tabella1.RR12,
                LocalDB2.tabella1.RR13,
                LocalDB2.tabella1.RR14,
                LocalDB2.tabella1.RR15,
                LocalDB2.tabella1.RR16,
                LocalDB2.tabella1.RR17,
                LocalDB2.tabella1.RR18,
                LocalDB2.tabella1.RR19,
                LocalDB2.tabella1.RR20,
                LocalDB2.tabella1.RR21,
                LocalDB2.tabella1.RR22,
                };

        Cursor c = db.query(LocalDB1.tabella1.TABLE_NAME, uno, null, null, null, null, null);
        return c;

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }


}

ListDataAdapter

package com.example.gioele.mathup;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Gioele on 24/09/2015.
 */
public class ListDataAdapter extends ArrayAdapter {
    List list = new ArrayList<>();
    public ListDataAdapter(Context context, int resource) {
        super(context, resource);
    }


    static class LayoutHandler
    {
        TextView user_figura1, user_figura2, user_nickname_problem;
    }
    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @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 tabella2 = convertView;
        LayoutHandler layoutHandler;
        if(tabella2 == null){
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            tabella2 = layoutInflater.inflate(R.layout.tabella2,parent,false);
            layoutHandler = new LayoutHandler();
            layoutHandler.user_figura1 = (TextView)tabella2.findViewById(R.id.figura11);
            layoutHandler.user_figura2 = (TextView)tabella2.findViewById(R.id.figura22);
            layoutHandler.user_nickname_problem = (TextView)tabella2.findViewById(R.id.nominativo);
            tabella2.setTag(layoutHandler);
        }
        else{
            layoutHandler = (LayoutHandler) tabella2.getTag();

        }

        DataProvider dataProvider = (DataProvider)this.getItem(position);
        layoutHandler.user_figura1.setText(dataProvider.getUser_figura1());
        layoutHandler.user_figura2.setText(dataProvider.getUser_figura2());
        layoutHandler.user_nickname_problem.setText(dataProvider.getUser_nickname_problem());
        return tabella2;
    }
}

MainActivity

final ListView listview1 = (ListView) findViewById(R.id.listview);
            listDataAdapter = new ListDataAdapter(getApplicationContext(),R.layout.tabella2);
            listview1.setAdapter(listDataAdapter);
            helper = new LocalDB1Helper(getApplicationContext());
            SQLiteDatabase = helper.getReadableDatabase();
            c2 = helper.getInformations(SQLiteDatabase);
            if (c2.moveToFirst()) {
                do{
                    String user_figura1, user_figura2, user_nickname_problem;
                    user_figura1 = c2.getString(1);
                    user_figura2 = c2.getString(2);
                    user_nickname_problem = c2.getString(3);
                    DataProvider dataProvider = new DataProvider(user_figura1, user_figura2, user_nickname_problem);
                    listDataAdapter.add(dataProvider);

                }while (c2.moveToNext());
            }

因此,简而言之,我想点击列表中的某个项目,打开包含该项目更多数据的页面。 我知道也许我问得太多,但我需要你的帮助。

1 个答案:

答案 0 :(得分:0)

查看ListView.setOnItemClickListener()方法。您在此处提供OnItemClickListener,当用户单击列表视图的项目时,会调用onItemClick方法。