简体   繁体   English

如何在 Android 的 ListView Items 中动态添加 TextView?

[英]How to add A TextView Dynamically in the ListView Items Android?

I want to add a TextView and Set it's value dynamically in the ListView and On click of the item i want to get the value of the item clicked .我想添加一个 TextView 并在 ListView 中动态设置它的值,并在单击项目时获得单击项目的值。

I have a array我有一个数组

String[] statesList = {"listItem 1", "listItem 2", "listItem 3"};

My TextView XML is我的TextView XML

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"
    android:padding="10dp" 
    android:textSize="16sp" 
    android:textStyle="bold" 
    >
</TextView>

My ListView XML我的列表视图 XML

 <ListView
            android:id="@+id/list"
            android:choiceMode="singleChoice"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_width="290dp"/>

Please help me how could i set those values in the array to the listview .i am new in android.Thanks请帮助我如何将数组中的这些值设置为列表视图。我是 android 新手。谢谢

Use this code i hope this is what u want使用此代码我希望这是你想要的

public void showstatesList() {
        listView = (ListView) findViewById(R.id.list);
        String[] statesList = {"listItem 1", "listItem 2", "listItem 3"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.simple_list_item_1, android.R.id.text1, statesList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                int itemPosition     = position;
                String  itemValue    = (String) listView.getItemAtPosition(position);

//              Toast.makeText(getApplicationContext(),
//                "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
//                .show();
              }
        });
}

I hope this will help you..我希望这能帮到您..

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView tv;
    ListView lv;
      String s="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         tv=(TextView) findViewById(R.id.textView1);
         lv=(ListView) findViewById(R.id.listView1);

        String value[]={"asda","Ansar","Nisam"};
        ArrayAdapter<String>adapter=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1,android.R.id.text1,value);
        lv.setAdapter(adapter);
//list item click
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                     s=s+("\n")+((String) lv.getItemAtPosition(arg2));
                    tv.setText(s);
                }

            });

    }

To create a ListView is an easy thing.创建 ListView 是一件容易的事情。 All what you do is to create: -XML code : --create a element inside your main XML file , with its size, id attributes :您所做的就是创建:-XML 代码:--在主 XML 文件中创建一个元素,其大小、id 属性:

 <ListView 
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/mainList"

                />

--another XML file , that you wanna make form of your List elements, like the from of your textView etc.. inside 'layout' file : -- 另一个XML 文件,您希望在“布局”文件中制作 List 元素的形式,例如 textView 的 from 等:

 elementsForm.xml : 

if you want to make list of countries for example , so create a LinearLayout that you will put text例如,如果您想制作国家/地区列表,请创建一个用于放置文本的 LinearLayout

form inside with "vertical" value to 'android:orientation' attribute inside it create your TextView :将“垂直”值设置为 'android:orientation' 属性在里面创建你的 TextView :

 <TextView 


   android : layout_width="match_parent"
   android : layout_height="wrap_content"
   android : textSize="25sp"
   android : id="@+id/texty"
    />

INSIDE YOUR JAVA FILE " MainClass " :在您的 JAVA 文件“ MainClass ”中:

--create an inner class and let it inherits from ArrayAdapter and create a constructor that takes 2 Parameters : the first Type is Context , the second Type is Data Type that you wanna display it and pass three values to the super constructor (ArrayAdapter): -- 创建一个内部类并让它继承自 ArrayAdapter 并创建一个带有 2 个参数的构造函数:第一个 Type 是 Context ,第二个 Type 是您想要显示它的 Data Type 并将三个值传递给超级构造函数(ArrayAdapter):

1th : the context that your own class takes第一:你自己的班级的背景

2th : your own XML file that will consist and display the value like text View, audio View etc ... 2th :您自己的 XML 文件将包含并显示文本视图、音频视图等值...

3th : the array of values that we wanna display that our class takes as second parameter of its constructor第三个:我们想要显示的值数组,我们的类将其作为其构造函数的第二个参数

:: ::

public class MyOwnInnerClass extends ArrayAdapter<String>{
public MyOwnInnerClass(Context context, String countries[]){
super(cotext , R.layout. , countries);

inside your class now override the getView() method that takes在您的类中现在覆盖 getView() 方法

@Override 
getView(int pos , View convert  , ViewGroup vg);

it consist three parameters that could provide with the element position and to make the elemenet inside our own XML file and parent as a reference to the parent View : Now go easy on this method and do ur purpose : inflate your List :它包含三个参数,这些参数可以提供元素位置,并在我们自己的 XML 文件和父视图中制作元素作为对父视图的引用:现在轻松使用此方法并实现您的目的:扩充您的列表:

LayoutInflater li = LayoutInflater.from(getContext());

: we pass to the context of our outclass that will be used :我们传递到将要使用的 outclass 的上下文

 convert = li.inflate(parent , R.layout.elementsForm,false);

we pass to our View paramter of getView() method the inflate li object function and we pass to 3 values the first : our own XML file ID ;我们将 inflate li 对象函数传递给 getView() 方法的 View 参数,我们首先传递给 3 个值:我们自己的 XML 文件 ID; the second one : parent the instance of ViewGroup ;第二个:父 ViewGroup 的实例; the third one : we pass false ;第三个:我们传假;

now we must getItems using getItem function pass to position parameter :现在我们必须使用 getItem 函数传递给位置参数来获取项目:

String st = getItem(position);

create TextView object and pass to your your xml using convert parameter:创建 TextView 对象并使用 convert 参数传递给您的 xml:

TextView tv  = convert.findViewById(R.id.texty);

then set Text to your own text View :然后将 Text 设置为您自己的文本 View :

tv.setText(st); 

now don't forget the most important statement : the return Type method : return convert ;现在不要忘记最重要的声明:返回类型方法:返回转换;

-------- -------- ------------- ---------- -------- -------- ------------- ----------

here we go back to our "MainClass" , we create instance of ListView , ListAdapter instances :在这里我们回到我们的“MainClass”,我们创建 ListView 实例,ListAdapter 实例:

`ListView lv ; `列表视图lv; ListAdapter la ;列表适配器拉;

` and inside the onCreate method : ` 和 onCreate 方法内部:

 0)String [] countries = {"US" , "Canada","Egypt","China"};


 1) lv = findViewById(R.id.mainList);


 2) la = new MyOwnInnerClass(MainClass.this,countries);

0) we define a String array named countries contains four Values those we wanna display . 0) 我们定义了一个名为 countries 的 String 数组,其中包含我们想要显示的四个值。

1) here we pass to lv object the id of our Element 'ListView' that will consist countries values with View Text ; 1) 在这里,我们将元素“ListView”的 id 传递给 lv 对象,该元素将包含带有 View Text 的国家/地区值;

2) we passed an anonymous class that its constructor takes two parameters first context of the main class and the second the values we wanna display 2)我们传递了一个匿名类,它的构造函数接受两个参数,第一个是主类的上下文,第二个是我们想要显示的值

the last operation to connect our ListView element with our own ListView Adapter Class 'D' :将我们的 ListView 元素与我们自己的 ListView Adapter Class 'D' 连接的最后一个操作

is declaring this function :正在声明此功能:

 lv.setAdapter(la);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM