1

I have a TableLayout that has it's rows populated dinamically via database. On each row, I'm trying to make a OnLongClickListener() to show a ListView from a different .xml than my activity_product.xml. Here is relevant part of code from my 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>

And here is my edit_or_delete_menu.xml with the ListView

<?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>

And here is the relevant part of the code I use to populate the rows and attempt to show the 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); 


    }

The code compiles and no error happens when executing this code, but my ListView doesn't show up. What am I missing here?

Thanks in advance.

lucasdc
  • 1,032
  • 2
  • 20
  • 42

1 Answers1

0

I guess the problem is here:

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

You inflated a view, but didn't add it anywhere. Add it as a child view to the view which is supposed to be its parent. Also you need to declare and assign the inflater.inflate(...) result to another variable instead of v. The v is the view, which is clicked. Create a new variable and then add it as a child, as I wrote above. Something like this:

LayoutInflater inflater = (LayoutInflater)  ProductActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.edir_or_delete_menu, null);
v.addView(view);
smb
  • 834
  • 1
  • 8
  • 17