kilg
kilg

Reputation: 55

How get id from dynamic view and start for them context menu?

I create class for adding LinearLayout with any TextView:

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);}}

And adding this class to another 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 {
   ...
}}

How can I get id for LinearLayout(creating from database information) and use it in onCreateContextMenu?

Upvotes: 0

Views: 225

Answers (2)

kilg
kilg

Reputation: 55

I met new trouble: in this construction:

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")));
}

If I open context menu on getChildAt(0) or getChildAt(1), there are same menu entry: "Zero" and "One". But I wont separate menu for these two views. If there are switch operator used with R.id.xxxx, both views has separate menu.

Upvotes: 0

Preetika Kaur
Preetika Kaur

Reputation: 2031

 tab.getChildAt(index).getId()

This will give you id of view added dynamically at particular index.

Upvotes: 1

Related Questions