2013-04-10 45 views
0

我正在開發Android項目。我遵循http://www.vogella.com/articles/AndroidSQLite/article.html的教程,但我堅持的東西。教程展示瞭如何使用帶有1個String對象的Class。我正在處理2個String對象。所以我改變了一些東西(將新的String添加到我的類中,將layout.simple_list_item_1改爲android.R.layout.simple_list_item_2等)。現在問題是 - 如何使某些東西獲得Stoliki類對象(重寫toString()只有1件,所以沒用)。在Android項目中使用2個字符串的ListView/ListAdapter

類Stoliki

public class Stoliki { 
     private long id; 
     private String numer; 
     private String opis; 

     public long getId() { 
     return id; 
     } 

     public void setId(long id) { 
     this.id = id; 
     } 

     public String getNumer() { 
     return numer; 
     } 

     public void setNumer(String numer) { 
     this.numer = numer; 
     } 

     public String getOpis() { 
      return opis; 
     } 

     public void setOpis(String opis) { 
      this.opis = opis; 
     } 

    } 

活性
import android.app.ListActivity; 
import android.os.Bundle; 
import java.util.List; 
import java.util.Random; 
import android.view.View; 
import android.widget.ArrayAdapter; 

public class FirstGridPage extends ListActivity { 
     private StolikiDataSource datasource; 

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

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

    List<Stoliki> values = datasource.getAllStoliki(); 

    // Use the SimpleCursorAdapter to show the 
    // elements in a ListView 
    ArrayAdapter<Stoliki> adapter = new ArrayAdapter<Stoliki>(this, 
     android.R.layout.simple_list_item_1, values); 
    setListAdapter(adapter); 
    } 

    // Will be called via the onClick attribute 
    // of the buttons in main.xml 
    public void onClick(View view) { 
    @SuppressWarnings("unchecked") 
    ArrayAdapter<Stoliki> adapter = (ArrayAdapter<Stoliki>) getListAdapter(); 
    Stoliki stolik = null; 
    switch (view.getId()) { 
    case R.id.add: 
     String[] stoliki_numer = new String[] { "1", "2", "3" }; 
     String[] stoliki_opis = new String[] { "Czerwony", "Niebieski", "Zielony" }; 
     int nextInt = new Random().nextInt(3); 
     // Save the new comment to the database 
     stolik = datasource.createStolik(stoliki_numer[nextInt], stoliki_opis[nextInt]); 
     adapter.add(stolik); 
     break; 
    case R.id.delete: 
     if (getListAdapter().getCount() > 0) { 
      stolik = (Stoliki) getListAdapter().getItem(0); 
     datasource.deleteStolik(stolik); 
     adapter.remove(stolik); 
     } 
     break; 
    } 
    adapter.notifyDataSetChanged(); 
    } 

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

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

} 
+1

使用自定義列表適配器 – Raghunandan 2013-04-10 09:04:19

+0

只是一個提示:總是使用英語變量名稱。這真是醜陋的「setOpis」,即使它只是訓練應用程序,使用英文名稱來訓練和習慣它。 – Mateusz 2013-04-10 09:31:05

+0

是的。這是醜陋的,但只爲我們(波蘭人)。無論如何,我仍然在學習Java,閱讀代碼更加清晰。 – boski 2013-04-10 10:57:53

回答

1

http://www.youtube.com/watch?v=wDBM6wVEO70。 ListView由Romain guy(谷歌Android開發人員)發言。

main.xml中

<ListView android:id="@+id/list" 
android:layout_width="fill_parent" 
android:layout_height="0dip" 
android:focusableInTouchMode="false" 
android:listSelector="@android:color/transparent" 
android:layout_weight="2" 
android:headerDividersEnabled="false" 
android:footerDividersEnabled="false" 
android:dividerHeight="8dp" 
android:divider="#000000" 
android:cacheColorHint="#000000" 
android:drawSelectorOnTop="false"> 
</ListView> 
</LinearLayout> 

Customw行。 row.xml

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

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="TextView" 
    android:background="@drawable/itembkg" 
    /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="20dp" 
    android:text="TextView" /> 

</LinearLayout> 

public class CustomListView extends Activity { 
/** Called when the activity is first created. */ 

ListView lv1; 
Customlistadapter cus; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    // Button b= (Button) findViewById(R.id.remove); 
    lv1 = (ListView) findViewById(R.id.list); 
    cus= new Customlistadapter(this); 
    lv1.setAdapter(cus); 

}   
    } 

自定義列表適配器。爲每行填充自定義佈局。

public class Customlistadapter extends ArrayAdapter { 

private LayoutInflater mInflater; 
    Context c; 

public Customlistadapter(CustomListView customListView) { 
    super(customListView, 0); 
    // TODO Auto-generated constructor stub 
    this.mInflater = LayoutInflater.from(customListView); 
    c=customListView; 
} 
public int getCount() { 
    return 20; // number of listview rows. 
} 

public Object getItem(int arg0) { 
    return arg0; 
} 

public long getItemId(int arg0) { 
return arg0; 
} 

public View getView(final int arg0, View arg1, ViewGroup arg2) { 
    final ViewHolder vh; 
    vh= new ViewHolder(); 

    if(arg1==null) 
    { 
     arg1=mInflater.inflate(R.layout.row, arg2,false); 
     vh.tv1= (TextView)arg1.findViewById(R.id.textView1); 
     vh.tv2= (TextView)arg1.findViewById(R.id.textView2); 
    } 
    else 
    { 
    arg1.setTag(vh); 
    } 
     vh.tv1.setText("hello");  
     vh.tv2.setText("hello"); 

    return arg1; 
} 

    static class ViewHolder //use a viewholder for smooth scrolling and performance. 
    { 
TextView tv1,tv2; 

} 
} 

編輯:

您的活動將有一個列表視圖。這在oncreate setContentView(R.layout.activity_main);中設置。主佈局將有一個列表視圖。您將listview的適配器設置爲listview.setAdapter(youradapter);

然後,listview將有自定義佈局,即爲每個行項目充氣的row.xml。您爲listview定製的適配器是row.xml充滿的地方。您定義了擴展ArrayAdapter的類CustomAdapter。你重寫一組方法。

getCount() --- size of listview. 
    getItem(int position) -- returns the position 
    getView(int position, View convertView, ViewGroup parent) 
    // position is the position in the listview. 
    //convertview - view that is tobe inflated 
    // you will return the view that is infated. 

你將不得不使用一個視圖的平滑滾動和性能。想象一下,1000行是與圖像一起查看可能會導致內存異常。擺脫這種情況的一種方法是回收視圖。可見視圖(行)不會被回收。在頂部的鏈接的視頻對主題

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="#0095FF"> 

<ListView android:id="@+id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="0dip" 
    android:focusableInTouchMode="false" 
    android:listSelector="@android:color/transparent" 
    android:layout_weight="2" 
    android:headerDividersEnabled="false" 
    android:footerDividersEnabled="false" 
    android:dividerHeight="8dp" 
    android:divider="#000000" 
    android:cacheColorHint="#000000" 
    android:drawSelectorOnTop="false"> 
    </ListView> 
</LinearLayout> 

row.xml(佈局充氣每個列表視圖行)詳細說明

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

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:text="Header" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="80dp" 
    android:layout_gravity="center" 
    android:text="TextView" /> 

</LinearLayout> 

MainActivity

public class MainActivity extends Activity { 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ListView ll = (ListView) findViewById(R.id.list); 
    CustomAdapter cus = new CustomAdapter(); 
    ll.setAdapter(cus); 

} 

class CustomAdapter extends BaseAdapter 
{ 
    LayoutInflater mInflater; 


    public CustomAdapter() 
    { 
     mInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 
    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return 30; 
    } 

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

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     final ViewHolder vh; 
     vh= new ViewHolder(); 

     if(convertView==null) 
     { 
      convertView=mInflater.inflate(R.layout.row, parent,false); 

      vh.tv2= (TextView)convertView.findViewById(R.id.textView2); 
      vh.tv1= (TextView)convertView.findViewById(R.id.textView2); 


     } 
     else 
     { 
     convertView.setTag(vh); 
     } 
      vh.tv1.setText("my text"); 
      vh.tv2.setText("Postion = "+position); 
     return convertView; 
    } 

class ViewHolder 
{ 
    TextView tv1,tv2; 
} 
} 
} 

enter image description here

+1

不工作。這對我來說也太難了。 – boski 2013-04-10 10:55:58

+0

@boski它的工作原理。困難的方式,我不明白。你想讓我發佈相同的快照? – Raghunandan 2013-04-10 11:38:11

+0

@boski檢查編輯可能會幫助你。它有效,但你必須做對。因爲您可以在一行中看到快照兩個文字瀏覽。 – Raghunandan 2013-04-10 12:02:09

相關問題