无法在自定义列表视图适配器上的onitemclicklistener中对非静态方法进行静态引用从sqlite数据库获取数据

时间:2014-05-30 07:04:08

标签: java android android-listview custom-adapter

应用程序结构首先:

//此类有一个列表视图,将从自定义适配器

填充
public class Transactions_show extends Activity implements OnItemClickListener

//这个类是自定义适配器,它使用sqlite数据库为上面列表视图中填充的每一行返回自定义视图

public class CustomAdapter extends BaseAdapter

//这里一切正常
//现在
//在listview的每个项目上设置onitemclicklistener

public class Transactions_show extends ListActivity implements
    OnItemClickListener {

List<Transactions> all_transactions;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_transactions);

    DatabaseHandler db = new DatabaseHandler(getApplicationContext());

    all_transactions = db.get_all_transactions();
    size_of_transaction = all_transactions.size();

    ListView all_transactions_list = (ListView) findViewById(R.id.all_transaction_show_list);

    CustomAdapter adapter = new CustomAdapter(this, all_transactions);

    all_transactions_list.setAdapter(adapter);
    try {
        all_transactions_list.setOnItemClickListener(this);
    } catch (NullPointerException e) {
        Log.e("null pointer exception at item click", e.getMessage());
    }

}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Transactions single_transaction = new Transactions();
    single_transaction = all_transactions.get(position);

    Intent intent = new Intent(this, transacee_summary.class);
            int p_n = single_transaction.get_phone_number();
    Intent.putExtra("phone_number", p_n);
     startActivity(intent);
    /*
     * Toast toast = Toast.makeText(getApplicationContext(), "Item " +
     * (position + 1) + ": " +single_transaction.get_phone_number() ,
     * Toast.LENGTH_SHORT); toast.setGravity(Gravity.BOTTOM |
     * Gravity.CENTER_HORIZONTAL, 0, 0); toast.show();
     */
}

}

错误发生在这里
Intent.putExtra(&#34; phone_number&#34;,p_n);
哪个是:不能从类型Intent

中对非静态方法putExtra(String,int)进行静态引用

查找并尝试堆栈溢出数日以及谷歌开发人员,决定制作Passing a Bundle on startActivity()?

中给出的捆绑包
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Transactions single_transaction = new Transactions();
single_transaction = all_transactions.get(position);

Intent intent = new Intent(this, transacee_summary.class);
int p_n = single_transaction.get_phone_number();
    Bundle extras = intent.getExtras();
    try {
        extras.putInt("phone_number", p_n);
    } catch (NullPointerException e) {
        Log.e("Null exception at putint", e.getMessage());
    }

    Intent.putExtra("phone_number", p_n);
     startActivity(intent);
}

错误已删除,但是当应用程序运行但在extras.puInt崩溃时,如果不通过此捆绑包,则下一个活动会很好地启动。


所以考虑采取完整的自定义视图行和提取电话号码字段

public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    Intent intent = new Intent(this, transacee_summary.class);
    TextView textv = (TextView) findViewById(R.id.phone_number_show);
    int p_n = Integer.parseInt( textv.getText().toString());

    Intent.putExtra("phone_number", p_n);
    startActivity(intent);
  }

现在错误又来了。
周围发生了什么!

2 个答案:

答案 0 :(得分:2)

应该是:

intent.putExtra("phone_number", p_n);

intent(实例),而不是Intent(类),因为putExtra确实是一个实例方法。

答案 1 :(得分:0)

问题在于这段代码:

Intent intent = new Intent(this, transacee_summary.class);
        int p_n = single_transaction.get_phone_number();
Intent.putExtra("phone_number", p_n);

你需要用Object Intent refrence替换Class Intent refrence,因为putExtra不是静态方法,它是一个实例方法

Intent intent = new Intent(this, transacee_summary.class);
        int p_n = single_transaction.get_phone_number();
intent.putExtra("phone_number", p_n); // see starts with small i

希望这有帮助。