This is my app.When clicking new button it will display the questions one by one and if we click the options(A,B,C,D)i should display correct or incorrect.For questions i did the coding now suggest me for options.
I have 5textviews and 4buttons in a xml file,data for those are being populated from database so i used ListView and SimpleCursorAdapter.It all works fine.Now i want to write OnClick functions for the buttons that are in the Xml file.I couldnt add SetOnClickListener as it displays an error "Couldnt fine the resource id" it displays as the buttons are available in the different xml. How to assign OnClick() for those buttons?
SQLiteDatabase db;
Cursor c1;
Button b1;
String from[] = {"_id", "optn1", "optn2", "optn3", "optn4", "ab"};
int[] to = {R.id.textView3, R.id.textView5, R.id.textView6, R.id.textView7, R.id.textView8, R.id.textView10};
int i = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db.execSQL("CREATE TABLE ques(_id TEXT,optn1 TEXT ,optn2 TEXT,optn3 TEXT,optn4 TEXT,qno INTEGER,ab INTEGER)");
t1 = (TextView) findViewById(R.id.textView4);
t2 = (TextView) findViewById(R.id.textView9);
b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(this);
}
public void onClick(View v) {//this onclick is to display the texts only if the button is clicked not as soon as the app is executed
if (v == b1) {
db = openOrCreateDatabase("quiz1.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
if (c1 != null) {
i = c1.getInt(c1.getColumnIndex("qno"));
}
c1 = db.rawQuery("SELECT * FROM ques WHERE qno>'" + i + "' ORDER BY qno LIMIT 1", null);
SimpleCursorAdapter sca = new SimpleCursorAdapter(MainActivity.this, R.layout.compo_list, c1, from, to);
ListView l1 = (ListView) findViewById(R.id.listView);
l1.setAdapter(sca);
sca.changeCursor(c1);
t1.setText(String.valueOf(i));
}
}
Layout.XML
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/textView3"
android:text="QUESTION"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="44dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView5"
android:layout_marginTop="46dp"
android:phoneNumber="false"
android:gravity="center"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:id="@+id/button2"
android:layout_alignTop="@+id/textView5"
android:layout_toLeftOf="@+id/textView5"
android:layout_toStartOf="@+id/textView5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView6"
android:layout_below="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:layout_toEndOf="@+id/button2"
android:gravity="center" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_alignStart="@+id/button2"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView7"
android:layout_below="@+id/button3"
android:layout_alignLeft="@+id/textView6"
android:layout_alignStart="@+id/textView6"
android:gravity="center" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:id="@+id/button4"
android:layout_alignTop="@+id/textView7"
android:layout_alignLeft="@+id/button3"
android:layout_alignStart="@+id/button3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView8"
android:layout_below="@+id/button4"
android:layout_toRightOf="@+id/button4"
android:layout_toEndOf="@+id/button4"
android:gravity="center" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D"
android:id="@+id/button5"
android:layout_alignTop="@+id/textView8"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4" />
waiting for suggestion.It will so grateful if u help me out.thanks in advance.
Placed a new activity named ListViewAdapter which extends BaseAdapter and Overrided the functions in it.Placed all the OnClick() activities inside this class and it works perfect. Ping me if anyone need more info on it.