SimpleCustomAdapter가 내 ListView를 처리하고 채 웠지만 ListView 내에 삭제 ImageButton을 구현하려고했습니다. 이를 위해 사용자 지정 어댑터를 만들어야했습니다. 이제 ListView에 데이터가 표시되지 않습니다.사용자 지정 Simplecursoradapter가 Listview를 채우지 않음
내 MainActivity.java로, MyListAdapter 클래스도 배치했습니다 (맨 아래). 나는 초심자이므로 코드가 여기 저기에있을 수도 있다는 것을 알고 있습니다. MainAcitivty는 어댑터 내에서 deleteNotes에서 catch 할 수 있도록 sharedpreferences에있는 item의 id를 보내는 listview에 대한 setonclicklistener를 호출합니다. 또한 삭제 단추가 완벽하게 작동하고 메모를 삭제할 수 있지만 유일한 문제는 ListView에 제목과 날짜가 표시되지 않는다는 것입니다. 감사합니다
package com.example.android.notepad2;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.media.Image;
import android.preference.PreferenceManager;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import static com.example.android.notepad2.R.id.parent;
import static com.example.android.notepad2.R.layout.listtemplate;
public class MainActivity extends AppCompatActivity {
NDb mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new NDb(this);
ListView listview;
// listview
listview = (ListView) findViewById(R.id.listview);
String[] array = new String[] {mydb.name, mydb.date};
int[] display = new int[] {R.id.title, R.id.date};
Cursor c = mydb.fetchAll();
MyListAdapter adapter = new MyListAdapter(this, listtemplate, c, array, display, 0);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", (int)l);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("ajdi", (int)l);
editor.apply();
Intent intent = new Intent(MainActivity.this, NoteDisplay.class);
intent.putExtras(dataBundle);
MainActivity.this.startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.add_note:
Bundle dataBundle = new Bundle();
dataBundle.putInt("id", 0);
Intent intent = new Intent(MainActivity.this,NoteDisplay.class);
intent.putExtras(dataBundle);
MainActivity.this.startActivity(intent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public class MyListAdapter extends SimpleCursorAdapter{
private LayoutInflater mInflater;
private Context context;
private int layout;
public MyListAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags){
super(context, layout, c, from, to, flags);
this.context = context;
this.layout = layout;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup par){
ViewHolder holder;
if(convertView==null){
mInflater = LayoutInflater.from(getBaseContext());
convertView = mInflater.inflate(R.layout.listtemplate, null);
holder = new ViewHolder();
holder.btn = (ImageButton) convertView.findViewById(R.id.delete_note);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
int l = sp.getInt("ajdi", 0);
if (l != 0) {
mydb.deleteNotes(l);
Toast.makeText(MainActivity.this, "Deleted", Toast.LENGTH_SHORT).show();
finish();
startActivity(getIntent());
}
}
});
convertView.setTag(holder);
}else{
holder= (ViewHolder) convertView.getTag();
}
return convertView;
}
}
public class ViewHolder{
ImageButton btn;
TextView title;
TextView date;
}
}
이것은 내 데이터베이스 Java 파일입니다. 사람이 관심을 가질만한 경우
package com.example.android.notepad2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by DelicAnte on 04.10.2017..
*/
public class NDb extends SQLiteOpenHelper {
public static final String dbname = "mydb";
public static final String mynotes = "mynotes";
public static final String _id = "_id";
public static final String name = "name";
public static final String content = "content";
public static final String date = "date";
SQLiteDatabase db;
public NDb(Context context){
super(context,dbname,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table mynotes"
+"(_id integer primary key, name text, content text, date text)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS" + mynotes);
onCreate(db);
}
public boolean insertNotes(String name, String content, String date){
db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("content", content);
contentValues.put("date", date);
db.insert(mynotes, null, contentValues);
return true;
}
public Cursor fetchAll(){
db = this.getReadableDatabase();
Cursor mCursor = db.query(mynotes, new String[] {"_id", "name", "content", "date"}, null, null, null, null, null);
if(mCursor!=null){
mCursor.moveToFirst();
}
return mCursor;
}
public Integer deleteNotes(Integer id){
db = this.getWritableDatabase();
return db.delete(mynotes, "_id=?", new String[] {Integer.toString(id)});
}
public Cursor getData(int id){
db = this.getReadableDatabase();
Cursor c = db.rawQuery("select * from " + mynotes +" where _id=" + id + "", null);
return c;
}
}
이
내 listtemplate.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/title"
android:textSize="22sp"
android:layout_weight="1"
android:paddingBottom="15dp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/date"
android:textSize="14sp"
android:layout_weight="0.5"
/>
<ImageButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:src="@drawable/delete_note"
android:layout_weight="0.5"
android:focusable="false"
android:id="@+id/delete_note"
/>
<!-- android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"-->
</LinearLayout>
</LinearLayout>