11

我尝试使用 arraylist 和简单的适配器在 listview 中显示一些东西。我尝试了类似下面的方法,但在我的结果中显示了数组列表的姓氏。我怎么了,我无法理解。

final ListView listView = (ListView) findViewById(R.id.mylist);

    ArrayList<HashMap<String, String>> list_of_bookmarks = new ArrayList<HashMap<String, String>>();

        HashMap<String, String> b = new HashMap<String, String>();

        String[] from = { "php_key","c_key","android_key","hacking_key" };
        String[] name_of_bookmarks = { "php","c","android","hacking" };

            for(int i=0;i<4;i++)
            {
              b.put(from[i],name_of_bookmarks[i]);   
              list_of_bookmarks.add(b);
            }

         };

            int[] to = { R.id.txt1,R.id.txt1,R.id.txt1,R.id.txt1};

            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_bookmarks, R.layout.list_layout, from, to);         
            listView.setAdapter(adapter);

我只想在列表视图中显示“php”、“c”、“android”、“hacking”。什么应该是更有效的方法来做到这一点。我是一个初学者,所以你可以建议我应该遵循的更好的方法

4

5 回答 5

10

我对您的建议是创建一个单独的类来扩展适配器(或它的某个子类)

这是一个字符串数组适配器的简单示例。

package ro.gebs.captoom.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import antistatic.spinnerwheel.adapters.AbstractWheelTextAdapter;

import com.example.captoom.R;

public class LanguagesAdapter extends AbstractWheelTextAdapter {
    // Countries names
    private String languages[];

    public LanguagesAdapter(Context context) {
        super(context, R.layout.lang_item, NO_RESOURCE);
        languages = context.getResources().getStringArray(R.array.lang_array);
        setItemTextResource(R.id.language_txt);
    }

    @Override
    public View getItem(int index, View cachedView, ViewGroup parent) {
        View view = super.getItem(index, cachedView, parent);
        return view;
    }

    @Override
    public int getItemsCount() {
        return languages.length;
    }

    @Override
    protected CharSequence getItemText(int index) {
        return languages[index];
    }
}

用法很简单,只要使用方法.setAdapter();

或者另一个使用 arrayAdapter 的例子:

package apc.example;

import java.util.ArrayList;

import utils.BitmapManager;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PersonAdapter extends ArrayAdapter<Person> {

    Context context;
    int layoutResourceId;
    ArrayList<Person> data = null;

    public PersonAdapter(Context context, int layoutResourceId,
            ArrayList<Person> 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 row = convertView;
        ItemHolder holder = null;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ItemHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.icon);
            holder.txtName = (TextView) row.findViewById(R.id.title);
            holder.txtDescription = (TextView) row.findViewById(R.id.desc);

            row.setTag(holder);
        } else {
            holder = (ItemHolder) row.getTag();
        }

        Person bean = data.get(position);
        holder.txtName.setText(bean.getName());
        holder.txtDescription.setText(bean.getDescription());


        Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.user);
        BitmapManager.INSTANCE.setPlaceholder(b);
        BitmapManager.INSTANCE.loadBitmap(bean.getUrl(), holder.imgIcon, 80, 80);

        return row;
    }

    public static class ItemHolder {
        public ImageView imgIcon;
        TextView txtName;
        TextView txtDescription;
    }

    public void updateAdapter(ArrayList<Person> pers){
        this.data = pers;
    }
}

这是具有更多字段而不是简单字符串的更复杂类的适配器示例。但这可以很容易地修改到ArrayAdapter<String>然后从那里开始。

无论如何,我认为为列表视图编写自定义适配器始终是最佳实践。

希望这可以帮助!

于 2013-09-05T06:16:18.627 回答
7

主.xml

<LinearLayout  

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dp"   >


    <ListView
        android:id="@+id/zone_list"
        android:layout_marginBottom="70dp"
        android:background="@drawable/batteryborder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

设置语言.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="18dp"
        android:gravity="center_vertical" />

</LinearLayout>

添加活动文件的onCreate()

ListView listView;

String[] from = { "php_key","c_key","android_key","hacking_key" };

ArrayAdapter arrayAdapter;

listView = (ListView) findViewById(R.id.zone_list); 

arrayAdapter = new ArrayAdapter<>(this,R.layout.setlanguage, R.id.tvName, from);

listView.setAdapter(arrayAdapter);
于 2013-09-05T07:05:40.667 回答
3

您在 int[] 对象中重用相同的视图。

int[] to = { R.id.txt1,R.id.txt1,R.id.txt1,R.id.txt1};

看起来它将它们都视为同一个对象,因此每次添加新项目时都会更改以前的项目。

为了使用,SimpleAdapter您需要在 XML 中定义具有不同 ID 的每个视图。

int[] to = { R.id.txt1,R.id.txt2,R.id.txt3,R.id.txt4};

就其SimpleAdapter内部复杂性而言,它可能更简单,但实际使用绝对不会更简单。使用 anArrayAdapter您可以将项目列表传递给它并让它自动生成视图。只要您不耗尽内存,它可以是您需要的任何大小。(例如见下文)

一旦您开始使用自定义适配器,我强烈建议您观看Romain Guy 和 Adam Powell 的 I/O 谈话。学习时要吸收很多东西,但他们在解释如何ListViews工作方面做得很好。

//List of Items
String[] name_of_bookmarks = { "php","c","android","hacking" };

//Create your List object for the ArrayAdapter
//and make it the same size as name_of_books
List<String> listBookmarks = new ArrayList<String>(Array.getLength(name_of_bookmarks));

//Add name_of_bookmarks contents to listBookmarks
Collections.addAll(listBookmarks, name_of_books);

//Create an ArrayAdapter passing it the Context, a generic list item and your list
//An alternative to "this" would be "getApplicationContext()" from your main activity
//or "getActivity()" from a fragment. "getBaseContext()" is not recommended.
ArrayAdapter arrayAdapter = new ArrayAdapter(this, R.layout.list_item_text, listBookmarks);

//Set the adapter to your ListView
final ListView listView = (ListView) findViewById(R.id.mylist);
listView.setAdapter(arrayAdapter);
于 2013-09-05T06:13:22.743 回答
0

试试这个

  public class MyFragment extends ListFragment{

    String[] from = { "php_key","c_key","android_key","hacking_key" };
    String[] name_of_bookmarks = { "php","c","android","hacking" };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        List<HashMap<String, String>> list= new ArrayList<HashMap<String,String>>();

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

            HashMap<String, String> map= new HashMap<String, String>();
            map.put("key",  name_of_bookmarks[i]);
            list.add(map);
        }
        String[] from = { "key" };

        int[] to = { R.id.txt};

        SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), list, R.layout.list_layout, from, to);
        setListAdapter(adapter);

        return super.onCreateView(inflater, container, savedInstanceState);
    }

}
于 2013-09-05T06:15:31.427 回答
-1

无论您遇到什么问题,我都面临着称为“列表视图显示数组数据的最后位置...”的问题

问题是由哈希映射产生的

final ListView listView = (ListView) findViewById(R.id.mylist);

ArrayList<HashMap<String, String>> list_of_bookmarks = new ArrayList<HashMap<String, String>>();

    String[] from = { "php_key","c_key","android_key","hacking_key" };
    String[] name_of_bookmarks = { "php","c","android","hacking" };

        for(int i=0;i<4;i++)
        { 
          HashMap<String, String> b = new HashMap<String, String>();
          b.put(from[i],name_of_bookmarks[i]);   
          list_of_bookmarks.add(b);
        }

     };

        int[] to = { R.id.txt1,R.id.txt1,R.id.txt1,R.id.txt1};

        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_bookmarks, R.layout.list_layout, from, to);         
        listView.setAdapter(adapter);

如果您有任何疑问,请尝试这个,然后再问我任何问题,您只需在 For 循环中声明您的哈希图...

在每次创建的 For 循环变量“b”中使用哈希映射,并将其视为不同的对象。然后简单的数组列表显示不同的哈希映射对象。

您正在使用相同的对象来存储哈希映射的值,并且该变量被同名覆盖,这就是您面临问题的原因谢谢...

于 2017-07-13T10:28:47.183 回答