Android 数组适配器getview仅显示最后一个值
我是android新手,我正在使用array adapter实现自定义listview,在我的arraylist中我有10个不同的值,但当我将此值设置为listview时,它只显示最后一个视图。我非常困惑,请澄清我的疑问Android 数组适配器getview仅显示最后一个值,android,Android,我是android新手,我正在使用array adapter实现自定义listview,在我的arraylist中我有10个不同的值,但当我将此值设置为listview时,它只显示最后一个视图。我非常困惑,请澄清我的疑问 for (int i = 0; i<a ; i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); messageObject = new AllMessageObject();
for (int i = 0; i<a ; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
messageObject = new AllMessageObject();
messageObject.setFrom_name(jsonObject.getString(FROM_NAME));
System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME));
messageObject.setMessage(jsonObject.getString(MESSAGE));
System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE));
messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE));
System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE));
arrayOfList.add(messageObject);
}
这是我的阵列适配器
public class GetAllmessageAdapter extends ArrayAdapter<AllMessageObject>{
public List<AllMessageObject> marrayOfList;
public Context context;
AllMessageObject messageObject;
int row;
public GetAllmessageAdapter(Context context, int textViewResourceId,
List<AllMessageObject> arrayOfList) {
super(context, textViewResourceId,arrayOfList);
this.marrayOfList = arrayOfList;
this.context=context;
this.row=textViewResourceId;
}
public Context getContext() {
return context;
}
public int getCount(){
return this.marrayOfList.size();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// return super.getView(position, convertView, parent);
System.out.println("in get view");
ViewHolder holder;
if(convertView==null){
LayoutInflater Inflator = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView=Inflator.inflate(row, null);
holder = new ViewHolder();
convertView.setTag(holder);
}else
holder = (ViewHolder) convertView.getTag();
messageObject=marrayOfList.get(position);
holder.user_name = (TextView) convertView.findViewById(R.id.textView1);
holder.message = (TextView) convertView.findViewById(R.id.textView2);
holder.from_name = (TextView) convertView.findViewById(R.id.textView4);
holder.user_img=(ImageView) convertView.findViewById(R.id.imageview1);
if(holder.message!=null && messageObject.getMessage().length()>0){
holder.message.setText(messageObject.getMessage());
}
if(holder.from_name!=null && messageObject.getFrom_name().length()>0){
holder.from_name.setText(messageObject.getFrom_name());
}
// holder.user_img.setImageResource(R.drawable.fb);
holder.user_name.setText(messageObject.getMessage_date());
return convertView;
}
/*private view holder class*/
public class ViewHolder {
ImageView user_img;
TextView user_name;
TextView message,from_name;
}
for (int i = 0; i<a ; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
messageObject = new AllMessageObject();
messageObject.setFrom_name(jsonObject.getString(FROM_NAME));
System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME));
messageObject.setMessage(jsonObject.getString(MESSAGE));
System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE));
messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE));
System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE));
arrayOfList.add(messageObject);
}
公共类GetAllmessageAdapter扩展了ArrayAdapter{
公开结婚名单;
公共语境;
所有消息对象消息对象;
int行;
public GetAllmessageAdapter(上下文,int textViewResourceId,
列表(列表列表){
超级(上下文、textViewResourceId、arrayOfList);
this.marrayoff列表=arrayOfList;
this.context=context;
this.row=textViewResourceId;
}
公共上下文getContext(){
返回上下文;
}
public int getCount(){
返回此.marrayOfList.size();
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
//返回super.getView(position、convertView、parent);
System.out.println(“获取视图中”);
视窗座;
if(convertView==null){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(活动.布局\充气机\服务);
convertView=充气机。充气(世界其他地区,空);
holder=新的ViewHolder();
convertView.setTag(支架);
}否则
holder=(ViewHolder)convertView.getTag();
messageObject=marrayOfList.get(位置);
holder.user_name=(TextView)convertView.findViewById(R.id.textView1);
holder.message=(TextView)convertView.findViewById(R.id.textView2);
holder.from_name=(TextView)convertView.findViewById(R.id.textView4);
holder.user_img=(ImageView)convertView.findViewById(R.id.imageview1);
if(holder.message!=null&&messageObject.getMessage().length()>0){
holder.message.setText(messageObject.getMessage());
}
if(holder.from_name!=null&&messageObject.getFrom_name().length()>0){
holder.from_name.setText(messageObject.getFrom_name());
}
//holder.user\u img.setImageResource(R.drawable.fb);
holder.user_name.setText(messageObject.getMessage_date());
返回视图;
}
/*私有视图持有者类*/
公共类视图持有者{
ImageView用户界面;
TextView用户名;
TextView消息,来自_name;
}
以下是我如何从我的活动中解脱出来
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
System.out.println("background");
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
// Use this to add parameters
request.addProperty("UserId",
"fd83ca17-41f5-472a-8efe-7a975d83ed9b");
request.addProperty("FacebookId", "100006693371290");
request.addPropertyIfValue("count", "6");
// Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
SoapObject soap_result = (SoapObject) envelope.bodyIn;
System.out.println("soap_result " + soap_result);
result = soap_result.getProperty(0).toString();
System.out.println("result in message activity " + result);
JSONArray jsonArray = new JSONArray(result);
int a=jsonArray.length();
System.out.println("a "+a);
for (int i = 0; i<a ; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
messageObject = new AllMessageObject();
messageObject.setFrom_name(jsonObject.getString(FROM_NAME));
System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME));
messageObject.setMessage(jsonObject.getString(MESSAGE));
System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE));
messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE));
System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE));
}
arrayOfList.add(messageObject);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
System.out.println("on post");
adapter=new GetAllmessageAdapter(Woosuite_Message.this, R.layout.list_row, arrayOfList);
listView.setAdapter(adapter);
}
for (int i = 0; i<a ; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
messageObject = new AllMessageObject();
messageObject.setFrom_name(jsonObject.getString(FROM_NAME));
System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME));
messageObject.setMessage(jsonObject.getString(MESSAGE));
System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE));
messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE));
System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE));
arrayOfList.add(messageObject);
}
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
系统输出打印项次(“背景”);
SoapObject请求=新的SoapObject(名称空间,方法\名称1);
//使用此选项可以添加参数
request.addProperty(“UserId”,
“fd83ca17-41f5-472a-8efe-7a975d83ed9b”);
请求。addProperty(“FacebookId”,“100006693371290”);
请求。addPropertyIfValue(“计数”,“6”);
//声明SOAP请求的版本
SoapSerializationEnvelope=新的SoapSerializationEnvelope(
第11版);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
androidHttpTransport.debug=true;
//这是将调用webservice的实际部分
调用(SOAP_ACTION1,信封);
SoapObject soap_result=(SoapObject)envelope.bodyIn;
System.out.println(“soap\u结果”+soap\u结果);
result=soap_result.getProperty(0).toString();
System.out.println(“导致消息活动”+结果);
JSONArray JSONArray=新JSONArray(结果);
int a=jsonArray.length();
系统输出打印项次(“a”+a);
对于(int i=0;i首先,您将消息对象添加到for循环外部的arrayOfList中。因此,它将只执行一次。尝试将该行放入for循环内部,以获得添加到arraylist的所有值
for (int i = 0; i<a ; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
messageObject = new AllMessageObject();
messageObject.setFrom_name(jsonObject.getString(FROM_NAME));
System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME));
messageObject.setMessage(jsonObject.getString(MESSAGE));
System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE));
messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE));
System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE));
arrayOfList.add(messageObject);
}
对于(int i=0;i请像这样更改您的try块
for (int i = 0; i<a ; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
messageObject = new AllMessageObject();
messageObject.setFrom_name(jsonObject.getString(FROM_NAME));
System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME));
messageObject.setMessage(jsonObject.getString(MESSAGE));
System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE));
messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE));
System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE));
arrayOfList.add(messageObject);
}
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
// this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1, envelope);
SoapObject soap_result = (SoapObject) envelope.bodyIn;
System.out.println("soap_result " + soap_result);
result = soap_result.getProperty(0).toString();
System.out.println("result in message activity " + result);
JSONArray jsonArray = new JSONArray(result);
int a=jsonArray.length();
System.out.println("a "+a);
for (int i = 0; i<a ; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
messageObject = new AllMessageObject();
messageObject.setFrom_name(jsonObject.getString(FROM_NAME));
System.out.println("jsonObject.getString(FROM_NAME) "+i+" "+jsonObject.getString(FROM_NAME));
messageObject.setMessage(jsonObject.getString(MESSAGE));
System.out.println("jsonObject.getString(MESSAGE) "+i+" "+jsonObject.getString(MESSAGE));
messageObject.setMessage_date(jsonObject.getString(MESSAGE_DATE));
System.out.println("jsonObject.getString(MESSAGE_DATE) "+i+" "+jsonObject.getString(MESSAGE_DATE));
arrayOfList.add(messageObject);
}
} catch (Exception e) {
e.printStackTrace();
}
试试看{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
androidHttpTransport.debug=true;
//这是将调用webservice的实际部分
调用(SOAP_ACTION1,信封);
SoapObject soap_result=(SoapObject)envelope.bodyIn;
System.out.println(“soap\u结果”+soap\u结果);
result=soap_result.getProperty(0).toString();
System.out.println(“导致消息活动”+结果);
JSONArray JSONArray=新JSONArray(结果);
int a=jsonArray.length();
系统输出打印项次(“a”+a);
对于(int i=0;我知道您已将messageObject添加到循环之外。这将仅将最后一个元素添加到arrayOfList。很棒的nanba…非常感谢我浪费了一个小时在getview中查看…再次感谢