Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在适配器中的运行时添加TextView_Java_Android - Fatal编程技术网

Java 在适配器中的运行时添加TextView

Java 在适配器中的运行时添加TextView,java,android,Java,Android,在数组中追加每条消息 m、 getgreen() 作为数组中每个消息的TextView的新消息,我想为它创建TextView并附加 这是它里面的CustomListAdapter,我希望在数组中循环,并将数组中的每个项作为新的TextView追加 public class CustomListAdapter extends BaseAdapter { private Activity activity; private LayoutInflater inflater; private List&

在数组中追加每条消息

m、 getgreen()

作为数组中每个消息的TextView的新消息,我想为它创建TextView并附加

这是它里面的CustomListAdapter,我希望在数组中循环,并将数组中的每个项作为新的TextView追加

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<notofication> movieItems;
private final Context context;


public CustomListAdapter(Activity activity, List<notofication> movieItems,Context c) {
    this.activity = activity;
    this.movieItems = movieItems;
    this.context = c;
}

@Override
public int getCount() {
    return movieItems.size();
}

@Override
public Object getItem(int location) {
    return movieItems.get(location);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);


    TextView title = (TextView) convertView.findViewById(R.id.title);
    TextView rating = (TextView) convertView.findViewById(R.id.rating);
    TextView genre = (TextView) convertView.findViewById(R.id.genre);
    TextView year = (TextView) convertView.findViewById(R.id.releaseYear);

    // getting movie data for the row
    final notofication m = movieItems.get(position);







    // title
    title.setText(m.getTitle());



    // rating
    rating.setText(String.valueOf(m.getRating()));


    String genreStr = "";
    for (String str : m.getGenre()) {
       //append every str as new textView
    }


    // release year
    year.setText(m.getYear());

    return convertView;
}

}

但是我不知道如何创建新的TextView并将其附加到CustomAdapter中

您可以通过

TextView textView=new TextView(context);
要将其添加为列表视图项,我建议您在
list\u行中创建一个holder
XML

<LinearLayout
     android:id="@+id/holder"
     android:orientation="vertical"
     android:width="match_parent"
     android:height="wrap_content"/>

您可以通过编程方式创建
文本视图

TextView textView=new TextView(context);
要将其添加为列表视图项,我建议您在
list\u行中创建一个holder
XML

<LinearLayout
     android:id="@+id/holder"
     android:orientation="vertical"
     android:width="match_parent"
     android:height="wrap_content"/>
用这个例子

而不是这个

TextView genre = (TextView) convertView.findViewById(R.id.genre);
用这个

LinearLayout mgenre = (LinearLayout) convertView.findViewById(R.id.genre);
注意:-在xml中,将id类型的文本视图更改为方向垂直的线性布局。

for(String genre:m.getGenre()){
    TextView text = new TextView(context);
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    text.setLayoutParams(p);
    text.setText(genre);
    text.setTextAppearance(R.style.boldText);

    mgenre.addView(text);
}
享受编码……。

使用此示例

而不是这个

TextView genre = (TextView) convertView.findViewById(R.id.genre);
用这个

LinearLayout mgenre = (LinearLayout) convertView.findViewById(R.id.genre);
注意:-在xml中,将id类型的文本视图更改为方向垂直的线性布局。

for(String genre:m.getGenre()){
    TextView text = new TextView(context);
    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    text.setLayoutParams(p);
    text.setText(genre);
    text.setTextAppearance(R.style.boldText);

    mgenre.addView(text);
}


享受编码………..

体裁制作成
线性布局
并在
运行时创建
文本视图
,并将其添加到
体裁
的文本视图中。…@sushildh任何我是androidmake
体裁
初学者的例子都可以在
线性布局
中创建
文本视图
运行时
并将文本视图添加到
体裁
。@sushildlh任何我是androidi初学者的示例现在都可以尝试:)您必须像这样设置LayoutParams:LinearLayout.LayoutParams p=新建LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容);text.setLayoutParams(p);更新了答案我将立即尝试:)您必须像这样设置LayoutParams:LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_内容,LinearLayout.LayoutParams.WRAP_内容);text.setLayoutParams(p);更新了答案如何将样式添加到新文本视图?查看编辑答案或浏览此链接,然后选择LinearLayout genrxe=(LinearLayout)convertView.findViewById(R.id.rrr);对于(int i=0;i<17;i++){TextView text=new TextView(上下文);LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);text.setLayoutParams(text.setText)(“此处的消息”+i);genrxe.addView(文本);}线性布局中没有显示任何内容!!xml中线性布局的大小是多少?宽度为189dp,高度为509dp,背景为红色这只是一个测试如何为新的文本视图添加样式?查看编辑答案或浏览此链接,线性布局genrxe=(线性布局)convertView.findViewById(R.id.rrr);for(int i=0;i<17;i++){TextView text=new TextView(context);LinearLayout.LayoutParams p=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);text.setLayoutParams(p);text.setText(“此处的消息”+i);genrxe.addView(text)}线性布局中没有显示任何内容!!xml中线性布局的大小是多少?宽度为189dp,高度为509dp,背景为红色,这只是一个测试