android listview Adapter复用问题

孤单盖伦 2015-09-22 01:23:42
listview在滑动的时候复用 导致我的list全部不对了,不知道该怎么赋值了,求大神解答!



上代码:


package hg.eht.com.ecarehg;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.graphics.ColorUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import factory.JSONExchange;
import ui.ImageFactory;


public class Ecare_HG_ServiceObjectList extends Activity {
private ArrayList<String> mListName;
private ArrayList<String> mListSex;
private ArrayList<String> mListAge;
private ArrayList<String> mListAbility;
private ArrayList<String> mListAddress;
ListView mListView = null;
MyListAdapter myAdapter = null;
Ecare_HG_ServiceObjectList arrayList = null;
ImageButton buttonHead;
JSONExchange jsonExchange = new JSONExchange();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setBackgroundDrawableResource(R.color.background_gary);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_ecare_hg_serviceobject_head);
//arrayList = this;
//mListView = getListView();
Intent intent =getIntent();
jsonExchange.Message=intent.getStringExtra("jsonList");
UpadteJsonToList();
mListView = (ListView) findViewById(R.id.list_view);
myAdapter = new MyListAdapter(this, R.layout.activity_ecare_hg_serviceobject_item);
mListView.setAdapter(myAdapter);
//Headͷ
/*LayoutInflater lif = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//View headerView = lif.inflate(R.layout.activity_ecare_hg_map_search_head, null);
View headerView = lif.inflate(R.layout.activity_ecare_hg_serviceobject_head,mListView, false);
mListView.addHeaderView(headerView);
myAdapter = new MyListAdapter(this,R.layout.activity_ecare_hg_serviceobject_item);
setListAdapter(myAdapter);
this.getListView().setBackgroundResource(R.color.background_gary);
LayoutParams layoutParams = this.getListView().getLayoutParams();
layoutParams.width = LayoutParams.MATCH_PARENT;
layoutParams.height = LayoutParams.MATCH_PARENT;*/
//this.getListView().setLayoutParams(layoutParams);
//this.getListView().setDivider(new ColorDrawable(Color.parseColor("#ecebf0")));
mListView.setDividerHeight(ImageFactory.dip2px(getApplicationContext(), 10));
findViewById(R.id.btn_agreement).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Ecare_HG_ServiceObjectList.this, Ecare_HG_ServiceObjectEdit.class);
intent.putExtra("isAdd", "false");
startActivityForResult(intent, 0);
}
});
findViewById(R.id.back_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Ecare_HG_ServiceObjectList.this.finish();
}
});
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("name", mListName.get(arg2));
setResult(RESULT_OK, intent);
finish();
}

});

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case RESULT_OK:
Bundle b=data.getExtras(); //data为B中回传的Intent
jsonExchange.Message=b.getString("jsonList");//str即为回传的值
UpadteJsonToList();
myAdapter.notifyDataSetChanged();
break;
default:
break;
}
}

private void UpadteJsonToList(){
try{
mListName = new ArrayList<String>();
mListSex = new ArrayList<String>();
mListAge = new ArrayList<String>();
mListAbility = new ArrayList<String>();
mListAddress = new ArrayList<String>();
JSONObject resultObj = new JSONObject(jsonExchange.Message);
JSONArray resultList= new JSONArray(resultObj.get("result").toString());
for(int i = 0;i<resultList.length();i++){
JSONObject temp = (JSONObject) resultList.get(i);
mListName.add(temp.getString("fullName"));
switch (Integer.parseInt(temp.getString("sex"))){
case 0:
mListSex.add("女士");
break;
case 1:
mListSex.add("男士");
break;

}
if(!temp.getString("age").equals("null")&&temp.getString("age").length()>0){
mListAge.add(temp.getString("age")+"岁");
}else {
mListAge.add("");
}
if(!temp.getString("address").equals("null")&&temp.getString("address").length()>0){
mListAddress.add(temp.getString("address"));
}else {
mListAddress.add("");
}
switch (Integer.parseInt(temp.getString("incapableStatus"))){
case 0:
mListAbility.add("全自理");
break;
case 1:
mListAbility.add("半自理");
break;
case 2:
mListAbility.add("不能自理");
break;

}
}
}catch (Exception ex){}
}

public class MyListAdapter extends ArrayAdapter<Object> {
int mTextViewResourceID = 0;
private Context mContext;
public MyListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mTextViewResourceID = textViewResourceId;
mContext = context;
}

//private int[] colors = new int[] { 0xff626569, 0xff4f5257 };

public int getCount() {
return mListName.size();
}

@Override
public boolean areAllItemsEnabled() {
return false;
}

public Object getItem(int position) {
return position;
}

public long getItemId(int position) {
return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
TextView tv_title = null;
TextView tv_age = null;
TextView tv_ability = null;
TextView tv_address = null;
RelativeLayout btn_edit = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
mTextViewResourceID, null);
btn_edit = (RelativeLayout) convertView.findViewById(R.id.btn_edit);
btn_edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Ecare_HG_ServiceObjectList.this, Ecare_HG_ServiceObjectEdit.class);
intent.putExtra("isAdd","true");
startActivity(intent);
}
});
tv_title = (TextView) convertView.findViewById(R.id.object_title);
tv_title.setText(mListName.get(position)+" "+mListSex.get(position));
tv_age = (TextView) convertView.findViewById(R.id.object_age_value);
tv_age.setText(mListAge.get(position));
tv_ability = (TextView) convertView.findViewById(R.id.object_ability_value);
tv_ability.setText(mListAbility.get(position));
tv_address = (TextView) convertView.findViewById(R.id.object_address_value);
tv_address.setText(mListAddress.get(position));
convertView.setMinimumHeight(ImageFactory.dip2px(getApplicationContext(), 80));
}

return convertView;
}
}
}
...全文
237 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧