带有arraylist的listview,android中的简单适配器

带有arraylist的listview,android中的简单适配器,android,listview,android-listview,arraylist,Android,Listview,Android Listview,Arraylist,我尝试使用arraylist和简单适配器在listview中显示一些内容。 我尝试了下面的方法,但结果显示了arraylist的姓氏。 我不明白我怎么了 final ListView listView = (ListView) findViewById(R.id.mylist); ArrayList<HashMap<String, String>> list_of_bookmarks = new ArrayList<HashMap<String, S

我尝试使用arraylist和简单适配器在listview中显示一些内容。 我尝试了下面的方法,但结果显示了arraylist的姓氏。 我不明白我怎么了

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);
final ListView ListView=(ListView)findviewbyd(R.id.mylist);
ArrayList list_of_bookmarks=新建ArrayList();
HashMap b=新的HashMap();
字符串[]from={“php_key”、“c_key”、“android_key”、“hacking_key”};
String[]name_of_bookmarks={“php”、“c”、“android”、“hacking”};

对于(inti=0;i您在int[]对象中重用相同的视图

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

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

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

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

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

一旦你开始使用自定义适配器,我强烈推荐你观看。学习时需要了解很多内容,但它们在解释
listview
如何工作方面做得很好

//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);
//项目列表
String[]name_of_bookmarks={“php”、“c”、“android”、“hacking”};
//为ArrayAdapter创建列表对象
//并使其大小与书籍的名称相同
List listBookmarks=newarraylist(Array.getLength(name_of_bookmarks));
//将书签内容的名称添加到listBookmarks
Collections.addAll(列表书签、书籍名称);
//创建一个ArrayAdapter,将上下文、通用列表项和列表传递给它
//“this”的另一个选项是主活动中的“getApplicationContext()”
//或片段中的“getActivity()”。不建议使用“getBaseContext()”。
ArrayAdapter ArrayAdapter=新的ArrayAdapter(此,R.layout.list\u item\u text,listBookmarks);
//将适配器设置为ListView
最终ListView ListView=(ListView)findViewById(R.id.mylist);
setAdapter(arrayAdapter);
试试这个

  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);
    }

}
公共类MyFragment扩展了ListFragment{
字符串[]from={“php_key”、“c_key”、“android_key”、“hacking_key”};
String[]name_of_bookmarks={“php”、“c”、“android”、“hacking”};
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
列表=新的ArrayList();
for(int i=0;i
我的建议是创建一个单独的类来扩展适配器(或它的某个子类)

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

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;
    }
}
package apc.example;
导入java.util.ArrayList;
导入utils.BitmapManager;
导入android.app.Activity;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类PersonalAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
ArrayList数据=null;
公共角色适配器(上下文、内部布局、资源ID、,
ArrayList数据){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图行=转换视图;
ItemHolder=null;
if(行==null){
LayoutInflater充气器=((活动)上下文)。getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
holder=新的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=(ItemHolder)row.getTag();
}
personbean=data.get(位置);
holder.txtName.setText(bean.getName());
holder.txtDescription.setText(bean.getDescription());
位图b=BitmapFactory.decodeResource(context.getResources(),R.drawable.user);
bitmappmanager.INSTANCE.setPlaceholder(b);
bitmappmanager.INSTANCE.loadBitmap(bean.getUrl(),holder.imgIcon,80,80);
返回行;
}
公共静态类ItemHolder{
公共图像视图imgIcon;
TextView-txtName;
TextView txtDescription;
}
公共无效更新适配器(ArrayList pers){
这个数据=pers;
}
}
这是一个更复杂类的适配器示例,该类具有更多字段,而不是简单字符串。但是可以很容易地将其修改为
ArrayAdapter
,然后从那里开始

无论如何,我认为为ListView编写自定义适配器始终是一种最佳实践

<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 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>
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);
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);