listview.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:id="@+id/lstText">
</ListView>
</LinearLayout>
ListView代码
package saint.animaltracking;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import java.util.List;
import saint.animaltracking.helper.AnimalAdapter;
import saint.animaltracking.helper.DatabaseHelper;
/**
* Created by Kodie on 3/28/2016.
*/
public class selectAnimal extends AppCompatActivity
{
private ListView lv;
private List<animal> animal;
private DatabaseHelper db;
AnimalAdapter adapter;
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
db = new DatabaseHelper(this.getApplicationContext());
setContentView(R.layout.main);
animal = db.getAllAnimal();
lv = (ListView) findViewById(R.id.lstText);
adapter = new AnimalAdapter(this, R.layout.list_item, animal);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
animal anim = (animal) lv.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), specific.class);
intent.putExtra("animal", anim);
startActivity(intent);
}
});
}
}
specific.class xml(我尝试调试问题时通用)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/btnButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="@+id/btnButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_toRightOf="@+id/btnButton1"/>
<Button
android:id="@+id/btnButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_below="@+id/btnButton1"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnButton3"
android:layout_marginTop="94dp"
android:text="User :"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:layout_toRightOf="@+id/btnButton3" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/editText1"
android:text="Submit" />
</RelativeLayout>
Specific.java代码
package saint.animaltracking;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
/**
* Created by Kodie on 5/2/2016.
*/
public class specific extends AppCompatActivity {
String id;
private SQLiteOpenHelper AT;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.specific);
Intent intent = getIntent();
}
}
我遇到的问题是,当我点击特定的listview项目转到特定的动物时,它显示的只是一个空白页面。我不是很确定为什么,但我有一种强烈的感觉,它与我编写OnItemClickListener和OnItemClick的方式或它如何将意图传递给specific.java类有关?除了尝试重写OnItemClickListener部分之外,我已经完成了我能想到的所有事情(我已经在android中工作了大约三周)来解决这个问题。非常感谢任何正确方向的提示。提前谢谢。
更新: 单击listitem时,我会反复收到以下消息
W / ViewRootImpl:由于没有窗口焦点而取消事件:MotionEvent ...
E / Surface:getSlotFromBufferLocked:未知缓冲区:0xb40d53a0
答案 0 :(得分:0)
尝试在特定类中使用随机文本定义一些textview ..或类似Toast之类的东西,以证明特定类正在运行。