1

This is the main Activity, Where a listview has some data and a button

it is show data,but whenever i clicked the button it does not work.

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

And this is the XML of SimpleCursorAdapter where it is display the data and button

<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>
  • I think you will need a custom adapter. See [Custom Simplecursoradapter](http://stackoverflow.com/questions/17708971/using-custom-simplecursoradapter) – K Neeraj Lal Sep 10 '16 at 09:22

1 Answers1

0

Try to add this following attribute to Button from XML:

android:focusable="false"

If it didn't work, create a custom adapter for listview: Custom Adapter for List View

Community
  • 1
  • 1
xxx
  • 3,315
  • 5
  • 21
  • 40