2013-09-30 46 views
0

有人知道爲什麼ListActivity將無法​​正常工作,當我將其更改爲Activity基類時,只需稍作更改,但onClick()方法仍然不行。我想只是把一些字符串到列表...下面的代碼:ListActivity Android

public class TestDataBaseActivity extends Activity implements OnClickListener { 
private CommentsDataSource datasource; 
ListView m_list; 

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

    datasource = new CommentsDataSource(this); 
    datasource.open(); 

    List<Comment> values = datasource.getAllComments(); 
    m_list = (ListView) findViewById(R.id.listView1); 
    // Use the SimpleCursorAdapter to show the 
    // elements in a ListView 
    ArrayAdapter<Comment> adapter = new ArrayAdapter<Comment>(this, 
      android.R.layout.simple_list_item_1, values); 
    m_list.setAdapter(adapter); 

    Button add = (Button)findViewById(R.id.button_add); 
    Button delete = (Button)findViewById(R.id.button_delete); 

    add.setOnClickListener(this); 
    delete.setOnClickListener(this); 

} 

// Will be called via the onClick attribute 
// of the buttons in main.xml 
@Override 
public void onClick(View view) { 
    @SuppressWarnings("unchecked") 
    ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) m_list.getAdapter(); 
    Comment comment = null; 
    switch (view.getId()) { 
     case R.id.button_add: 
      String[] comments = new String[] { 
        "Cool", "Very nice", "Hate it" 
      }; 
      int nextInt = new Random().nextInt(3); 
      // Save the new comment to the database 
      comment = datasource.createComment(comments[nextInt]); 
      adapter.add(comment); 
      break; 
     case R.id.button_delete: 
      if (m_list.getAdapter().getCount() > 0) { 
       comment = (Comment) m_list.getAdapter().getItem(0); 
       datasource.deleteComment(comment); 
       adapter.remove(comment); 
      } 
      break; 
    } 
    adapter.notifyDataSetChanged(); 
} 

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

@Override 
protected void onPause() { 
    datasource.close(); 
    super.onPause(); 
} 

} 

這裏是XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".TestDataBaseActivity" > 

<Button 
    android:id="@+id/button_add" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/add_new" /> 

<Button 
    android:id="@+id/button_delete" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/button_add" 
    android:text="@string/delete_first" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/button_add" 
    android:padding="15dp" > 

</ListView> 

</RelativeLayout> 

我現在編輯的擴展活動,現在「唯一」的onClick( )不工作... 在此先感謝!

+0

你可以發佈你的xml嗎? – Beginner

+0

我也嘗試覆蓋onListItemClick()方法Jin建議,但仍然不工作... –

回答

0

你應該實現OnClickListener

public class TestDatabaseActivity extends ListActivity implements 
    OnClickListener{ 
+0

它只是不工作時,我擴展ListActivity沒有覆蓋的將有助於... –

+0

也你應該初始化你的R.id.add和R. id.delete並將其onclicks設置爲view.setOnClickListener(this); – invisbo

+0

現在它的工作,我只需要實現OnClickListener和擴展活動類,但我仍然不知道爲什麼ListActivity不會工作。謝謝 –

1

對於ListActivity,你應該重寫onListItemClick()代替:

@Override 
protected void onListItemClick(ListView lv, View v, int position, long id){ 


} 
0

試試下面的代碼的簡單列表活動

public class MainActivity extends ListActivity implements OnItemClickListener { 

ListView listView; 
static final String[] SPORTS = {"Shuttle Badminton", "Tennis", "FootBall", 
    "Basket Ball","Table Tennis", "Chess","Hockey"}; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    listView = getListView(); 

    //setting adapter 
    listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),R.layout.list_item,SPORTS)); 
    listView.setOnItemClickListener(this); 
} 

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // TODO Auto-generated method stub 
    TextView tv = (TextView)view.findViewById(R.id.text1); 
    Toast.makeText(getApplicationContext(),"Position is: "+position,Toast.LENGTH_SHORT).show(); 
    Toast.makeText(getApplicationContext(),"Text is: "+tv.getText().toString(),Toast.LENGTH_SHORT).show(); 
} 
} 

here list_item is an xml having following code 
<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/text1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:textColor="@android:color/black" 
android:padding="10dp" /> 

使用此代碼列出活動並根據你的n修改行xml電火工品。 希望這有助於..

1

既然要擴展ListActivity你需要有一個Listviewandroid:id="@android:id/list".,如果你想使用@+id/lst_id然後

不延長ListActivity,只是擴展活動。

而且,無論何時使用Listview,您都需要執行OnItemClickListener而不是OnClickListener

,並覆蓋默認OnItemClick方法

@Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 

    } 
0

這是你哪裏不舒服:

ArrayAdapter<Comment> adapter = (ArrayAdapter<Comment>) m_list.getAdapter(); 

建立適配器一個全局對象來代替。 (聲明之前onCreate())