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

Posted

技术标签:

【中文标题】如何在 simpleadapter 中实现 OnItemClickListener?代码删除、更新等,【英文标题】:how to implement OnItemClickListener in simpleadapter? code delete, update etc., 【发布时间】:2017-04-16 21:58:29 【问题描述】:

我想我只需要在MainActivity.java 中编码。但我不知道如何实现OnItemClickListener()。当我在两个EditTexts 中写入数据并单击插入按钮时,它会出现在listViewsimpleadapter 中。当我在listView 中选择一行并单击删除按钮时,它应该从listView 中删除。我已经编写了“插入”代码,但我不知道如何编写删除、更新、onitemclicklistener 的代码。有人知道如何编码吗?谢谢。

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_
    android:layout_
    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_
        android:layout_>
        <EditText
            android:id="@+id/eng"
            android:layout_
            android:layout_weight="1"
            android:layout_
            />
        <EditText
            android:id="@+id/kor"
            android:layout_
            android:layout_weight="1"
            android:layout_
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_
        android:layout_>
        <Button
            android:id="@+id/ins"
            android:layout_
            android:layout_
            android:text="INS"
            android:onClick="onClick"
            />
        <Button
            android:id="@+id/del"
            android:layout_
            android:layout_
            android:text="DEL"
            android:onClick="onClick"
            />
        <Button
            android:id="@+id/upd"
            android:layout_
            android:layout_
            android:text="UPD"
            android:onClick="onClick"
            />
    </LinearLayout>
    <ListView
        android:id="@+id/list"
        android:layout_
        android:layout_
        />
</LinearLayout>

memberlist.xml

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

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

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

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();
    

【问题讨论】:

【参考方案1】:

请更换

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

现在是 listview.onItemClickListener

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

【讨论】:

【参考方案2】:

首先在listView中声明你的按钮,这样当你点击delete时,app会删除你要删除的对象,否则它不知道你要删除listView的哪个对象 其次,您应该在 SimpleAdapter 构造函数中的 MainActivity.class 中实现 OnClickListener,如下所示:

    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;

;

然后设置你的适配器

【讨论】:

data.remove(位置);请问那里的“数据”是什么意思? 你应该使用列表而不是数据 我更新了我的答案,我猜你有点困惑

以上是关于如何在 simpleadapter 中实现 OnItemClickListener?代码删除、更新等,的主要内容,如果未能解决你的问题,请参考以下文章

SimpleAdapter 每隔一秒启动一次就崩溃一次

如何在 simpleAdapter 表单 URL(XML 提要)中显示图像?

如何在Castle.Core中实现IProxyGenerationHook的类中实现Equals和GetHashCode的覆盖方法?

如何在android中实现svm模型?

如何使用 SimpleAdapter 在微调器中设置选择?

如何在 laravel 中实现 vuetify?