【问题标题】:ListView not displaying on inflateListView 不显示在充气
【发布时间】:2014-06-08 17:43:53
【问题描述】:

我有一个TableLayout,它的行通过数据库动态填充。在每一行上,我都在尝试制作一个OnLongClickListener(),以显示与我的activity_product.xml 不同的.xmlListView。这是我的activity_product.xml中的相关代码部分:

<TableLayout
    android:id="@+id/productsTable"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="3dip"
    android:paddingRight="3dip"
    android:shrinkColumns="1"
    android:weightSum="1"
    android:stretchColumns="*">
</TableLayout>

这是我的edit_or_delete_menu.xmlListView

<?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:orientation="vertical" >

    <ListView
        android:id="@+id/edirOrDeleteMenu"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    </ListView> 
</LinearLayout>

这是我用来填充行并尝试显示ListView的代码的相关部分:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_product);
    new LoadProductsTableContent().execute(); 
}

private class LoadProductsTableContent extends AsyncTask<Void, Void, Integer> {
    private TableLayout productsTable = (TableLayout) findViewById(R.id.productsTable); 
    private TableRow labelRow = (TableRow) findViewById(R.id.productLabelsRow);

    @Override
    protected Integer doInBackground(Void... params) 
    {
        int result=1;

        // Usado para acessarmos as views da thread pai
        // http://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi
        runOnUiThread(new Runnable() {
             @Override
             public void run() {

                // Create a new row to be added
                TableRow tr = new TableRow(ProductActivity.this);
                /* CODE TO SET ROW PROPERTIES AND CONTENT */

                // Row click listener
                tr.setOnLongClickListener(new OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        // TODO Auto-generated method stub
                        onClickRow(v);
                        return false;
                    }
                });

                // Add row to TableLayout
                productsTable.addView(tr);

            }
        });

        return result;
    }

    private void onClickRow(View v) {
        String editText = getResources().getString(R.string.menuEditOption);
        String deleteText = getResources().getString(R.string.menuDeleteOption);

        // Inflating to display ListView
        LayoutInflater inflater = (LayoutInflater)  ProductActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.edir_or_delete_menu, null);
        edirOrDeleteMenu = (ListView) v.findViewById(R.id.edirOrDeleteMenu);            
        String[] options = new String[]{editText, deleteText};   

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(ProductActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, options);            
        edirOrDeleteMenu.setAdapter(adapter); 


    }

代码编译,执行此代码时没有错误发生,但我的 ListView 没有显示。我在这里想念什么?

提前致谢。

【问题讨论】:

    标签: android android-listview android-inflate


    【解决方案1】:

    我猜问题出在这里:

    LayoutInflater inflater = (LayoutInflater)  ProductActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.edir_or_delete_menu, null);
    

    您放大了一个视图,但没有在任何地方添加它。将其作为子视图添加到应该是其父视图的视图中。 您还需要声明并将inflater.inflate(...) 结果分配给另一个变量而不是vv 是被点击的视图。如上所述,创建一个新变量,然后将其添加为子变量。 像这样的:

    LayoutInflater inflater = (LayoutInflater)  ProductActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.edir_or_delete_menu, null);
    v.addView(view);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多