ebatinstitute ebatinstitute - 3 months ago 9
Android Question

Why does my Header mix up the onClicklistener

When I add a header to my listview it messes up my onclick,it selects the wrong item every time.

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


When I add this code it messes up my app:

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

Answer

The reason for that is that the header counts as additional one item inside your listview so when click on item you get the item in position - 1.
Add this inside onItemClick:

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