listview with arraylist,android中的简单适配器

时间:2013-09-05 06:02:57

标签: 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, 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”。 什么应该是更有效的方式来做到这一点。我是一个初学者,所以你可以建议一个更好的方式,我应该遵循

5 个答案:

答案 0 :(得分:10)

我给你的建议是创建一个扩展适配器(或它的一些子类)的单独类

以下是String数组适配器的一个简单示例。

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>,然后从那里开始。

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

希望这有帮助!

答案 1 :(得分:7)

<强> main.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>

<强> setlanguage.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);

答案 2 :(得分:2)

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

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

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

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

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

SimpleAdapter关于它的内部复杂性可能更简单,但实际使用它绝对不简单。使用ArrayAdapter,您只需将项目列表传递给它,然后让它自动生成视图。只要你的内存不足,它就可以是你需要它的任何大小。 (参见下面的例子)

开始使用自定义适配器后,我强烈建议您观看Romain Guy & Adam Powell's I/O talk。在学习时需要考虑很多,但他们在解释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);

答案 3 :(得分: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);
    }

}

答案 4 :(得分:-1)

  

无论你面对什么问题我都面对被问到的问题&#34;列表   查看显示数组数据的最后位置...&#34;

     

使用哈希地图生成问题

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循环变量中使用哈希映射&#39; b&#39;创建每个&amp;每次被视为不同的对象。然后,简单的数组列表显示不同的哈希映射对象。

     

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