我正在创建一个新的ListView
,它将从用户输入中获取其值。此方法工作正常,但删除项目时遇到了问题。
我为此OnItemLongClickListener
设置了ListView
,以便获得从列表中删除项目的正确索引。但是它删除了错误的索引项。这意味着如果我长时间单击第一个项目,那么我想删除该项目,但是每次它从Listview
中删除最后一个项目时(Toast显示正确的索引)。
请参阅源代码以获取更多详细信息。查看照片和源代码。
package com.example.powerbase.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
ListView listView;
TextView hTxt,pTxt;
BaseAdapter baseAdapter;
ArrayList<Item> inputValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputValues=new ArrayList<Item>();
editText=(EditText)findViewById(R.id.et);
button=(Button)findViewById(R.id.btn);
listView=(ListView)findViewById(R.id.lv);
hTxt=(TextView)findViewById(R.id.hTxt);
pTxt=(TextView)findViewById(R.id.pTxt);
baseAdapter=new BaseAdapter() {
@Override
public int getCount() {
return inputValues.size();
}
@Override
public Object getItem(int i) {
return inputValues.get(i);
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view==null){
LayoutInflater inflater= (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.custom_view,null);
TextView headerText=(TextView)view.findViewById(R.id.hTxt);
TextView pragraphText=(TextView)view.findViewById(R.id.pTxt);
headerText.setText(inputValues.get(i).getHeader());
pragraphText.setText(inputValues.get(i).getPragraph());
}
return view;
}
};
listView.setAdapter(baseAdapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s=editText.getText().toString();
Item item=new Item(s,s);
inputValues.add(item);
editText.setText("");
baseAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "New Item Added", Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
inputValues.remove(i);
baseAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, String.valueOf(i), Toast.LENGTH_SHORT).show();
return false;
}
});
}}
Item.java文件的源代码如下:
package com.example.powerbase.myapplication;
public class Item {
String header;
String pragraph;
public Item(String header, String pragraph){
super();
this.header=header;
this.pragraph=pragraph;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getPragraph() {
return pragraph;
}
public void setPragraph(String pragraph) {
this.pragraph = pragraph;
}
}