自定义Simplecursoradapter不会填充列表视图
问题描述:
SimpleCustomAdapter工作和填充我的ListView,但后来我想在ListView中实现一个删除ImageButton。为了实现这一点,我必须制作一个自定义适配器。现在它不会在ListView中显示数据。自定义Simplecursoradapter不会填充列表视图
这是我的MainActivity.java,我也放了MyListAdapter类(在底部)。我是一名初学者,所以我意识到代码可能遍布全球。 MainAcitivty已经调用适配器,一个用于listview的setonclicklistener,它也在sharedpreferences中发送item的id,所以我可以在适配器的deleteNotes中捕获它。此外,删除按钮工作得很好,我可以删除笔记,但唯一的问题是,标题和日期不显示在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>
答
如果有人可能会感兴趣。我通过在mainactivity.java中添加adapter.bindView来解决此问题。在该方法中,您为标题,内容和最后一个按钮setonclicklistener设置文本。