如何从动态视图中获取id并启动它们的上下文菜单?

时间:2016-09-23 06:57:58

标签: android android-contextmenu

我使用任何TextView创建用于添加LinearLayout的类:

public class Tabs extends LinearLayout {


Configuration c = getResources().getConfiguration();

int screenWidthDP = c.screenWidthDp;
float density = this.getResources().getDisplayMetrics().density;

float ftabsize = screenWidthDP * density / 3;
int tabsize = (int) (float) ftabsize;

float fTabMargins = 1 * density;
int tabMargins = (int) (float) fTabMargins;

LinearLayout tabB;
LayoutParams tabBP;
LinearLayout tab;
LayoutParams tabP;
TextView text;
LayoutParams textP;

Tabs(Context context, String name, int id) {
    super(context);

//tabB
    tabB = new LinearLayout(context);
    tabB.setOrientation(HORIZONTAL);
    tabBP = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
    tabBP.width = tabsize;
    tabBP.setMargins(tabMargins,tabMargins,tabMargins,tabMargins);
    tabB.setLayoutParams(tabBP);
    tabB.setBackgroundColor(Color.BLACK);
    tabB.setGravity(Gravity.CENTER);

    //tab

    tab = new LinearLayout(context);
    tab.setOrientation(HORIZONTAL);
    tabP = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    tabP.setMargins(4,4,4,4);
    tab.setLayoutParams(tabP);
    tab.setBackgroundColor(Color.WHITE);
    tab.setGravity(Gravity.CENTER);
    tab.setId(id);
    tabB.addView(tab);


    //Text in Tab
    text = new TextView(context);
    textP = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    text.setLayoutParams(textP);
    text.setText(name);
    /*text.setText(String.valueOf(ress));*/

    tab.addView(text);

}
public void addTab(LinearLayout ll) {ll.addView(tabB);}}

将此类添加到另一个LinearLayout:

public class MainActivity extends AppCompatActivity {

final String LOG_TAG = "myLogs";
final String SqlInit1 = "INSERT INTO tabs " +
        "(" +
        "name, " +
        "color, " +
        "order1" +
        ")" +
        "VALUES(" +
        "'first', " +
        "'blue'," +
        "'1'" +
        ");";
final String SqlInit2 = "INSERT INTO tabs " +
        "(" +
        "name, " +
        "color, " +
        "order1" +
        ")" +
        "VALUES(" +
        "'second'," +
        "'red', " +
        "'2'" +
        ");";
DBHelper dbHelper;
LinearLayout n77;
int rrr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout ll = (LinearLayout) findViewById(R.id.tabs);


    dbHelper = new DBHelper(this);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Log.d(LOG_TAG, "Reading Database....");
    Cursor c = db.query("tabs", null, null, null, null, null, null);
    if (c.moveToFirst()) {
        int idColIndex = c.getColumnIndex("id");
        int nameColIndex = c.getColumnIndex("name");
        int colorColIndex = c.getColumnIndex("color");
        int order1ColIndex = c.getColumnIndex("order1");

        do {
            Tabs myTab = new Tabs(this, c.getString(nameColIndex), c.getInt(idColIndex));
            myTab.addTab(ll);
            registerForContextMenu(myTab);
            if (c.getString(nameColIndex) == "first") {
                rrr = myTab.gID();
            }

          Log.d(LOG_TAG, "ID = " + c.getInt(idColIndex) + "\n" +
                  "Name = " + c.getString(nameColIndex) + "\n" +
                  "Color = " + c.getString(colorColIndex) + "\n" +
                  "Order = " + c.getString(order1ColIndex));
        } while (c.moveToNext());
    } else
        Log.d(LOG_TAG, "0 Rows...");
    c.close();

    dbHelper.close();
    Log.d(LOG_TAG, "Closing database....");


    n77 = (LinearLayout) findViewById(R.id.n77);
    registerForContextMenu(n77);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  /*  super.onCreateContextMenu(menu, v, menuInfo);*/

    switch (v.getId()) {
        case R.id.n77:
            menu.setHeaderTitle("Title:");
            menu.add(0,1,0,"1");
            menu.add(0,2,0,"2");
            break;
       /* case rrr:
            menu.add(0,1,0,"3");
            break;*/

    }
}


class DBHelper extends SQLiteOpenHelper {
   ...
}}

如何获取LinearLayout的id(从数据库信息创建)并在onCreateContextMenu中使用它?

2 个答案:

答案 0 :(得分:1)

 tab.getChildAt(index).getId()

这将为您提供在特定索引处动态添加的视图的ID。

答案 1 :(得分:0)

我遇到了新的麻烦: 在这种结构中:

if (v.getId() == ll.getChildAt(0).getId()) {
        menu.setHeaderTitle("First Title");
        menu.add(0, v.getId(), 0, "Zero.");
    } else if (v.getId() == ll.getChildAt(1).getId()) {
        menu.setHeaderTitle("Second Title");
        menu.add(0, v.getId(), 0, "One")));
}

如果我在getChildAt(0)或getChildAt(1)上打开上下文菜单,则会有相同的菜单项:“Zero”和“One”。但我不会为这两个视图分开菜单。 如果有与R.id.xxxx一起使用的开关操作符,则两个视图都有单独的菜单。