我在onclick方法中有很长的价值总是== 0 ..为什么?

时间:2013-11-26 00:46:47

标签: android

DisplayActivity类

 package com.example.test;


 import java.util.ArrayList;




 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Bundle;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.AdapterView.OnItemLongClickListener;
 import android.widget.ListView;
 import android.widget.SimpleCursorAdapter;
 import android.widget.TextView;
 import android.widget.Toast;

 public class DisplayActivity extends Activity {

private DbHelper mHelper;
private SQLiteDatabase dataBase;

private ArrayList<String> bookId = new ArrayList<String>();
private ArrayList<String> book_Name = new ArrayList<String>();
private ArrayList<String> book_Author = new ArrayList<String>();
private ArrayList<String> book_Info = new ArrayList<String>();
private ArrayList<String> book_Isbn = new ArrayList<String>();
private ArrayList<String> book_Copy = new ArrayList<String>();
public static String bNAME="bname";
public static String bAUTHOR="bauthor";
public static String bISBN="bisbn";
public static String bCOPY="bcopy";
public static String bINFO="binfo";
public static String bID="ID";
private ListView userList;
private TextView t1,t2,t3,t4,t5;
private AlertDialog.Builder build;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_activity);

    t1=(TextView) findViewById(R.id.name);
    t2=(TextView) findViewById(R.id.author);
    t3=(TextView) findViewById(R.id.isbn);
    t4=(TextView) findViewById(R.id.copy);
    t5=(TextView) findViewById(R.id.info);



    userList = (ListView) findViewById(R.id.List);

    mHelper = new DbHelper(this);




    userList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

            Toast.makeText(DisplayActivity.this, "Hi"+arg3+"..", Toast.LENGTH_LONG).show();

        }});}


@Override
protected void onResume() {
    displayData();
    super.onResume();
}

/**
 * displays data from SQLite
 */
private void displayData() {
    dataBase = mHelper.getWritableDatabase();
    Cursor mCursor = dataBase.rawQuery("SELECT * FROM "
            + DbHelper.TABLE_NAME, null);

    bookId.clear();
    book_Name.clear();
    book_Author.clear();
    book_Isbn.clear();
    book_Copy.clear();
    book_Info.clear();
    if (mCursor.moveToFirst()) {
        do {
            bookId.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ID)));
            book_Name.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_NAME)));
            book_Author.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_AUTHOR)));
            book_Isbn.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_ISBN)));
            book_Copy.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_COPIES)));
            book_Info.add(mCursor.getString(mCursor.getColumnIndex(DbHelper.KEY_INFO)));

        } while (mCursor.moveToNext());
    }
    DisplayAdapter disadpt = new DisplayAdapter(DisplayActivity.this,bookId, book_Name);
    userList.setAdapter(disadpt);
    mCursor.close();
}

这是我的DisplayAdapter类

  package com.example.test;


 import java.util.ArrayList;

 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.TextView;

 public class DisplayAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<String> id;
private ArrayList<String> bookName;
//private ArrayList<String> lastName;


public DisplayAdapter(Context c,ArrayList<String> id,ArrayList<String> bname) {
    this.mContext = c;

    this.id = id;
    this.bookName = bname;
    //this.lastName = lname;
}

public int getCount() {
    // TODO Auto-generated method stub
    return id.size();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int pos, View child, ViewGroup parent) {
    Holder mHolder;
    LayoutInflater layoutInflater;
    if (child == null) {
        layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        child = layoutInflater.inflate(R.layout.listcell, null);
        mHolder = new Holder();
        mHolder.txt_id = (TextView) child.findViewById(R.id.txt_id);
        mHolder.txt_bName = (TextView) child.findViewById(R.id.txt_bname);
        //mHolder.txt_lName = (TextView) child.findViewById(R.id.txt_lName);
        child.setTag(mHolder);
    } else {
        mHolder = (Holder) child.getTag();
    }
    mHolder.txt_id.setText(id.get(pos));
    mHolder.txt_bName.setText(bookName.get(pos));
    //mHolder.txt_lName.setText(lastName.get(pos));

    return child;
}

public class Holder {
    TextView txt_id;
    TextView txt_bName;

}

  }

我总是有arg3 == 0!在DisplayActivity类中的onItemClickListener

为什么会发生这种情况?!!

我想要arg3 == id所以我可以查看数据库中的数据..

1 个答案:

答案 0 :(得分:0)

这是你的理由:

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}