在andorid中的listview中创建动态textview

时间:2016-01-22 06:30:15

标签: android listview textview baseadapter

我在screens项目中以编程方式TextView创建,因为它已经完成了以下代码。但没有获得所需的解决方案,所以请帮助我解决我的问题。

这是我的

ListView

从上面的代码我得到以下输出,如图像 MY OUTPUT FROM ABOVE CODE

但是我希望像图像MY DESIRE THESE

中那样输出类似输出的输出

从我上面代码的输出中,所有的testview相互叠加,只有最后一项在我的第一张图片中可见 Number10

所以朋友请帮我解决这些问题。谢谢:)

3 个答案:

答案 0 :(得分:1)

嗨试试这种方式可能对你有帮助..

   RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);

    Random rnd = new Random();
    int prevTextViewId = 0;     
    for(int i = 0; i < 10; i++)
    {                       
        final TextView textView = new TextView(this);
        textView.setText("Text "+i);     
        int curTextViewId = prevTextViewId + 1;
        textView.setId(curTextViewId);
        final RelativeLayout.LayoutParams params = 
            new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
                                            RelativeLayout.LayoutParams.WRAP_CONTENT);

        params.addRule(RelativeLayout.ALIGN_RIGHT, prevTextViewId);
        textView.setLayoutParams(params);

        prevTextViewId = curTextViewId;
        layout.addView(textView, params);
    }              

答案 1 :(得分:0)

此示例代码可能会帮助您

public View getView(int position, View view, ViewGroup parent) {

    view = inflateIfRequired(view, position, parent);
    bind(getItem(position), view);

    return view;
}

private void bind(Eddystone eddystone, View view) {

    ViewHolder holder = (ViewHolder) view.getTag();

    String name = StringToHex.convertHexToString(eddystone.instance);

    holder.macTextView.setText("");
    holder.rssiTextView.setText("");
    holder.eddystoneNamespaceTextView.setText("");
    holder.eddystoneInstanceIdTextView.setText("");
}

private View inflateIfRequired(View view, int position, ViewGroup parent) {

    if (view == null) {
        view = inflater.inflate(R.layout.item, null);
        view.setTag(new ViewHolder(view));
    }

    return view;
}

static class ViewHolder {

    final TextView macTextView;
    final TextView rssiTextView;
    final TextView namespace;
    final TextView instanceId;

    ViewHolder(View view) {
        macTextView = (TextView) view.findViewWithTag("mac");
        rssiTextView = (TextView) view.findViewWithTag("rssi");
        namespace = (TextView) view.findViewWithTag("namespace");
        instanceId = (TextView) view.findViewWithTag("instance_id");
    }
}

答案 2 :(得分:0)

onCreate()方法中的

openssl req -new -key /etc/kubernetes/ssl/kube-apiserver-key.pem -out /tmp/user-cert.pem

您可以使用自定义适配器( BaseAdapter )根据 ListView

中的数据添加 TextView
List<String> arrayList = new ArrayList<String>();
arrayList.add("One");
arrayList.add("Two");
arrayList.add("Three");
arrayList.add("Four");


ListViewAdapter adapter = new ListViewAdapter(DemoClass.this,arrayList);

list_demo.setAdapter(adapter);

您还可以创建一个用于生成自定义 TextView 的布局文件,作为 single_row.xml

public class ListViewAdapter extends BaseAdapter {

    Context context;
    TextView contact_number;
    List<String> arrayList;

    DataAdapter(Context c, List<String> arrayList) {
        this.context = c;
    this.arrayList=arrayList;
    }

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

    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

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

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

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.single_row, parent, false);

        textView = (TextView) row.findViewById(R.id.textView);

        textView.setText(arrayList.get(position));

        return row;

    }
}