Java 如何使用两个数组填充自定义列表。

Java 如何使用两个数组填充自定义列表。,java,android,listview,adapter,getter-setter,Java,Android,Listview,Adapter,Getter Setter,我创建了一个自定义适配器类和一个名为Bean的getter和setter类。这是为了创建一个包含文本视图和图像的列表视图 如何填充myList,使其在设置适配器时可用,从而将数组中相应的文本和图像显示到listView中 我已经为我的适配器和bean类以及我的主活动类的异步任务提供了代码,但是问题出在我的异步类的onPostExecute方法中 我只是想澄清一下。此代码未经测试,因此未返回任何错误。我的问题是如何使用字符串数组“descriptionArray”和“photoArray”中的信息

我创建了一个自定义适配器类和一个名为Bean的getter和setter类。这是为了创建一个包含文本视图和图像的列表视图

如何填充myList,使其在设置适配器时可用,从而将数组中相应的文本和图像显示到listView中

我已经为我的适配器和bean类以及我的主活动类的异步任务提供了代码,但是问题出在我的异步类的onPostExecute方法中

我只是想澄清一下。此代码未经测试,因此未返回任何错误。我的问题是如何使用字符串数组“descriptionArray”和“photoArray”中的信息填充onPostExecute方法中的myList。

我的主活动类的异步任务

    private class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {

        String content = HttpManager.getData(params[0]);
        return content;
    }




//-----------------------THIS IS WHERE THE ISSUE IS HAPPENING---------------------------
    @Override
    protected void onPostExecute(String result) {
        hideDialog();
        String parseResult = InfoJSONResultParser.parseFeed(result);

        importerArray = OrderInformationParser.orderParser(result);

        if (parseResult.equals("ok")) {
            //Returns the Array with the JSON info already parsed.
            List<Bean> myList = new ArrayList<>(); //<---***How to populate this***




//***With  the information from these two String arrays.***
            String[] descriptionArray = OrderInformationParser.orderParser(result);
            String[] photoArray = PhotoParser.photoParser(result);


            //This creates and executes the list
            list = (ListView)findViewById(R.id.orderListView);


            //***So i can then transfer over to this adapter***
            MyAdapter adapter = new MyAdapter(MainActivity.this, myList);
            list.setAdapter(adapter);


        } else {
            findViewById(R.id.nullOrders).setVisibility(View.VISIBLE);
        }
    }

}

您可以通过迭代2个数组来填充列表,并将字符串添加到Bean对象中

例如:

 private List<Bean> populateBeanList(List<Bean> myList, String[] descArray, String[] photoArray){

     for(int i=0; i< descArray.length; i++){

      Bean bean = new Bean();
      bean.setText(descArray[i]);
      bean.setUrl(photoArray[i]);
      myList.Add(bean);
     }

   return myList;
} 
私有列表populateBeanList(列表myList,字符串[]descArray,字符串[]photoArray){
对于(int i=0;i
然后在异步类中调用该函数

    private class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {

        String content = HttpManager.getData(params[0]);
        return content;
    }




//-----------------------THIS IS WHERE THE ISSUE IS HAPPENING---------------------------
    @Override
    protected void onPostExecute(String result) {
        hideDialog();
        String parseResult = InfoJSONResultParser.parseFeed(result);

        importerArray = OrderInformationParser.orderParser(result);

        if (parseResult.equals("ok")) {
            //Returns the Array with the JSON info already parsed.
            List<Bean> myList = new ArrayList<>(); //<---***How to populate this***




//***With  the information from these two String arrays.***
            String[] descriptionArray = OrderInformationParser.orderParser(result);
            String[] photoArray = PhotoParser.photoParser(result);

        myList = populateBeanList(myList,descriptionArray, photoArray);

        //This creates and executes the list
        list = (ListView)findViewById(R.id.orderListView);


        //***So i can then transfer over to this adapter***
        MyAdapter adapter = new MyAdapter(MainActivity.this, myList);
        list.setAdapter(adapter);


    } else {
        findViewById(R.id.nullOrders).setVisibility(View.VISIBLE);
    }
}
私有类MyTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
String content=HttpManager.getData(参数[0]);
返回内容;
}
//-----------------------这就是问题所在---------------------------
@凌驾
受保护的void onPostExecute(字符串结果){
hideDialog();
字符串parseResult=InfoJSONResultParser.parseFeed(结果);
importerArray=OrderInformationParser.orderParser(结果);
if(parseResult.equals(“ok”)){
//返回已解析JSON信息的数组。

List myList=new ArrayList();//您可以通过迭代2个数组并将字符串添加到Bean对象来填充列表

例如:

 private List<Bean> populateBeanList(List<Bean> myList, String[] descArray, String[] photoArray){

     for(int i=0; i< descArray.length; i++){

      Bean bean = new Bean();
      bean.setText(descArray[i]);
      bean.setUrl(photoArray[i]);
      myList.Add(bean);
     }

   return myList;
} 
私有列表populateBeanList(列表myList,字符串[]descArray,字符串[]photoArray){
对于(int i=0;i
然后在异步类中调用该函数

    private class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {

        String content = HttpManager.getData(params[0]);
        return content;
    }




//-----------------------THIS IS WHERE THE ISSUE IS HAPPENING---------------------------
    @Override
    protected void onPostExecute(String result) {
        hideDialog();
        String parseResult = InfoJSONResultParser.parseFeed(result);

        importerArray = OrderInformationParser.orderParser(result);

        if (parseResult.equals("ok")) {
            //Returns the Array with the JSON info already parsed.
            List<Bean> myList = new ArrayList<>(); //<---***How to populate this***




//***With  the information from these two String arrays.***
            String[] descriptionArray = OrderInformationParser.orderParser(result);
            String[] photoArray = PhotoParser.photoParser(result);

        myList = populateBeanList(myList,descriptionArray, photoArray);

        //This creates and executes the list
        list = (ListView)findViewById(R.id.orderListView);


        //***So i can then transfer over to this adapter***
        MyAdapter adapter = new MyAdapter(MainActivity.this, myList);
        list.setAdapter(adapter);


    } else {
        findViewById(R.id.nullOrders).setVisibility(View.VISIBLE);
    }
}
私有类MyTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
String content=HttpManager.getData(参数[0]);
返回内容;
}
//-----------------------这就是问题所在---------------------------
@凌驾
受保护的void onPostExecute(字符串结果){
hideDialog();
字符串parseResult=InfoJSONResultParser.parseFeed(结果);
importerArray=OrderInformationParser.orderParser(结果);
if(parseResult.equals(“ok”)){
//返回已解析JSON信息的数组。

List myList=new ArrayList();//根据我对您的问题的理解,您不知道如何填充列表

好吧,这是一项简单的任务。让我们假设
descriptionArray
包含文本,而
photoArray
包含URL

所需的代码是:

首先,为方便起见,将构造函数添加到
Bean
类中:

public Bean(String text, String url) {
    this.text = text;
    this.url = url;
}
它只需添加一个非空构造函数,这样我们就可以在创建实例时直接初始化类字段

其次,调用
ArrayList
的方法
add

myList.add(new Bean(text, url))
显然,您需要将其放入for循环(对于数组的每一项,您都会插入一个新的
Bean

for (int i=0; i<descriptionArray.lenght; i++) {
    myList.add(new Bean(descriptionArray[i], photoArray[i]);
}
也许是时候在发布之前在谷歌上搜索一下这个话题了;)


希望它能帮上一点忙。从我对您的问题的理解来看,您不知道如何填充列表

好吧,这是一项简单的任务。让我们假设
descriptionArray
包含文本,而
photoArray
包含URL

所需的代码是:

首先,为方便起见,将构造函数添加到
Bean
类中:

public Bean(String text, String url) {
    this.text = text;
    this.url = url;
}
它只需添加一个非空构造函数,这样我们就可以在创建实例时直接初始化类字段

其次,调用
ArrayList
的方法
add

myList.add(new Bean(text, url))
显然,您需要将其放入for循环(对于数组的每一项,您都会插入一个新的
Bean

for (int i=0; i<descriptionArray.lenght; i++) {
    myList.add(new Bean(descriptionArray[i], photoArray[i]);
}
也许是时候在发布之前在谷歌上搜索一下这个话题了;)


希望对您有所帮助

问题出在哪里?是否引发异常?请详细说明您的issue@rsella它没有引发异常。我只是不知道如何用字符串数组中的信息填充myList。有什么问题吗?它是否引发异常?请更具体地说明您的issue@rsella这不是throwing一个例外。我只是不知道如何用字符串数组中的信息填充myList。虽然填充列表的最佳方法是使用ie:GSON,Jackson将Web响应序列化到列表中。这正是我最后所做的,它使用文本部分工作,所以谢谢!我想我一定是在做wro我的适配器类中出现了ng,因为我的图像仍然没有显示。只有文本视图。你知道你的适配器发生了什么吗?我没有。我已经做了一段时间了,但即使使用调试器,也很难准确地知道发生了什么。我肯定它在适配器中。我可能不得不离开毕加索……也许吧这不是listview使用的最佳方式。我发现了!它甚至不在我的适配器中,我在设置图像URL时犯了一个愚蠢的错误。感谢您的帮助!虽然填充列表的最佳方式是