gpt4 book ai didi

android - 从 ListView 中删除项目

转载 作者:搜寻专家 更新时间:2023-11-01 08:16:06 25 4
gpt4 key购买 nike

如果我想通过点击从我的 ListView 中删除一个项目,我是从我使用 ArrayAdapter 的 ArrayList 中删除该对象,还是直接从适配器中删除该对象并调用 notifyDatasetChange?

另外我想把列表中的数据存到磁盘上,我是保存adapter还是我用的arraylist?

这是代码:我的想法是保存列表中的数据并在 Activity 再次启动时将其加载回来,但是一旦发生这种情况,我就无法再从列表中删除项目。

public class EditKeywords extends ListActivity implements Serializable{

private ArrayList<String> keyWordList;
private EditText keywordEditText;
private IconicAdapter adapter;
private Button enterButton;
private int position;
private Button finishedButton;
public static final String KEYWORDS = "keywords";
public static final String ADAPTER = "adapter";


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editkeyowords);
getKeywordsAndAdapter();
keywordEditText = (EditText) findViewById(R.id.keywordedittext);
setListAdapter(adapter);

enterButton = (Button) findViewById(R.id.enterkeywords);
enterButton.setOnClickListener(enterButtonListener);

finishedButton = (Button) findViewById(R.id.finishedbutton);
finishedButton.setOnClickListener(finishedButtonListener);
}

public void onResume(){
super.onResume();
getKeywordsAndAdapter();
}

public void onPause(Bundle savedInstanceState){
super.onPause();
writeKeywordsAndAdapter();
}


public void getKeywordsAndAdapter(){
try {
InputStream fi = openFileInput(KEYWORDS);

if (fi!=null) {
ObjectInputStream in = new ObjectInputStream(fi);
keyWordList = (ArrayList<String>) in.readObject();
in.close();
}
}
catch (java.io.FileNotFoundException e) {
// that's OK, we probably haven't created it yet
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
.show();
}
if(keyWordList == null){
keyWordList = new ArrayList<String>();
}

try {
InputStream fi = openFileInput(ADAPTER);

if (fi!=null) {
ObjectInputStream in = new ObjectInputStream(fi);
adapter = (IconicAdapter) in.readObject();
in.close();
}
}
catch (java.io.FileNotFoundException e) {
// that's OK, we probably haven't created it yet
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
.show();
}
if(adapter == null){
adapter = new IconicAdapter();
}
}

public void writeKeywordsAndAdapter(){

try {
OutputStream fi = openFileOutput(KEYWORDS, 0);

if (fi!=null) {
ObjectOutputStream out = new ObjectOutputStream(fi);
out.writeObject(keyWordList);
out.close();
}
}
catch (java.io.FileNotFoundException e) {
// that's OK, we probably haven't created it yet
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
.show();
}

try {
OutputStream fi = openFileOutput(ADAPTER, 0);

if (fi!=null) {
ObjectOutputStream out = new ObjectOutputStream(fi);
out.writeObject(adapter);
out.close();
}
}
catch (java.io.FileNotFoundException e) {
// that's OK, we probably haven't created it yet
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)
.show();
}


}

public void onListItemClick(ListView parent, View v,
int aPosition, long id) {

ImageView image = (ImageView) findViewById(R.id.icon1);
image.setImageResource(R.drawable.delete);
image.setOnClickListener(deleteImageListener);
getListView().getChildAt(aPosition).invalidate();
//adapter.notifyDataSetInvalidated();
position = aPosition;
}

public void startHome() {
Intent intent = new Intent(this, ECS.class);
this.startActivity(intent);
finish();
}

public class IconicAdapter extends ArrayAdapter<String> implements Serializable {
private static final long serialVersionUID = 6175078429973952022L;


IconicAdapter() {
super(EditKeywords.this, R.layout.rowkeywords, keyWordList);
}


public View getView(int position, View convertView,ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();

View row=inflater.inflate(R.layout.rowkeywords, parent, false);
TextView label=(TextView)row.findViewById(R.id.label1);

label.setText(keyWordList.get(position));
return(row);
}
}

private OnClickListener enterButtonListener = new OnClickListener() {

@Override
public void onClick(View arg0){

if(keywordEditText.getText().toString() != ""){
boolean unique = true;
for(String s : keyWordList){
if(s.equals(keywordEditText.getText().toString())){
unique = false;
}
}
if(unique == true){
adapter.add(keywordEditText.getText().toString());
keywordEditText.setText("");
adapter.notifyDataSetChanged();
}
}
}
};

private OnClickListener deleteImageListener = new OnClickListener() {

@Override
public void onClick(View arg0){
//keyWordList.remove(position);
adapter.remove(keyWordList.get(0));
adapter.notifyDataSetChanged();
writeKeywordsAndAdapter();


}
};

private OnClickListener finishedButtonListener = new OnClickListener() {

@Override
public void onClick(View arg0){
writeKeywordsAndAdapter();
startHome();
}
};

}

最佳答案

你必须将它从你的 arrayList 中移除然后设置:adapter.notifyDataSetChanged();

关于android - 从 ListView 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4763954/

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