How to add OnClick() for buttons in ListView of android?

775 views Asked by At

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.

4

There are 4 answers

0
Sharath On BEST ANSWER

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.

5
The Original Android On

About the code:

SimpleCursorAdapter sca = new SimpleCursorAdapter(MainActivity.this, R.layout.compo_list, c1, from, to);
ListView l1 = (ListView) findViewById(R.id.listView);

When you call SimpleCursorAdapter with the layout xml compo_list, it does not inflate the layout. It is meant for the Adapter and not the Activity. Therefore you should get runtime error on findViewById(R.id.listView)

Suggestions:

  • In the SimpleCursorAdapter, copy code findViewById for the Adapter. This is a common technique.
  • Don't try to inflate in the Activity, setContentView did that already. This is confusing and much work for it. Normally inflating is done in a Fragment. In the Fragment, you can call and create new instance of SimpleCursorAdapter.
  • There is code already setContentView(R.layout.activity_main) in the Activity. This is normally your only layout file that you can place the UI elements in.

EDIT: Per your suggestion, here are good webpage tutorials on ListView:

  1. ArrayAdapter @ Using an ArrayAdapter with ListView
  2. SimpleCursorAdapter with ListView Using CursorAdapters

Notes:

  • Webpage 1 introduces using ListView with popular ARrayAdapter.
  • Page 2 introduces using CursorAdapters, assumes you know how to use ListView and basic Adapters.

Code snippet from Link page 1:

public View getView(int position, View convertView, ViewGroup parent) {
       // Get the data item for this position
       User user = getItem(position);    
       // Check if an existing view is being reused, otherwise inflate the view
       if (convertView == null) {

          convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
       }

Notice the inflate method in the code snippet. This is the layout that you want to use for R.layout.compo_list.

I hope this helps and explains your issue. It seems you still have some work to do. Good luck and have fun...

8
dave On

Do it from your listview.

    l1.setOnItemClickListener(new OnItemClickListener()
{
    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
    { 
        Toast.makeText(getActivity(), "" + position, Toast.LENGTH_SHORT).show();
    }
});
2
Kristy Welsh On
  listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

  @Override
  public void onItemClick(AdapterView<?> parent, final View view,
      int position, long id) {
    final String item = (String) parent.getItemAtPosition(position);
    t1.setText(String.valueOf(item));

  }

});