为什么我的标题会混淆onClicklistener

时间:2016-08-28 12:14:28

标签: java android

当我在列表视图中添加标题时,它会弄乱我的onclick,它每次都会选择错误的项目。

package ie.example.artur.projectrepeat;

import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class DataListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    ListView listView;
    SQLiteDatabase sqLiteDatabase;
    DatabaseClass  database;
    Cursor cursor;
    ListDataAdapter listDataAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_list_layout);

        listView = (ListView) findViewById(R.id.list_view);

        listDataAdapter = new ListDataAdapter(getApplicationContext(), R.layout.row_layout);
        listView.setAdapter(listDataAdapter);
        listView.setOnItemClickListener(this);
        database = new DatabaseClass(getApplicationContext());

        sqLiteDatabase = database.getReadableDatabase();

        Cursor cursor=database.getInformation(sqLiteDatabase);

        if (cursor.moveToFirst()) {
            do {

                String id, product_name, category,quantity,importance;
                id = cursor.getString(0);
                product_name = cursor.getString(1);
                category = cursor.getString(2);
                quantity = cursor.getString(3);
                importance = cursor.getString(4);
                DataProvider dataProvider = new DataProvider(id, product_name, category,quantity,importance);
                listDataAdapter.add(dataProvider);
            } while (cursor.moveToNext());
        }
    }
    @Override
    public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {


        final TextView tv = (TextView) view.findViewById(R.id.product_id);
        AlertDialog.Builder alert = new AlertDialog.Builder(
                DataListActivity.this);
        alert.setTitle("Alert!!");
        alert.setMessage("Are you sure to delete record");
        alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //do your work here

                sqLiteDatabase = database.getReadableDatabase();

                DatabaseClass.DeleteInformation(tv.getText().toString(), sqLiteDatabase);

                listView.setAdapter(listDataAdapter);
                listDataAdapter.notifyDataSetChanged();
                listDataAdapter.removeItemAt(position);
                dialog.dismiss();
            }
        });
        alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();
            }
        });

        alert.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        {
            switch (item.getItemId())
            {
                case R.id.home : startActivity (new Intent(this, Main2Activity.class));
                    break;

                case R.id.action_settings : startActivity (new Intent(this, SecondActivity.class));
                    break;
                case R.id.catalogue :startActivity (new Intent(this, ViewAllItems.class));
                    break;

                case R.id.ViewList :startActivity (new Intent(this, DataListActivity.class));
                    break;

                case R.id.find :startActivity (new Intent(this, Main3Activity.class));
                    break;

                case R.id.Update :startActivity (new Intent(this, Edit_Activity.class));
                    break;

            }
            return super.onOptionsItemSelected(item);
        }}
}

当我添加此代码时,它会弄乱我的应用程序:

LayoutInflater myinflater = getLayoutInflater();
        ViewGroup myHeader = (ViewGroup)myinflater.inflate(R.layout.header, listView, false);
        listView.addHeaderView(myHeader, null, false)

1 个答案:

答案 0 :(得分:0)

原因是标头在listview内计为额外的一个项目,因此当点击项目时,您会在position - 1中获得该项目。
onItemClick内添加:

 position -= listView.getHeaderViewsCount(); // or position - 1