So i have a main activity:
public class MainActivity extends AppCompatActivity {
static DBhelper myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDB=new DBhelper(this);
//myDB.access_token(93588930);
}
@Override
public void onResume() {
super.onResume();
LinearLayout ll= (LinearLayout) findViewById(R.id.char_layout);
if(( ll).getChildCount() > 0)
( ll).removeAllViews();
JSONArray chars=myDB.get_all_chars();
TextView[] tv= new TextView[chars.length()];
for (int i = 0; i < chars.length(); i++) {
try {
tv[i] = new TextView(this);
JSONObject c = (JSONObject) chars.get(i);
tv[i].setText(c.getString("name")+"\n"+c.getString("id"));
ll.addView(tv[i]);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
with the following xml:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
tools:context="com.jbs.evecompanion.MainActivity"
android:columnCount = "1">
<LinearLayout
android:orientation="vertical"
android:id="@+id/char_layout"
android:clickable="true"
android:onClick="stufftocall"/>
</GridLayout>
I want to be able to on every row and call a function.
Now, as you can see i set "clickable" and "onclick" for the layout. But this would call the same function now matter which row I click, right?
What I want is to click an each row and call the same function with different parameters depending on what text is set in this row.
You can use setTag()
and getTag()
methods.
Create one listener :
mListener = new OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG,"Clicked on "+v.getTag());
}
};
Assign all textViews one tag and add this listener to each of them :
tv[i].setTag(i);
tv[i].setOnClickListener(mListener);