Android 如何首先使用CustomView生成15个没有数组的列表项

Android 如何首先使用CustomView生成15个没有数组的列表项,android,Android,我已经制作了适配器,但我只需要列表中的15个位置,而且我没有一个数组可以放入int资源,我该怎么办?假设您想要一个如下所示的自定义列表视图: 列表\u look.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"

我已经制作了适配器,但我只需要列表中的15个位置,而且我没有一个数组可以放入int资源,我该怎么办?

假设您想要一个如下所示的自定义列表视图:

列表\u look.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/1st_text"
        style="@style/liststyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:gravity="center"
       />


</LinearLayout>

现在,您需要在main.xml中填充一个具有列表视图id的列表视图。 列表的信息位于名为data的ArrayList中 这将是您的适配器:

MyAdapter

public class MyAdapter extends ArrayAdapter<String>
{
    private ArrayList<String> data;

    public MyAdapter(Context context, ArrayList<String> values) 
    {
        super(context, R.layout.list_look,values);
        this.data = values;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.list_look, parent, false);

        String s = data.get(position);

        TextView text = (TextView) view.findViewById(R.id.1st_text);
        text.setText(s);


        return view;
    }
公共类MyAdapter扩展了ArrayAdapter
{
私有数组列表数据;
公共MyAdapter(上下文、ArrayList值)
{
超级(上下文、右布局、列表、外观、值);
这个数据=数值;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
LayoutInflater充气器=LayoutInflater.from(getContext());
视图=充气机。充气(R.layout.list\u look,parent,false);
字符串s=data.get(位置);
TextView text=(TextView)view.findViewById(R.id.1st_text);
text.setText(s);
返回视图;
}
现在在mainActivity中使用此适配器,如下所示:

MainActivity.onCreate()

@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList data=getYourOwnDataArray();
ListView列表=(ListView)findViewById(R.id.main\u列表);
MyAdapter ma=新的MyAdapter(此,数据);
列表.设置适配器(ma);
}
也就是说,您可以调整一个自定义数据数组,而在ListView中没有用于该数组的资源xml文件
我希望它能有所帮助:)

假设您想要一个如下所示的自定义列表视图:

列表\u look.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/1st_text"
        style="@style/liststyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        android:gravity="center"
       />


</LinearLayout>

现在,您需要在main.xml中填充一个具有列表视图id的列表视图。 列表的信息位于名为data的ArrayList中 这将是您的适配器:

MyAdapter

public class MyAdapter extends ArrayAdapter<String>
{
    private ArrayList<String> data;

    public MyAdapter(Context context, ArrayList<String> values) 
    {
        super(context, R.layout.list_look,values);
        this.data = values;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.list_look, parent, false);

        String s = data.get(position);

        TextView text = (TextView) view.findViewById(R.id.1st_text);
        text.setText(s);


        return view;
    }
公共类MyAdapter扩展了ArrayAdapter
{
私有数组列表数据;
公共MyAdapter(上下文、ArrayList值)
{
超级(上下文、右布局、列表、外观、值);
这个数据=数值;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
LayoutInflater充气器=LayoutInflater.from(getContext());
视图=充气机。充气(R.layout.list\u look,parent,false);
字符串s=data.get(位置);
TextView text=(TextView)view.findViewById(R.id.1st_text);
text.setText(s);
返回视图;
}
现在在mainActivity中使用此适配器,如下所示:

MainActivity.onCreate()

@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList data=getYourOwnDataArray();
ListView列表=(ListView)findViewById(R.id.main\u列表);
MyAdapter ma=新的MyAdapter(此,数据);
列表.设置适配器(ma);
}
也就是说,您可以调整一个自定义数据数组,而在ListView中没有用于该数组的资源xml文件
我希望它有帮助:)

可能重复的请用新信息编辑您的其他问题。可能用您尝试过的内容添加一些代码。可能重复的请用新信息编辑您的其他问题。可能用您尝试过的内容添加一些代码