我有一个TableLayout
,它的行通过数据库填充。在每一行中,我都试图让OnLongClickListener()
显示ListView
来自与我.xml
不同的activity_product.xml
。以下是我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.xml
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>
以下是我用来填充行并尝试显示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没有显示。我在这里错过了什么?
提前致谢。
答案 0 :(得分:0)
我想问题出在这里:
LayoutInflater inflater = (LayoutInflater) ProductActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.edir_or_delete_menu, null);
你夸大了一个视图,但没有在任何地方添加它。将它作为子视图添加到视图中,该视图应该是其父视图。
您还需要声明并将inflater.inflate(...)
结果分配给另一个变量,而不是v
。 v
是视图,单击该视图。创建一个新变量,然后将其添加为子项,如上所述。
像这样:
LayoutInflater inflater = (LayoutInflater) ProductActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.edir_or_delete_menu, null);
v.addView(view);