我是android的新手,我正在使用数组适配器实现自定义列表视图,在我的arraylist中我有10个不同的值但是我将这个值设置为listview它只显示最后一个view.i非常混乱请清除我的怀疑
这是我的数组适配器
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;
}
以下是我从活动中传递的方式
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);
}
谢谢
答案 0 :(得分:0)
首先,您要将 messageObject 添加到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);
}
答案 1 :(得分:0)
请更改您的试用版,
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();
}