2015-06-18 41 views
0

這是我的app.When點擊新按鈕時,它會逐個顯示問題,如果我們點擊選項(A,B,C,D),我應該顯示正確或不正確。對於我的問題,編碼現在建議我爲選項。如何添加的OnClick()的按鈕在Android的ListView的?

我有一個XML文件5textviews和4buttons,對於那些正在從數據庫中填充的數據,所以我使用的ListView和SimpleCursorAdapter.It所有作品fine.Now我想寫的按鈕的OnClick函數在XML file.I不可能添加SetOnClickListener,因爲它顯示錯誤「不可能罰資源ID」,它顯示的按鈕是在不同的XML提供。 如何爲這些按鈕分配OnClick()?

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" /> 

等待建議。如果你能幫助我,請提前感謝。

+0

我很抱歉,您在哪裏使用'setOnClick'?我可以看到只查詢數據庫並將適配器設置爲listView – deadfish

+0

實際上,按鈕位於「R.layout.compo_list」XML文件中。它是傳遞給ListView的組件。 – Sharath

+0

請使用當前信息更新您的問題。你能否提供你剛纔提到的這種佈局缺失的部分? – deadfish

回答

0

放置了一個名爲ListViewAdapter的新活動,該活動擴展了BaseAdapter並覆蓋了它的功能。將此類中的所有OnClick()活動放在一起,它的功能非常完美。 如果有人需要更多的信息,請給我打電話。

0

它從你的列表視圖。

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

爲什麼不讓ListView的子元素遍歷它們,如果它是Button的一個實例,則將它轉換爲按鈕並在該按鈕上調用setOnClickListener。我會在一秒內給你一個例子 –

+0

如何做到這一點? – Sharath

+0

@dave可以告訴我應該在哪裏添加該代碼? – Sharath

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

    } 

}); 
+0

你能告訴我應該在哪裏添加這段代碼嗎? – Sharath

+0

它不會工作,如果位置計數不等於listView的子女計數 – deadfish

+0

我的問題很簡單@Deadfish ...我在myAdap類中有onClick(),它應該在點擊按鈕時打印增量值.Plz爲它提供了一些方法?謝謝! – Sharath

0

關於代碼:

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

當你調用SimpleCursorAdapter的佈局XML compo_list,它不膨脹的佈局。它的目的是爲適配器不活動。因此,你應該得到findViewById(R.id.listView)

建議運行時錯誤:

  • 在SimpleCursorAdapter,複製代碼findViewById的適配器。這是一種常見的技術。
  • 不要試圖誇大活動,setContentView已經這樣做。這是令人困惑和很多工作。通常在片段中進行充氣。在Fragment中,您可以調用並創建SimpleCursorAdapter的新實例。
  • 活動中已有代碼setContentView(R.layout.activity_main)。這是正常,你可以將UI元素在你僅有的佈局文件

編輯: 根據您的建議,下面是的ListView好的網頁教程:

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

  • 頁面1引入了使用的ListView與流行ARrayAdapter。使用CursorAdapters介紹瞭如何使用ListView和基本適配器。從鏈接頁面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); 
     } 

通知在代碼段的充氣法。這是您想用於R.layout.compo_list的佈局。

我希望這有助於解釋您的問題。看來你還有一些工作要做。祝你好運,玩得開心...

+0

你是在告訴如何處理fragment而不是用XML膨脹? – Sharath

+0

@Sharathkumar,創建一個片段並在其中擴充佈局xml,而不是主要活動。我在我的建議文章中做了幾處修改,澄清。關於Adapters和Listview的主題有很好的示例/教程。 –

+0

你可以爲它建議一些頁面嗎?閱讀和學習 – Sharath

相關問題