Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android自定义阵列适配器问题_Android - Fatal编程技术网

Android自定义阵列适配器问题

Android自定义阵列适配器问题,android,Android,我是android新手。我在android上有一个应用程序,其中我从.Net Web服务返回一些数据(名称和id),在返回这些数据后,我使用适配器在列表视图中设置所有这些名称。我已经通过调试器检查了数组列表是否从WEB服务获取数据,但android SDK中的LISTVIEW并没有显示任何结果 我的代码是: import java.util.List; import android.app.Activity; import android.content.Context; import and

我是android新手。我在android上有一个应用程序,其中我从.Net Web服务返回一些数据(名称和id),在返回这些数据后,我使用适配器在列表视图中设置所有这些名称。我已经通过调试器检查了数组列表是否从WEB服务获取数据,但android SDK中的LISTVIEW并没有显示任何结果

我的代码是:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<MyValues>{
Context context; 
int layoutResourceId;    
List data = null;

public MyArrayAdapter(Context context, int layoutResourceId, List data) {
       super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder view;

        if(rowView == null)
        {
            // Get a new instance of the row layout view
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            rowView = inflater.inflate(R.layout.multirow, null);

            // Hold the view objects in an object, that way the don't need to be "re-  finded"
            view = new ViewHolder();
            view.name= (TextView) rowView.findViewById(R.id.rowTextView);
            view.address= (TextView) rowView.findViewById(R.id.secondLine);

            rowView.setTag(view);
        } else {
            view = (ViewHolder) rowView.getTag();
        }

        /** Set data to your Views. */
        MyValues item =(MyValues) data.get(position); //List.get(position);
        view.name.setText(item.getName());
        view.address.setText(item.getAddress());

        return rowView;
    }

    protected static class ViewHolder{
        protected TextView name;
        protected TextView address;
    }


}
public class MyValues {
private String name;
private String address;

public MyValues(String name, String address) {
    this.name = name;
    this.address = address;
}
public void setName(String name) {
    this.name= name;
}
public String getName() {
    return name;
}
public void setAddress(String address) {
    this.address= address;
}
public String getAddress() {
    return address;
}
}
 import java.util.ArrayList;

 import java.util.HashMap;
 import java.util.List;
 import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;


import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class SimpleListViewActivity extends Activity {

private static String SOAP_ACTION_D = "http://tempuri.org/GetSalesMen";
private static String NAMESPACE = "http://tempuri.org/";
 private static String METHOD_NAME_D = "GetSalesMen";
private static String URL_D = "http://apollon.dnet.gr/webservice/Service1.asmx";
 /** Called when the activity is first created. */
 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

SoapObject table = null;                
SoapObject tableRow = null;     
SoapObject responseBody = null;    

 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_D);        

SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);
envelope.dotNet = true;

try {
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_D);

    List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
    headerList.add(new HeaderProperty("Authorization", "Basic " +   org.kobjects.base64.Base64.encode("administrator:MyPassword".getBytes())));

    androidHttpTransport.call(SOAP_ACTION_D, envelope, headerList);

    SoapObject result = (SoapObject)envelope.bodyIn;
    int intPropertyCount = result.getPropertyCount();
    {
        responseBody = (SoapObject) envelope.getResponse();

        responseBody = (SoapObject) responseBody.getProperty(1);

        table = (SoapObject) responseBody.getProperty(0);
        ArrayList<MyValues> list = new ArrayList<MyValues>();
        SoapObject responseChild = (SoapObject) result.getProperty(0);
        int lengthOfResponseChild = responseChild.getPropertyCount();

        for (int j = 0; j < lengthOfResponseChild; j++){
            tableRow  = (SoapObject) table.getProperty(j);
            list.add(new MyValues(tableRow.getProperty(2).toString(),      tableRow.getProperty(1).toString()));

        }  
        ListView lv = (ListView) findViewById(R.id.mainListView);
        MyArrayAdapter adapter = new MyArrayAdapter(this,R.id.mainListView, list);
        lv.setAdapter(adapter);

    }
} catch (Exception e) {
    e.printStackTrace();
}

 }

    public class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

}
我的活动类别代码是:

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MyArrayAdapter extends ArrayAdapter<MyValues>{
Context context; 
int layoutResourceId;    
List data = null;

public MyArrayAdapter(Context context, int layoutResourceId, List data) {
       super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ViewHolder view;

        if(rowView == null)
        {
            // Get a new instance of the row layout view
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            rowView = inflater.inflate(R.layout.multirow, null);

            // Hold the view objects in an object, that way the don't need to be "re-  finded"
            view = new ViewHolder();
            view.name= (TextView) rowView.findViewById(R.id.rowTextView);
            view.address= (TextView) rowView.findViewById(R.id.secondLine);

            rowView.setTag(view);
        } else {
            view = (ViewHolder) rowView.getTag();
        }

        /** Set data to your Views. */
        MyValues item =(MyValues) data.get(position); //List.get(position);
        view.name.setText(item.getName());
        view.address.setText(item.getAddress());

        return rowView;
    }

    protected static class ViewHolder{
        protected TextView name;
        protected TextView address;
    }


}
public class MyValues {
private String name;
private String address;

public MyValues(String name, String address) {
    this.name = name;
    this.address = address;
}
public void setName(String name) {
    this.name= name;
}
public String getName() {
    return name;
}
public void setAddress(String address) {
    this.address= address;
}
public String getAddress() {
    return address;
}
}
 import java.util.ArrayList;

 import java.util.HashMap;
 import java.util.List;
 import android.app.Activity;
 import android.content.Context;
 import android.os.Bundle;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;


import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;

import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class SimpleListViewActivity extends Activity {

private static String SOAP_ACTION_D = "http://tempuri.org/GetSalesMen";
private static String NAMESPACE = "http://tempuri.org/";
 private static String METHOD_NAME_D = "GetSalesMen";
private static String URL_D = "http://apollon.dnet.gr/webservice/Service1.asmx";
 /** Called when the activity is first created. */
 @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

SoapObject table = null;                
SoapObject tableRow = null;     
SoapObject responseBody = null;    

 SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_D);        

SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);
envelope.dotNet = true;

try {
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_D);

    List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
    headerList.add(new HeaderProperty("Authorization", "Basic " +   org.kobjects.base64.Base64.encode("administrator:MyPassword".getBytes())));

    androidHttpTransport.call(SOAP_ACTION_D, envelope, headerList);

    SoapObject result = (SoapObject)envelope.bodyIn;
    int intPropertyCount = result.getPropertyCount();
    {
        responseBody = (SoapObject) envelope.getResponse();

        responseBody = (SoapObject) responseBody.getProperty(1);

        table = (SoapObject) responseBody.getProperty(0);
        ArrayList<MyValues> list = new ArrayList<MyValues>();
        SoapObject responseChild = (SoapObject) result.getProperty(0);
        int lengthOfResponseChild = responseChild.getPropertyCount();

        for (int j = 0; j < lengthOfResponseChild; j++){
            tableRow  = (SoapObject) table.getProperty(j);
            list.add(new MyValues(tableRow.getProperty(2).toString(),      tableRow.getProperty(1).toString()));

        }  
        ListView lv = (ListView) findViewById(R.id.mainListView);
        MyArrayAdapter adapter = new MyArrayAdapter(this,R.id.mainListView, list);
        lv.setAdapter(adapter);

    }
} catch (Exception e) {
    e.printStackTrace();
}

 }

    public class StableArrayAdapter extends ArrayAdapter<String> {

    HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

    public StableArrayAdapter(Context context, int textViewResourceId,
        List<String> objects) {
      super(context, textViewResourceId, objects);
      for (int i = 0; i < objects.size(); ++i) {
        mIdMap.put(objects.get(i), i);
      }
    }

    @Override
    public long getItemId(int position) {
      String item = getItem(position);
      return mIdMap.get(item);
    }

    @Override
    public boolean hasStableIds() {
      return true;
    }

  }

}
import java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.os.Bundle;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入org.ksoap2.HeaderProperty;
导入org.ksoap2.SoapEnvelope;
导入org.ksoap2.serialization.SoapObject;
导入org.ksoap2.serialization.SoapSerializationEnvelope;
导入org.ksoap2.transport.HttpTransportSE;
公共类SimpleListViewActivity扩展活动{
私有静态字符串SOAP\u ACTION\u D=”http://tempuri.org/GetSalesMen";
私有静态字符串命名空间=”http://tempuri.org/";
私有静态字符串方法\u NAME\u D=“GetSalesMen”;
专用静态字符串URL_D=”http://apollon.dnet.gr/webservice/Service1.asmx";
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SoapObject表=null;
SoapObject tableRow=null;
SoapObject responseBody=null;
SoapObject请求=新的SoapObject(名称空间,方法名称);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(请求);
envelope.dotNet=true;
试一试{
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL\u D);
列表标题列表=新的ArrayList();
添加(新的HeaderProperty(“授权”,“基本”+org.kobjects.base64.base64.encode(“管理员:MyPassword.getBytes())));
调用(SOAP操作、信封、标题列表);
SoapObject结果=(SoapObject)envelope.bodyIn;
int intPropertyCount=result.getPropertyCount();
{
responseBody=(SoapObject)envelope.getResponse();
responseBody=(SoapObject)responseBody.getProperty(1);
表=(SoapObject)ResponseBy.getProperty(0);
ArrayList=新建ArrayList();
SoapObject responseChild=(SoapObject)结果.getProperty(0);
int lengthOfResponseChild=responseChild.getPropertyCount();
对于(int j=0;j

有人能帮我吗。谢谢在适配器类中实现
getCount
方法:

public class MyArrayAdapter extends ArrayAdapter<MyValues>{
   //...Your current codes here
   @Override
   public int getCount() {
       if(data == null) {
          return 0;
       } else { 
          return data.size();
       }
   }
}
公共类MyArrayAdapter扩展了ArrayAdapter{
//…您当前的代码在这里
@凌驾
public int getCount(){
如果(数据==null){
返回0;
}否则{
返回data.size();
}
}
}

顺便说一句,您的代码中未使用StableArrayAdapter
。(我在您的共享代码中没有看到任何引用。)如果与此无关,请将其从此处删除。

您正在UI线程上执行所有操作。查找异步任务或线程。这可能会导致出现一个无响应窗口,在该窗口中,如果按wait,您仍然可以对其进行测试,但它不正确,请在编写异步任务或线程之前仅将其用于测试

StableArrayAdapter不在任何地方使用,对吗;最好清理你的代码

您可以记录或调试
列表的属性
解析后是否有任何数据


MyArrayAdapter提到的代码似乎很好。

定义
工作不正常
您应该编辑您的问题,并提供一些关于“工作不正常”的细节。没有人会猜到问题出在哪里,发布一些logcat输出,详细描述出现了什么问题,以及预期会发生什么。能否为R.layout.main和R.layout.multirow添加布局。。你的问题可能就在那里