这是我的应用程序。当点击新按钮时,它将逐个显示问题,如果我们点击选项(A,B,C,D),我应该显示正确或不正确。对于我现在编码的问题我建议我可以选择。
我在xml文件中有5个文本视图和4个按钮,这些数据正在从数据库中填充,所以我使用了ListView和SimpleCursorAdapter.It一切正常。现在我想为Xml文件中的按钮编写OnClick函数。我无法添加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 :(得分: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();
}
});
答案 1 :(得分:0)
关于代码:
SimpleCursorAdapter sca = new SimpleCursorAdapter(MainActivity.this, R.layout.compo_list, c1, from, to);
ListView l1 = (ListView) findViewById(R.id.listView);
当您使用布局xml SimpleCursorAdapter
调用compo_list
时,它不会使布局膨胀。它适用于适配器和而非活动。因此,您应该在findViewById(R.id.listView)
建议:
setContentView
已经做了。这令人困惑,并为此付出了很多努力。通常在片段中进行充气。在Fragment中,您可以调用并创建SimpleCursorAdapter的新实例。setContentView(R.layout.activity_main)
。这通常是您可以放置UI元素的唯一布局文件。编辑: 根据您的建议,以下是 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);
}
请注意代码段中的inflate方法。这是您要用于R.layout.compo_list
的布局。
我希望这有助于解释您的问题。看来你还有一些工作要做。祝你好运,玩得开心......
答案 2 :(得分:0)
放置一个名为ListViewAdapter的新活动,它扩展了BaseAdapter并覆盖了它中的函数。放置了这个类中的所有OnClick()活动,它完美无缺。 如果有人需要更多信息,请告诉我。
答案 3 :(得分:-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));
}
});