的ListView / ListAdapter与Android项目2串 [英] ListView / ListAdapter with 2 strings in Android project

查看:171
本文介绍了的ListView / ListAdapter与Android项目2串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的Andr​​oid项目。我按照教程从 http://www.vogella.com/articles/AndroidSQLite/article.html 但我坚持的东西。教程演示如何使用级与1 String对象。我有2个String对象。所以我改变了一些东西(添加新的字符串上我的课,改layout.simple_list_item_1到android.R.layout.simple_list_item_2等),现在的问题是 - 如何让事情得到Stoliki类对象(覆盖的toString()给我只有1项,所以也没用)。

类Stoliki

 公共类Stoliki {
      私人长期身份证;
      私人字符串NUMER;
      私人字符串OPIS;      众长的getId(){
        返回ID;
      }      公共无效SETID(长ID){
        this.id = ID;
      }      公共字符串getNumer(){
        返回NUMER;
      }      公共无效setNumer(字符串NUMER){
        this.numer = NUM​​ER;
      }      公共字符串getOpis(){
            返回OPIS;
        }      公共无效setOpis(字符串OPIS){
            this.opis = OPIS;
        }    }

活动

 进口android.app.ListActivity;
进口android.os.Bundle;
进口的java.util.List;
进口了java.util.Random;
进口android.view.View;
进口android.widget.ArrayAdapter;公共类FirstGridPage扩展ListActivity {
      私人StolikiDataSource数据源;  @覆盖
  公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_view_list_stoliki);    数据源=新StolikiDataSource(本);
    datasource.open();    清单< Stoliki>值= datasource.getAllStoliki();    //使用SimpleCursorAdapter显示
    //在ListView元素
    ArrayAdapter< Stoliki>适配器=新ArrayAdapter< Stoliki>(这一点,
        android.R.layout.simple_list_item_1,价值观);
    setListAdapter(适配器);
  }  //将通过onclick属性被称为
  // main.xml中的按钮
  公共无效的onClick(查看视图){
    @燮pressWarnings(未登记)
    ArrayAdapter< Stoliki>适配器=(ArrayAdapter&所述; Stoliki&GT)getListAdapter();
    Stoliki stolik = NULL;
    开关(view.getId()){
    案例R.id.add:
      的String [] = stoliki_numer新的String [] {1,2,3};
      的String [] = stoliki_opis新的String [] {Czerwony,Niebieski,Zielony};
      INT nextInt =新随机()nextInt(3)。
      //保存到数据库中的新评论
      stolik = datasource.createStolik(stoliki_numer [nextInt],stoliki_opis [nextInt]);
      adapter.add(stolik);
      打破;
    案例R.id.delete:
      如果(getListAdapter()的getCount()方式> 0){
          stolik =(Stoliki)getListAdapter()的getItem(0)。
        datasource.deleteStolik(stolik);
        adapter.remove(stolik);
      }
      打破;
    }
    adapter.notifyDataSetChanged();
  }  @覆盖
  保护无效onResume(){
    datasource.open();
    super.onResume();
  }  @覆盖
  保护无效的onPause(){
    datasource.close();
    super.onPause();
  }}


解决方案

HTTP:// WWW。 youtube.com/watch?v=wDBM6wVEO70 。由罗曼人(在谷歌Android开发者)列表视图的谈话。

main.xml中


   

 < ListView的机器人:ID =@ + ID /列表
机器人:layout_width =FILL_PARENT
机器人:layout_height =0dip
机器人:focusableInTouchMode =假
 机器人:listSelector =@机器人:彩色/透明
 机器人:layout_weight =2
 机器人:headerDividersEnabled =假
 机器人:footerDividersEnabled =假
 机器人:dividerHeight =8DP
 机器人:分=#000000
 机器人:cacheColorHint =#000000
 机器人:drawSelectorOnTop =假>
 < /&的ListView GT;
 < / LinearLayout中>

Customw一行。 row.xml

 <?XML版本=1.0编码=UTF-8&GT?;
 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT
   机器人:方向=横向
   机器人:背景=#FFFFFF
  > <的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=TextView的
    机器人:背景=@绘制/ itembkg
    /> <的TextView
    机器人:ID =@ + ID / textView2
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =20dp
    机器人:文字=TextView的/> < / LinearLayout中>公共类CustomListView延伸活动{
/ **当第一次创建活动调用。 * / ListView的LV1;
 Customlistadapter CUS;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
   //键b =(按钮)findViewById(R.id.remove);
    LV1 =(ListView控件)findViewById(R.id.list);
    CUS =新Customlistadapter(本);
    lv1.setAdapter(CUS);}
   }

自定义列表适配器。膨胀的自定义布局的每一行。

 公共类Customlistadapter扩展ArrayAdapter {私人LayoutInflater mInflater;
    上下文℃;公共Customlistadapter(CustomListView customListView){
    超级(customListView,0);
    // TODO自动生成构造函数存根
    this.mInflater = LayoutInflater.from(customListView);
    C = customListView;
}
公众诠释的getCount(){
    返回20; //列表视图的行数。
}公共对象的getItem(INT为arg0){
    返回将arg0;
}众长getItemId(INT为arg0){
返回将arg0;
}公共查看getView(最终诠释为arg0,ARG1查看,ViewGroup中ARG2){
    最后ViewHolder VH;
    VH =新ViewHolder();    如果(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);
     }
    其他
    {
     arg1.setTag(VH);
    }
        vh.tv1.setText(你好);
        vh.tv2.setText(你好);    返回ARG1;
}  静态类ViewHolder //使用平滑滚动和性能viewholder。
  {
TextView的TV1,TV2; }
 }

编辑:

您的活动将有一个列表视图。这在OnCreate中的setContentView(R.layout.activity_main)设置;.主要布局将有一个列表视图。您可以设置列表视图的适配器listview.setAdapter(youradapter);

然后列表视图将拥有自定义布局,即row.xml膨胀的每一行项目。对于列表视图您自定义适配器是其中row.xml膨胀。你定义它扩展ArrayAdapter类CustomAdapter。你重写了一套方法。

  getCount将()---列表视图的大小。
   的getItem(INT位置) - 返回的位置
   getView(INT位置,查看convertView,父母的ViewGroup)
   //位置在列表视图的位置。
   // convertview - 认为是砥膨胀
   //你将返回被infated的看法。

您必须使用平滑滚动和性能viewholder。试想一下,1000行是lstview它可能会导致内存不足异常图像。摆脱一种方法是回收再利用的意见。可见次(行)不回收。在顶部的链接的视频有一个详细的解释的话题

activity_main.xml中

 <?XML版本=1.0编码=UTF-8&GT?;
 LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:layout_width =FILL_PARENT
 机器人:layout_height =FILL_PARENT
 机器人:方向=垂直
 机器人:背景=#0095FF> < ListView的机器人:ID =@ + ID /列表
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =0dip
  机器人:focusableInTouchMode =假
  机器人:listSelector =@机器人:彩色/透明
  机器人:layout_weight =2
  机器人:headerDividersEnabled =假
  机器人:footerDividersEnabled =假
  机器人:dividerHeight =8DP
  机器人:分=#000000
  机器人:cacheColorHint =#000000
  机器人:drawSelectorOnTop =假>
  < /&的ListView GT;
 < / LinearLayout中>

row.xml(布局充气每个列表视图行)

 <?XML版本=1.0编码=UTF-8&GT?;
 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
 机器人:layout_width =match_parent
 机器人:layout_height =match_parent
 机器人:方向=横向> <的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:文字=头文件/> <的TextView
    机器人:ID =@ + ID / textView2
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_marginLeft =80dp
    机器人:layout_gravity =中心
    机器人:文字=TextView的/>< / LinearLayout中>

MainActivity

 公共类MainActivity延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    LL的ListView =(ListView控件)findViewById(R.id.list);
    CustomAdapter CUS =新CustomAdapter();
    ll.setAdapter(CUS);}类CustomAdapter扩展了BaseAdapter
{
    LayoutInflater mInflater;
    公共CustomAdapter()
    {
        mInflater =(LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @覆盖
    公众诠释的getCount(){
        // TODO自动生成方法存根
        返回30;
    }    @覆盖
    公共对象的getItem(INT位置){
        // TODO自动生成方法存根
        返回的位置;
    }    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回0;
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        // TODO自动生成方法存根
        最后ViewHolder VH;
        VH =新ViewHolder();        如果(convertView == NULL)
         {
            convertView = mInflater.inflate(R.layout.row,父母,假);            vh.tv2 =(TextView中)convertView.findViewById(R.id.textView2);
            vh.tv1 =(TextView中)convertView.findViewById(R.id.textView2);
         }
        其他
        {
         convertView.setTag(VH);
        }
            vh.tv1.setText(我的文字);
            vh.tv2.setText(当前位置=+位置);
        返回convertView;
    } 类ViewHolder
 {
    TextView的TV1,TV2;
 }
 }
}

I am working on Android project. I follow tutorial from http://www.vogella.com/articles/AndroidSQLite/article.html but I stuck on something. Tutorial shows how to use Class with 1 String object. I am working with 2 String objects. So I changed few things (add new String to my class, change layout.simple_list_item_1 to android.R.layout.simple_list_item_2 etc.) And now the question is - how to make something to get Stoliki class objects (override toString() gives me only 1 item, so It's useless).

Class 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;
        }

    } 

Activity

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();
  }

} 

解决方案

http://www.youtube.com/watch?v=wDBM6wVEO70. Listview talk by Romain guy( android developer at google).

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. 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);

}        
   }

Custom list adapter. Inflate custom layout for each row.

 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;

 }
 }

Edit:

Your activity will have a listview. This is set in oncreate setContentView(R.layout.activity_main);. The main layout will have a listview. You set the adapter of listview as listview.setAdapter(youradapter);

Then listview will have custom layout ie row.xml inflated for each row item. You custom adapter for listview is where the row.xml is inflated. You defined your class CustomAdapter which extends ArrayAdapter. You override a set of methods.

   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. 

You will have to use a viewholder for smooth scrolling and performance. Imagine 1000 rows is lstview with images it may cause memory exceptions. One way to get rid of this is to recycle views. The visible views(rows) are not recycled. The video in the link at the top has a detail explanation on the topic

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 (layout inflated for each listview row)

 <?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;
 }  
 }
}

这篇关于的ListView / ListAdapter与Android项目2串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆