这是主要的活动,其中列表视图包含一些数据和一个按钮
它是显示数据,但每当我点击按钮它都不起作用。
final String [] from = new String[]{AllDB.COLUMN_NAME, AllDB.COLUMN_AGE, AllDB.COLUMN_HEIGHT , AllDB.COLUMN_WEIGHT};
final int [] to = new int[]{R.id.nameTV,R.id.ageTV,R.id.heightTV,R.id.weightTV};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
dBmanegar = new AllDBmanegar(this);
dBmanegar.open();
final Cursor cursor = dBmanegar.fetch();
listView = (ListView) findViewById(R.id.list_view);
listView.setEmptyView(findViewById(R.id.emptyTV));
adapter = new SimpleCursorAdapter(this, R.layout.show_person_info, cursor, from, to,0);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TextView idTextView = (TextView)view.findViewById(R.id.idTV);
TextView nameTextView = (TextView) view.findViewById(R.id.nameTV);
TextView ageTextView = (TextView) view.findViewById(R.id.ageTV);
TextView heightTextView = (TextView) view.findViewById(R.id.heightTV);
TextView weightTextView = (TextView) view.findViewById(R.id.weightTV);
Button diet = (Button) view.findViewById(R.id.diet);
diet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent diet_P = new Intent(getApplicationContext(), Diet.class);
startActivity(diet_P);
}
});
//String iD = idTextView.getText().toString();
String name = nameTextView.getText().toString();
String age = ageTextView.getText().toString();
String height = heightTextView.getText().toString();
String weight = weightTextView.getText().toString();
Cursor cursor1 = (Cursor) parent.getItemAtPosition(position);
cursor1.moveToFirst();
//iD = cursor1.getString(cursor1.getColumnIndex(MyDB.COLUMN_ID));
Intent intent = new Intent(getApplicationContext(), Modify_person_info.class);
//intent.putExtra("_id",iD);
intent.putExtra("name", name);
intent.putExtra("age", age);
intent.putExtra("height", height);
intent.putExtra("weight", weight);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_person,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.add_person){
Intent intent = new Intent(this,Add_new.class);
startActivity(intent);
}else if (id == R.id.laout){
Intent backToHome = new Intent(this,MainActivity.class);
startActivity(backToHome);
finish();
}
return super.onOptionsItemSelected(item);
}
这是SimpleCursorAdapter的XML,它显示数据和按钮
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Name"
android:background="#f49c71"
android:id="@+id/nameTV"
android:layout_below="@+id/ageTV"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:background="#bbbbff"
android:id="@+id/ageTV"
android:text="Age"
android:layout_below="@+id/heightTV"
android:layout_alignLeft="@+id/heightTV"
android:layout_alignStart="@+id/heightTV" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Height"
android:background="#f49c71"
android:id="@+id/heightTV"
android:layout_below="@+id/ageTV"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Weight"
android:background="#bbbbff"
android:id="@+id/weightTV"
android:layout_below="@+id/heightTV"
android:layout_alignLeft="@+id/heightTV"
android:layout_alignStart="@+id/heightTV" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/diet"
android:layout_weight=".5"
android:text="Add Diet"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/setText"/>
</LinearLayout>
答案 0 :(得分:0)
尝试将以下属性添加到XML中的Button:
android:focusable="false"
如果它不起作用,请为listview创建自定义适配器: Custom Adapter for List View