onclicklistener没有在代码中做任何事情,在logcat中没有错误

时间:2014-03-21 02:25:25

标签: android listview onclicklistener

我有最严重的问题。我在我的活动中创建了一个onclick监听器。假设从listview中删除listview项。但点击时它什么都不做。该应用程序在logcat中运行时没有错误。它就像onclicklistener甚至不在代码中。

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class MyEvents extends ActionBarActivity{

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myevents);

    //TextView tv = (TextView)findViewById(R.id.contentlist);
    ListView dbListView = (ListView)findViewById(R.id.myEventsListView);
    final SQLiteAdapter info = new SQLiteAdapter(this);
    //long lg = 1;
    final Cursor myCursor;
    info.open();
    myCursor = info.getAllItems();
    startManagingCursor(myCursor);
    ListAdapter adapter = new SimpleCursorAdapter(MyEvents.this,
            R.layout.single_myevent, myCursor, new String[] { "title", "description", "time", "_id" }, new int[] { R.id.myEventstitle, R.id.myEventsDesc, R.id.myEventsTime, R.id.myeventid }, 0);
dbListView.setAdapter(adapter);

    info.close();
    dbListView.setOnItemClickListener(new OnItemClickListener(){

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        String eventid = (String)view.findViewById(R.id.myeventid).toString();
        long myrow = Long.parseLong(eventid);
        Log.d("onclick", "clicked " + eventid);
        //SQLiteAdapter sqlAd = new SQLiteAdapter(this);
        info.open();
        info.deleteEntry(myrow);
        Toast.makeText(MyEvents.this, "deleted" + eventid, Toast.LENGTH_LONG).show();
        info.close();
        Intent restarteventpage = new Intent(MyEvents.this, MyEvents.class);
        startActivity(restarteventpage);
    }

});
}
}

答案就像是&#39; rIHaN JiTHiN&#39;在上面的评论中说。我的单行xml中有一个按钮和一些textview。一旦我删除了按钮,我就可以单击该行。但是我该如何防止这种情况呢?我对该按钮有未来的计划。

但是现在当我点击一行时我收到logcat中的错误,它说:

Numberformatexception: Invalid long: "android.widget.TextView...app:id/eventid long.invalidlong

但我需要这个textview,它存储我用来识别行的行的id,所以我可以从我的sqlite数据库中删除它。我写这段代码错了吗?还是有另一种方式?

2 个答案:

答案 0 :(得分:0)

如果您尝试将dbListView.setAdapter(adapter);移到dbListView.setOnItemClickListener();下方

,该怎么办?

答案 1 :(得分:0)

解决了我的无效长问题:

而不只是这样做:

String eventid = (String)view.findViewById(R.id.myeventid).toString();
    long myrow = Long.parseLong(eventid);

我首先添加了Textview,然后将文本转换为字符串:

TextView eventid = (TextView)view.findViewById(R.id.myeventid);
        String stringeventid = eventid.getText().toString();
        long myrow = Long.parseLong(stringeventid);

哦,小伙子我真的很擅长这个! :)