我知道讨论这个问题很多,但我的问题还没有解决。按照标题,ListView不会刷新代码
notifyDataSetChanged();
这是来自main.activity
verticalButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent;
intent = new Intent(getApplicationContext(),verticalActivity.class);
intent.putExtra("table","slide_vertical");
intent.putExtra("pathConf","vertical");
startActivity(intent);
}
});
这适用于verticalActivity.class
public class verticalActivity extends Activity {
private String tableActivity;
private String pathConf;
listAdapter adapter;
ArrayList<listActivity> items;
@Override
public void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setContentView(R.layout.slidevertical);
Intent intent = getIntent();
String thisNewIntent = intent.getStringExtra("table");
String thisNewPathConf = intent.getStringExtra("pathConf");
this.tableActivity = thisNewIntent;
this.pathConf = thisNewPathConf;
DatabaseAdapter db = new DatabaseAdapter(getApplicationContext());
try {
db.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
db.openDataBase();
this.items = db.getItem(thisNewIntent);
final ListView obj = (ListView)findViewById(R.id.listvertical);
adapter = new listAdapter(this,items,tableActivity);
obj.setAdapter(adapter);
db.close();
obj.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = obj.getItemAtPosition(position);
listActivity getObject = (listActivity) o;
Intent intent = new Intent(getApplicationContext(), viewItemAct.class);
intent.putExtra("type", "image");
intent.putExtra("data", getObject.getPath());
startActivity(intent);
//Toast.makeText(verticalActivity.this,"Data = "+getObject.getPath(),Toast.LENGTH_LONG).show();
}
});
}
public void refreshList(){
adapter.notifyDataSetChanged();
}
}
对于listAdapter.activity
public class listAdapter extends BaseAdapter {
//private Context context;
private ArrayList<listActivity> itemList;
private LayoutInflater layoutInflater;
private String tableName;
Context myContext;
private Activity activity;
public listAdapter(Context context,ArrayList<listActivity> results,String tableName){
this.itemList = results;
layoutInflater = LayoutInflater.from(context);
this.tableName = tableName;
this.myContext = context;
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int position) {
return itemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
//ViewGroup p = parent;
if(convertView==null){
//layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.listitem,null,false);
holder = new ViewHolder();
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.location = (TextView)convertView.findViewById(R.id.location);
holder.date = (TextView)convertView.findViewById(R.id.date);
holder.idItem = (TextView)convertView.findViewById(R.id.iditem);
holder.listImage = (ImageView)convertView.findViewById(R.id.list_image);
holder.buttonDelete = (Button)convertView.findViewById(R.id.delButton);
holder.buttonDelete.setFocusable(false);
holder.buttonDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String idString = holder.idItem.getText().toString();
DatabaseAdapter db = new DatabaseAdapter(myContext);
try {
db.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
db.openDataBase();
db.deleteData(idString,tableName);
((verticalActivity)activity).refreshList();
}
});
convertView.setTag(holder);
}
else{
holder = (ViewHolder)convertView.getTag();
}
int id_ = itemList.get(position).getId();
//String id__ = toString(id_);
holder.idItem.setText(String.valueOf(id_));
Uri uri = Uri.parse(itemList.get(position).getPath());
holder.listImage.setImageURI(uri);
String[] splitLocation = itemList.get(position).getPath().split("/");
String nameFile = splitLocation[splitLocation.length-1];
holder.title.setText(nameFile);
holder.location.setText(itemList.get(position).getPath());
holder.date.setText(itemList.get(position).getDate());
return convertView;
}
static class ViewHolder {
TextView title;
TextView location;
TextView date;
TextView idItem;
ImageView listImage;
Button buttonDelete;
}
}
从上面的代码中有一个按钮来执行从数据库中删除数据,因为从数据库中删除数据已经可以完成但是为了刷新ListView总是失败。如果我的代码有问题?有人能帮助我吗?
感谢您的帮助。对于麻烦我有一个小问题的解决方案。我只是刷新ArrayList&lt;&gt;中的项目列表。
public void refreshList(){
DatabaseAdapter db = new DatabaseAdapter(getApplicationContext());
try {
db.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
db.openDataBase();
items.clear();
items.addAll(db.getItem(tableActivity));
adapter.notifyDataSetChanged();
}
非常感谢:)
答案 0 :(得分:1)
在listAdapter构造函数中,Contect传递活动
public listAdapter(Activity context,ArrayList<listActivity> results,String tableName){
this.itemList = results;
layoutInflater = LayoutInflater.from(context);
this.tableName = tableName;
this.activity = context;
this.myContext = activity.getApplicationContext();
}
并在getView()方法中设置Button Id
holder.buttonDelete.setId(position);
按钮的onClick()放置以下代码
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
System.out.println("Deleted Id "+v.getId());
itemList.remove(v.getId());
System.out.println("Refresh the list Item ");
notifyDataSetChanged();
}
});
这将删除您的项目。如果你想要不同的机制来处理你的列表视图
答案 1 :(得分:0)
你有一个类变量活动活动,你永远不会在任何地方初始化它...要调用方法刷新列表,你需要在通过适配器构造函数传递的上下文中调用它... < / p>
((verticalActivity)context).refreshList();
答案 2 :(得分:0)
您只是从db中删除而不是从itemList中删除。
使用itemList.remove(position)