在 Android 中动态创建 Listview [英] Creating Listview dynamically in Android

查看:21
本文介绍了在 Android 中动态创建 Listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个二维字符串 array,

我想创建一个 3 列的列表视图,用于显示字符串数组中的数据,它的行数应与字符串数组的大小相同.

这是我的字符串数组:

String[][] 数据 = {{001",约翰",1001"},{"002", "SIBIN", "1002" },{"003", "TOM", "1003" },{004",自由",1004"}};

我想在 Listview 中显示它

ID NAME 空缺 ID001 约翰 1001002 思宾 1002003 汤姆 1003004 弗里迪 1004

此外,当我单击一个列表视图项时,它会重定向到另一个具有列表视图 ID 的页面

这怎么可能?我是android开发新手,只知道基础知识,只知道如何创建listview.

解决方案

为此,您首先需要一个自定义列表视图,您应该在 main.xml(只是一个 eg)文件中添加一个列表视图,然后创建一个这样的类

>

public class MySimpleArrayAdapter extends ArrayAdapter;{私有最终上下文上下文;私有最终 String[] 值;数据助手 dh;公共 MySimpleArrayAdapter(上下文上下文,int textViewResourceId,String[] 值){超级(上下文,textViewResourceId,值);this.context = 上下文;this.values = 值;dh=new DataHelper(getApplicationContext());}@覆盖public View getView(final int position, View convertView, ViewGroup parent) {LayoutInflater inflater = (LayoutInflater) 上下文.getSystemService(Context.LAYOUT_INFLATER_SERVICE);查看 rowView = inflater.inflate(R.layout.list_name, parent, false);textView = (TextView) rowView.findViewById(R.id.textname);textView.setText(values[位置]);//更改 Windows 和 iPhone 的图标textView.setOnClickListener(new View.OnClickListener() {公共无效 onClick(查看 v){Toast.makeText(this,""+values[position],10000).show();}});返回行视图;}

R.layout.list_name 这将是将内容加载到列表视图的新 xml 文件

最后一步就在你的 on create 方法中

con = (ListView) findViewById(R.id.main_listView);MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(MainActivity.this, R.id.main_listView ,data);//data 为列表视图中要添加的 String 数组值//设置适配器con.setAdapter(适配器);

I Have a two dimensional string array,

I want to create a 3 column list-view, that display data from the string array, it should have the same number of rows that the size of the string array.

This is my string array:

String[][] data = {
   {"001", "JOHN ","1001" },
   {"002", "SIBIN", "1002" },
   {"003", "TOM ",   "1003" },
   {"004", "FREEDY", "1004" }
   };

I want to display it in a Listview like

ID    NAME   VACANCY ID
001   JOHN    1001
002   SIBIN   1002
003   TOM     1003
004   FREEDY  1004

Also when I click a list-view item, it redirect to another page with the listview id

How is this possible? I am new in the android development, I know only the basics, and I only know how to create listview.

解决方案

for that you need a customize list view first you should add a listview in your main.xml(just an eg) file then create a class like this

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
      private final Context context;
      private final String[] values;
      DataHelper dh;

      public MySimpleArrayAdapter(Context context, int textViewResourceId, String[] values) {
        super(context, textViewResourceId, values);
        this.context = context;
        this.values = values;

        dh=new DataHelper(getApplicationContext());
      }

      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_name, parent, false);
       textView = (TextView) rowView.findViewById(R.id.textname);

        textView.setText(values[position]);
        // Change the icon for Windows and iPhone
        textView.setOnClickListener(new View.OnClickListener() {
               public void onClick(View v) 
               {
                   Toast.makeText(this,""+values[position],10000).show();
               }
               });


        return rowView;
} 

R.layout.list_name this will be the new xml file which will load the contents to the list view

and the the final step just in your on create method do this

con = (ListView) findViewById(R.id.main_listView); 
 MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(MainActivity.this,  R.id.main_listView ,data);// data is String array valu to be added in list view
    //setting the adapter
    con.setAdapter(adapter);

这篇关于在 Android 中动态创建 Listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆