简体   繁体   English

如何在simpleadapter中实现OnItemClickListener? 代码删除,更新等,

[英]how to implement OnItemClickListener in simpleadapter? code delete, update etc.,

I think I have to code in MainActivity.java only. 我想我只需要在MainActivity.java进行编码。 But I don't know how to implement OnItemClickListener() . 但是我不知道如何实现OnItemClickListener() when I write data in two EditTexts and click insert button, it appears in the listView through simpleadapter . 当我在两个EditTexts写入数据并单击insert按钮时,它通过simpleadapter出现在listView when I select one of row in listView and click delete button, it should be removed from listView . 当我在listView选择行之一并单击Delete按钮时,应将其从listView删除。 I have coded "insert" but I don't know how to code delete, update, onitemclicklistener . 我已经编码了“插入”,但是我不知道如何编码删除,更新onitemclicklistener Is anybody know how to code? 有人知道怎么编码吗? thanks. 谢谢。

activity_main.xml activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.eee.MainActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/eng"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            />
        <EditText
            android:id="@+id/kor"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/ins"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="INS"
            android:onClick="onClick"
            />
        <Button
            android:id="@+id/del"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DEL"
            android:onClick="onClick"
            />
        <Button
            android:id="@+id/upd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="UPD"
            android:onClick="onClick"
            />
    </LinearLayout>
    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

memberlist.xml memberlist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10sp"
        android:textStyle="bold" >
    </TextView>
    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10sp">
    </TextView>
</LinearLayout>

DBAdapter.java DBAdapter.java

public class DBAdapter {
    private DatabaseHelper mHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_NAME = "projectdb";
    private static final int DATABASE_VERSION = 1;
    private static String SQL_TABLE_CREATE;
    private static String TABLE_NAME;

    public static final String SQL_CREATE_PROJECT =
            "create table project (_id integer primary key autoincrement,"
                    + " eng text not null,"
                    + " kor text not null"
                    + ")";

    private final Context mCxt;

                    private static class DatabaseHelper extends SQLiteOpenHelper {

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

                        public DatabaseHelper(Context context, String name,
                                              SQLiteDatabase.CursorFactory factory, int version) {
                            super(context, name, factory, version);
                            // TODO Auto-generated constructor stub
                        }

                        @Override
                        public void onCreate(SQLiteDatabase db) {
                            // TODO Auto-generated method stub
                            db.execSQL(SQL_TABLE_CREATE);
                        }

                        @Override
                        public void onOpen(SQLiteDatabase db) {
                            // TODO Auto-generated method stub
                            super.onOpen(db);
                        }

                        @Override
                        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                            // TODO Auto-generated method stub
                            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                            onCreate(db);
                        }
                    }

    public DBAdapter(Context cxt, String sql, String tableName) {
        this.mCxt = cxt;
        SQL_TABLE_CREATE = sql;
        TABLE_NAME = tableName;
    }

    public DBAdapter open() throws SQLException {
        mHelper = new DatabaseHelper(mCxt);
        mDb = mHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mHelper.close();
    }

    public long insertTable(ContentValues values) {
        return mDb.insert(TABLE_NAME, null, values);
    }

    public boolean deleteTable(String pkColumn, long pkData) {
        return mDb.delete(TABLE_NAME, pkColumn + "=" + pkData, null) > 0;
        /*db.delete("memos", "_id=" + memo.getId(), null);
        db.delete(String table, String whereClause, String[] whereArgs);*/
    }

    public Cursor selectTable(String[] columns, String selection,
                              String[] selectionArgs, String groupBy,
                              String having, String orderBy) {
        return mDb.query(TABLE_NAME, columns, selection, selectionArgs, groupBy, having, orderBy);
    }

    public boolean updateTable(ContentValues values, String pkColumn, long pkData) {
        return mDb.update(TABLE_NAME, values, pkColumn + "=" + pkData, null) > 0;
    }
}

Project.java Project.java

public class Project {
    private long id;
    private String eng;
    private String kor;

    public Project(long id, String eng){
        this.id = id;
        this.eng = eng;
    }
    public Project(long id, String eng, String kor) {
        this.id = id;
        this.eng = eng;
        this.kor = kor;
    }

    public long getId() {
        return id;
    }

    public String getEng() {
        return eng;
    }

    public String getKor() {
        return kor;
    }
}

MainActivity.java MainActivity.java

public class MainActivity extends AppCompatActivity {
    private Button bt;
    private EditText et1;
    private EditText et2;
    private ListView lv;
    DBAdapter db;
    ArrayList<HashMap<String,String>> list;
    SimpleAdapter sap;
    Project project;
    private static final String TAG = "LogTest";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt = (Button)findViewById(R.id.ins);
        et1 = (EditText)findViewById(R.id.eng);
        et2 = (EditText)findViewById(R.id.kor);
        //
        list = new ArrayList<HashMap<String,String>>();
        makeDS();

        sap = new SimpleAdapter(this, list, R.layout.memberlist, new String[] {"eng", "kor"}, new int[] {R.id.text1, R.id.text2});
        lv.setAdapter(sap);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                /*sap.getItem(position);
                et1.setText(project.getEng());
                et2.setText(project.getKor());*/
                Log.v(TAG, "list click");
            }
        });
        db.close();
    }

    public void makeDS(){
        lv = (ListView)findViewById(R.id.list);
        list.clear();
        db = new DBAdapter(this, DBAdapter.SQL_CREATE_PROJECT, "project");

        db.open();
        String columns[] = {"eng", "kor"};
        Cursor cursor = db.selectTable(columns, null, null, null, null, null);

        if(cursor.moveToFirst()) {
            do{
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("eng", cursor.getString(0));
                map.put("kor", cursor.getString(1));
                list.add(map);
            }while(cursor.moveToNext());
        }
    }

    public void onClick(View v) {
        // TODO Auto-generated method stub
        // DBAdapter db;
        switch (v.getId()) {
            case R.id.ins:
                String streng = et1.getText().toString();
                String strkor = et2.getText().toString();

                db = new DBAdapter(this, DBAdapter.SQL_CREATE_PROJECT, "project");
                db.open();

                ContentValues value = new ContentValues();
                value.put("eng", streng);
                value.put("kor", strkor);
                db.insertTable(value);
                break;

            case R.id.del:
                db = new DBAdapter(this, DBAdapter.SQL_CREATE_PROJECT, "project");
                db.open();
                db.deleteTable("_id", project.getId());
                Log.v(TAG, "del button click");
                break;
        }
        makeDS();
        sap.notifyDataSetChanged();
        db.close();
    }
}

First of all declare your buttons in listView so when you click on delete, the app will delete the object you want to delete, otherwise it wo`nt know which object of the listView you want to delete Second you should implementation of OnClickListener in your MainActivity.class when inside the SimpleAdapter Constructor as following: 首先,在listView中声明您的按钮,这样,当您单击Delete时,应用程序将删除您要删除的对象,否则它将不知道您要删除listView的哪个对象。其次,您应该在MainActivity中实现OnClickListener .class在SimpleAdapter构造函数中时,如下所示:

    sap = new SimpleAdapter(this, list, R.layout.memberlist, new String[] {"eng", "kor"}, new int[] {R.id.text1, R.id.text2}) {

                        @Override
                        public View getView(final int position, View convertView,
                                            ViewGroup parent) {
                            ViewGroup layout = (ViewGroup) super.getView(position,
                                    convertView, parent);
                            delete = (Button) layout.findViewById(R.id.delete_button);
                            edit = (Button) layout.findViewById(R.id.edit_button);

                            delete.setTag(position);
                            edit.setTag(position);
                            delete.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    list.remove(position);
                                    notifyDataSetChanged();

                                }
                            });
                            edit.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    //do smth
                                }

                            });

                       return layout;
}
};

and then set your adapter 然后设置你的适配器

Please Change 请更换

new String[] {"eng", "kor"} to String[] tmpdata= {"eng", "kor"};
 sap = new SimpleAdapter(this, list, R.layout.memberlist, tmpdata, new int[] {R.id.text1, R.id.text2});

and now listview.onItemClickListener 现在是listview.onItemClickListener

 et1.setText(tmpData[0]);
            et2.setTexttmpdata[1] );

暂无
暂无

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

相关问题 如何在此代码中实现OnItemClickListener? - how to implement OnItemClickListener in this code? 如何更新Java构造函数,等于,哈希等。在日食? - how to update Java constructors,equals,hash,etc. in eclipse? 如何使用AutoBean实现不同基类型的列表,如String,Integer等? - How to implement with AutoBean a list of different base types like String, Integer, etc.? 如何实现注释以限制成员函数,函数类型等数目,例如FunctionalInterface? - How to implement annotations to limit number of member functions, function types, etc. like FunctionalInterface? 搜索算法(DFS,BFS,A star等)。 如何在没有“冻结”的情况下更新GUI(具有更新状态)? - Search algorithms (DFS,BFS,A star etc.). How to update the GUI (with updated state) without “freezing”? 不接受Android代码-OnItemClickListener()-“…必须实现继承的代码。” - Android code not being accepted - OnItemClickListener() - “…must implement the inherited..” 如何从代码中获取运行中的OSGi容器信息(名称,版本等)? - How to get running OSGi container info (name, version etc.) from code? 如何在Spring的JSP页面上显示URL的HTTP状态代码(例如404、200等)? - How to display a HTTP status code of a url (For example, 404, 200, etc. ) on Spring's JSP page? 如何在Java中查看内置类的源代码(例如BigInteger等)? - How do I view source code of built-in classes in Java (e.g. BigInteger etc.)? 如何在HttpServer的URL中包含tspecials(&lt;,“等)? - How to inclide tspecials(<," etc.) in URL for HttpServer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM