2011-11-25 80 views
6

我有一個應用程序的選項卡。在一個選項卡中,我需要將數據(字符串)放入行中。爲此,我選擇了tableLayout,但是當我想在其行上使用contextmenu時,它不起作用。Android如何使用適配器的listView而不擴展listActivity

我可以顯示contextmenuonLongClick但問題是我無法獲取有關所選行的信息以編輯或刪除所選行。然後我讀了一篇討論,如果我們有很多行,使用listViewtablelayout更好。但我看到的例子擴展爲listactivity,但我不想這樣做。

所以,當我試穿listView工作不延長listactivity我不知道該怎麼做我的意思是,我從來沒有使用過listView之前,所以我嘗試我發現在互聯網上了解其不同的例子,但它不工作。這是我做了迄今爲listView

String [] items=getRessources().getStringArray(R.arra.resolution); 
//Resolution is an array of strings 
ListView lv=(ListeView) findViewById(R.id.listView); 
v.setAdapter(new ArrayAdapter<string>(this, android.R.layout.simple_list_item_1, items); 

當我編譯它,我得到與我在裏面數組元素的列表,但首先,我想改變的文字,我不能的顏色。其次,我想動態地將行添加到我不知道如何做的列表中。我想我必須使用adapter來做到這一點,但我不知道如何。 有人可以引導我通過這個。我只是想知道如何進入我的列表附加到adapter which'll允許我動態添加行,添加contextMenu

+0

爲什麼你不想擴展ListActivity? – kaspermoerch

+0

@KasperMoerch其實我是新來的android dev,我已經遇到了一些元素的麻煩,我認爲它會帶來更多的困難。 – Anila

+0

如果你使用的是TabActivity,每個Tab都會保存一個Activity。如果包含您提到的列表的「Activity」將其作爲主要目的,那麼擴展ListActivity就更容易了。如果你不想這樣做,你需要實現你自己定製的「適配器」,以實現上述功能(無論如何,你必須這樣做,如果你想能夠添加項目到列表上的飛)。 – kaspermoerch

回答

6

主要活動類:

import java.util.ArrayList; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.ListView; 

public class SelectedActivity extends Activity { 

private SelectedAdapter selectedAdapter; 
private ArrayList<String> list; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.selected_example); 

    // populate the model - a simple a list 
    list = new ArrayList<String>(); 
    list.add("Apple"); 
    list.add("Orange"); 
    list.add("Grape"); 
    list.add("Grape1"); 
    list.add("Grape2"); 
    list.add("Grape3"); 
    list.add("Grape4"); 
    list.add("Grape5"); 
    list.add("Grape6"); 

    // create our SelectedAdapter 
    selectedAdapter = new SelectedAdapter(this,0,list); 
    selectedAdapter.setNotifyOnChange(true); 

    ListView listview = (ListView) findViewById(R.id.listExample); 
    listview.setAdapter(selectedAdapter); 

    listview.setOnItemClickListener(new OnItemClickListener() { 
     //@Override 
     public void onItemClick(AdapterView arg0, View view, 
             int position, long id) { 
      // user clicked a list item, make it "selected" 
      selectedAdapter.setSelectedPosition(position); 
     } 
    }); 
} 

適配器類:

import java.util.List; 
    import android.content.Context; 
    import android.graphics.Color; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.ArrayAdapter; 
    import android.widget.Button; 
    import android.widget.TextView; 

    public class SelectedAdapter extends ArrayAdapter{ 

     // used to keep selected position in ListView 
     private int selectedPos = -1; // init value for not-selected 

     public SelectedAdapter(Context context, int textViewResourceId, 
         List objects) { 
      super(context, textViewResourceId, objects); 
     } 
     public void setSelectedPosition(int pos){ 
     selectedPos = pos; 
      // inform the view of this change 
      notifyDataSetChanged(); 
     } 
     public int getSelectedPosition(){ 
      return selectedPos; 
     } 
     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
       View v = convertView; 
       // only inflate the view if it's null 
       // if (v == null) { 
         LayoutInflater vi = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
         v = vi.inflate(R.layout.selected_row, null); 
       // } 

       // get text view 
        TextView label = (TextView)v.findViewById(R.id.txtExample); 
        Button btn=(Button)v.findViewById(R.id.btn1); 

        // change the row color based on selected state 
        if(selectedPos == position){ 
         label.setBackgroundColor(Color.CYAN); 
         btn.setBackgroundResource(R.drawable.next); 
        } 
        else{ 
         label.setBackgroundColor(Color.WHITE); 
        } 

        label.setText(this.getItem(position).toString());  
        return(v); 
     } 
} 
+0

非常感謝答案,只有一個問題,vi.inflate(R.layout.selected_row,null)中引用了selected_Row。 – Anila

+0

對於selected_example也是同樣的問題。這些是您創建的佈局嗎?如果是,它們包含什麼? – Anila

+0

好吧,我創建了兩個佈局:一個與listView和另一個與按鈕和testView和我沒有得到編譯錯誤,但沒有任何反應,當我點擊一行。 :■ – Anila

0

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/bg8" 
    android:id="@+id/RootView" 
    > 
     <LinearLayout android:id="@+id/myLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent"> 
     </LinearLayout> 

</LinearLayout> 

您需要定義將被用來保存每行的數據的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:weightSum="1"> 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="793dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.23" > 

     <TextView android:id="@+id/col1" 
      android:layout_height="fill_parent" 
      android:layout_width="wrap_content" 
      android:width="50dp" 
      android:textSize="18sp" 

     /> 
     <TextView android:id="@+id/col2" 
      android:layout_height="fill_parent" 
      android:layout_width="wrap_content" 
      android:width="150dp" 
      android:textSize="18sp" 
     /> 
     <ImageView android:id="@+id/editimage" 
      android:background="@drawable/edit" 
      android:clickable="true" 
      android:onClick="ClickHandlerForEditImage" 
      android:layout_width="35dp" 
     android:layout_height="35dp"/> 

    </TableRow> 

</LinearLayout> 

在上面的XML我也包括在內,它是不是真的需要ImageView的,但是這僅僅是更新你我們也可以包含其他控件。

&在最後,你應該在你的相關類的函數:

private void LoadData() 
{ 

    DBAdapter db = new DBAdapter(this); 
    db.open(); 
    Cursor cur = db.GetData(); 
    private ListView lv = (ListView)findViewById(R.id.myLayout); 
    lv.setAdapter(null); 
    if(cur.moveToFirst()) 
    { 
      String[] from = new String[] {"_id","column1"}; 
      int[] to = new int[] {R.id.col1, R.id.col2}; 
      SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.grid_item, cur, from, to); 
      lv.setAdapter(adapter); 

    } 
     db.close(); 
     } 
} 
0

看來,沒有人回答了你的ContextMenu問題。爲了得到一個上下文菜單列表的工作,你打電話後ListView yourList = getListView();必須調用registerForContextMenu(yourList);

,並處理菜單的創作,你必須實現的方法

@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){ 
super.onCreateContextMenu(context, v,menuInfo); 
MenuInflater inflater = getMenuInflater(); 
menu.setHeaderTitle("YOUR TITLE"); 
menu.setHeaderIcon(R.drawable.YOUR DRAWABLE); 
inflater.inflate(R.menu.YOUR_MENU_RESOURCE, menu); 
} 

然後你就可以implenting的響應點擊方法

@Override public boolean onContextItemSelected(MenuItem item){ 
switch(item.getItemId()){ 
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected 
return true; 
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected 
return true; 
case R.id.YOUR_MENU_ITEM: // do stuff if the item is selected 
return true; 
} 
return false; // nothing selected 
}