Android 删除项目后,ListView中的剩余项目位置不正确
删除ListView中的项目后,我在获取正确的项目时遇到问题。 该位置应适合ArrayList中项目的索引,但在ListView中删除项目后,该位置不正确(甚至可能导致indexoutofbounds异常) 我打赌这与视图位置有关,但我就是找不到错误 我有一个带有内部BaseAdapter类的ListActivity类:Android 删除项目后,ListView中的剩余项目位置不正确,android,listview,view,position,Android,Listview,View,Position,删除ListView中的项目后,我在获取正确的项目时遇到问题。 该位置应适合ArrayList中项目的索引,但在ListView中删除项目后,该位置不正确(甚至可能导致indexoutofbounds异常) 我打赌这与视图位置有关,但我就是找不到错误 我有一个带有内部BaseAdapter类的ListActivity类: public class MSMobilMyStocksActivity extends ListActivity { static MyStocksCtr m
public class MSMobilMyStocksActivity extends ListActivity {
static MyStocksCtr myStocksCtr;
static ListView listview;
static EfficientAdapter adap;
boolean downloadSuccess;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mystockslist);
mContext = this;
listview = getListView();
myStocksCtr = MyStocksCtr.getInstance(mContext);
adap = new EfficientAdapter(this);
setListAdapter(adap);
if (isOnline())
new InsertDataTask().execute();
else {
startTimer();
Toast.makeText(mContext, "Du har ingen internetforbindelse", 1000)
.show();
}
}
public static boolean updateData() {
return myStocksCtr.downloadJson();
}
private class InsertDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
try {
listview.setEnabled(false);
} catch (Exception e) {
}
}
// can use UI thread here
@Override
protected void onPostExecute(final Void unused) {
if (isOnline()) {
if (downloadSuccess) {
try {
if (myStocksCtr.getArray().size() == 0) {
Toast.makeText(mContext, "Ingen data hentet", 2000)
.show();
} else if (myStocksCtr.getArray().size() == 1
&& myStocksCtr.getArray().get(0)
.getString("FH_FULLNAME").equals("-")) {
Toast.makeText(mContext, "Ingen data hentet", 2000)
.show();
} else {
adap.notifyDataSetChanged();
}
} catch (JSONException e) {
Toast.makeText(mContext, "JSONException", 2000).show();
}
}
} else
Toast.makeText(mContext, "Du har ingen internetforbindelse",
1000).show();
startTimer();
try {
listview.setEnabled(true);
} catch (Exception e) {
}
}
@Override
protected Void doInBackground(Void... arg0) {
downloadSuccess = false;
if (isOnline()) {
try {
if (updateData())
downloadSuccess = true;
} catch (Exception e) {
Toast.makeText(mContext, "Fejl under download af data",
2000).show();
downloadSuccess = false;
}
} else
Toast.makeText(mContext, "Du har ingen internetforbindelse",
1000).show();
return null;
}
}
}
public static class EfficientAdapter extends BaseAdapter implements
Filterable {
private LayoutInflater mInflater;
private Context context;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.mylistitem, null);
holder = new ViewHolder();
holder.stockName = (TextView) convertView
.findViewById(R.id.stockName1);
holder.stockPrice = (TextView) convertView
.findViewById(R.id.stockValue1);
holder.stockChange = (TextView) convertView
.findViewById(R.id.stockChange1);
holder.stockPctChange = (TextView) convertView
.findViewById(R.id.stockPctChange1);
convertView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent i = new Intent(mContext, MSStockDetailActivity.class);
Bundle b = new Bundle();
b.putInt("position", position); //Your id
b.putBoolean("isMyStocks", true);
i.putExtras(b); //Put your id to your next Intent
mContext.startActivity(i);
} catch (Exception e) {
Toast.makeText(mContext, "OnClick error ", 2000)
.show();
}
}
});
convertView.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
final CharSequence[] items = {
"See stock details",
"Delete the stock \"" + holder.stockName.getText()
+ "\" from the list", "Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(
mContext);
builder.setTitle("Configure list");
builder.setItems(items,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int item) {
if (item == 1) {
myStocksCtr.removeStock(holder.ric);
adap.notifyDataSetChanged();
} else if (item == 2) {
}
Toast.makeText(mContext, items[item],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
return true;
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try {
String result = myStocksCtr.getArray().get(position)
.getString("FH_FULLNAME");
holder.stockName.setText(result);
} catch (JSONException e) {
e.printStackTrace();
}
try {
String result = myStocksCtr.getArray().get(position)
.getString("FH_RIC");
holder.ric = result;
} catch (JSONException e) {
e.printStackTrace();
}
// + String.valueOf(position));
try {
String result = myStocksCtr.getArray().get(position)
.getString("FH_PRC");
if (String.valueOf(result.charAt(result.length() - 2)).equals(
".")) {
result += "0";
}
;
holder.stockPrice.setText(result);
} catch (JSONException e) {
e.printStackTrace();
}
holder.position = position;
return convertView;
}
static class ViewHolder {
String ric;
TextView stockName;
TextView stockPrice;
TextView stockPctChange;
TextView stockChange;
int position;
}
@Override
public Filter getFilter() {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getCount() {
return myStocksCtr.getArray().size();
}
@Override
public JSONObject getItem(int position) {
return myStocksCtr.getArray().get(position);
}
}
公共类MSMobilMyStocksActivity扩展了ListActivity{
静态MyStocksCtr MyStocksCtr;
静态列表视图列表视图;
静态效率adap;
布尔下载成功;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mystockslist);
mContext=这个;
listview=getListView();
myStocksCtr=myStocksCtr.getInstance(mContext);
adap=新效率适配器(本);
setListAdapter(adap);
if(isOnline())
新建InsertDataTask().execute();
否则{
startTimer();
Toast.makeText(McContext,“Du har ingen internetforbindelse”,1000)
.show();
}
}
公共静态布尔更新数据(){
返回myStocksCtr.downloadJson();
}
私有类InsertDataTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
试一试{
setEnabled(false);
}捕获(例外e){
}
}
//可以在这里使用UI线程
@凌驾
后期执行时受保护的无效(最终无效未使用){
if(isOnline()){
如果(下载成功){
试一试{
if(myStocksCtr.getArray().size()==0){
Toast.makeText(McContext,“Ingen data hentet”,2000年)
.show();
}如果(myStocksCtr.getArray().size()==1
&&myStocksCtr.getArray().get(0)
.getString(“FH_全名”).equals(“-”){
Toast.makeText(McContext,“Ingen data hentet”,2000年)
.show();
}否则{
adap.notifyDataSetChanged();
}
}捕获(JSONException e){
makeText(mContext,“JSONException”,2000).show();
}
}
}否则
Toast.makeText(McContext,“Du har ingen internetforbindelse”,
1000)show();
startTimer();
试一试{
setEnabled(true);
}捕获(例外e){
}
}
@凌驾
受保护的Void doInBackground(Void…arg0){
downloadSuccess=false;
if(isOnline()){
试一试{
if(updateData())
downloadSuccess=true;
}捕获(例外e){
makeText(mContext,“下载af数据下的Fejl”,
2000)show();
downloadSuccess=false;
}
}否则
Toast.makeText(McContext,“Du har ingen internetforbindelse”,
1000)show();
返回null;
}
}
}
公共静态类EfficientAdapter扩展BaseAdapter实现
可过滤{
私人停车场;
私人语境;
公共效率适配器(上下文){
mInflater=LayoutInflater.from(上下文);
this.context=上下文;
}
@凌驾
公共视图getView(最终整型位置,视图转换视图,
视图组(父级){
最终持票人;
if(convertView==null){
convertView=mInflater.inflate(R.layout.mylistitem,null);
holder=新的ViewHolder();
holder.stockName=(TextView)convertView
.findviewbyd(R.id.stockName1);
holder.stockPrice=(TextView)convertView
.findViewById(R.id.stockValue1);
holder.stockChange=(TextView)convertView
.findViewById(R.id.stockChange1);
holder.stockPctChange=(TextView)convertView
.findViewById(R.id.stockPctChange1);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
试一试{
意图i=新意图(mContext,MSStockDetailActivity.class);
Bundle b=新Bundle();
b、 putInt(“position”,position);//您的id
b、 putBoolean(“isMyStocks”,真);
i、 putExtras(b);//将您的id用于下一个目的
mContext.startActivity(i);
}捕获(例外e){
Toast.makeText(mContext,“OnClick错误”,2000年)
.show();
}
}
});
setOnLongClickListener(新的OnLongClickListener(){
@凌驾
仅长按公共布尔值(视图arg0){
最终字符序列[]项={
“见股票详情”,
“删除股票\”“+holder.stockName.getText()
+列表中的“\”、“取消”};
AlertDialog.Builder builde
public class MyStocksCtr {
private static MyStocksCtr INSTANCE = null;
ArrayList<String> myStocksArray;
JSONCtr jsonCtr;
static Context mContext;
boolean isRunning = false;
ArrayList<JSONObject> myArray;
MyStocksCtr(Context mContext) {
this.mContext = mContext;
jsonCtr = new JSONCtr(mContext);
myStocksArray = new ArrayList<String>();
myArray = new ArrayList<JSONObject>();
}
}
public static MyStocksCtr getInstance(Context context){
mContext = context;
if(INSTANCE == null) {
INSTANCE = new MyStocksCtr(mContext);
}
return INSTANCE;
}
public ArrayList<JSONObject> getArray() {
return myArray;
}
public boolean downloadJson() {
if (!myStocksArray.isEmpty()) {
String myStocksString = "";
for (String s : myStocksArray)
myStocksString += (s + "%2C");
myStocksString = myStocksString.substring(0,
myStocksString.length() - 3);
jsonCtr.getJSON(preStocksUrl + myStocksString + postStocksUrl);
myArray.clear();
myArray.addAll(jsonCtr.getArray());
return true;
} else {
return false;
}
}
public void removeStock(String newStock) {
JSONObject toRemove = null;
for (JSONObject obj : myArray) {
try {
if (obj.getString("FH_RIC").equals(newStock)) {
toRemove = obj;
}
} catch (JSONException e) {
Toast.makeText(mContext, "Kunne ikke fjerne objekt", 1000)
.show();
}
}
if (toRemove != null){
myStocksArray.remove(newStock);
jsonCtr.removeRic(toRemove);
myArray = jsonCtr.getArray();
saveMyStocks();
}
}
}
public ArrayList<JSONObject> getArray() {
return rics1;
}
public void removeRic(Object o){
rics1.remove(o);
}
jsonCtr.removeRic(toRemove);
myArray = jsonCtr.getArray();