gpt4 book ai didi

java - 安卓 : Refresh ListView not working

转载 作者:行者123 更新时间:2023-12-01 13:12:21 25 4
gpt4 key购买 nike

我知道这个问题有很多讨论,但我的问题还没有解决。根据标题,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总是失败。如果我的代码有问题?有人可以帮助我吗?

Thank's for your help guys. For the trouble I have a litle solution. I'm just refresh list for item from ArrayList<>.

公共(public)无效刷新列表(){
DatabaseAdapter db = new DatabaseAdapter(getApplicationContext());
尝试 {
db.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
db.openDataBase();
items.clear();
items.addAll(db.getItem(tableActivity));
适配器.notifyDataSetChanged();
}

Thank you very much :)

最佳答案

在 Contect 的 listAdapter 构造函数中传递 Activity

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() 方法中设置按钮 ID

 holder.buttonDelete.setId(position);

Button的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();
}
});

这将删除您的项目。如果你想要不同的机制来处理你的 ListView

click Here!

关于java - 安卓 : Refresh ListView not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22750177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com