Java Android项目中带有2个字符串的ListView/ListAdapter

Java Android项目中带有2个字符串的ListView/ListAdapter,java,android,listview,Java,Android,Listview,我正在从事Android项目。我遵循的教程,但我坚持的东西。本教程演示如何将类与1个字符串对象一起使用。我正在处理2个字符串对象。所以我改变了一些事情(向我的类添加新字符串,将layout.simple_list_item_1更改为android.R.layout.simple_list_item_2等),现在的问题是-如何制作一些东西来获取Stoliki类对象(override toString()只给我1项,所以它是无用的) 斯托利基类 活动 导入android.app.ListActivi

我正在从事Android项目。我遵循的教程,但我坚持的东西。本教程演示如何将类与1个字符串对象一起使用。我正在处理2个字符串对象。所以我改变了一些事情(向我的类添加新字符串,将layout.simple_list_item_1更改为android.R.layout.simple_list_item_2等),现在的问题是-如何制作一些东西来获取Stoliki类对象(override toString()只给我1项,所以它是无用的)

斯托利基类 活动
导入android.app.ListActivity;
导入android.os.Bundle;
导入java.util.List;
导入java.util.Random;
导入android.view.view;
导入android.widget.ArrayAdapter;
公共类FirstGridPage扩展ListActivity{
私有StolikiDataSource数据源;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u view\u list\u stoliki);
datasource=新的StolikiDataSource(此);
datasource.open();
列表值=datasource.getAllStoliki();
//使用SimpleCursorAdapter显示
//ListView中的元素
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,值);
setListAdapter(适配器);
}
//将通过onClick属性调用
//main.xml中按钮的
公共void onClick(视图){
@抑制警告(“未选中”)
ArrayAdapter=(ArrayAdapter)getListAdapter();
Stoliki-stolik=null;
开关(view.getId()){
案例R.id.add:
字符串[]stoliki_numer=新字符串[]{“1”、“2”、“3”};
String[]stoliki_opis=新字符串[]{“Czerwony”、“Niebieski”、“Zielony”};
int nextInt=new Random().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);
适配器。移除(stolik);
}
打破
}
adapter.notifyDataSetChanged();
}
@凌驾
受保护的void onResume(){
datasource.open();
super.onResume();
}
@凌驾
受保护的void onPause(){
datasource.close();
super.onPause();
}
} 
。Romain guy(谷歌android开发者)的Listview演讲

Main.xml

编辑:

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

然后,listview将为每个行项目提供自定义布局ie 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. 
您必须使用viewholder以实现平滑滚动和性能。假设1000行是带有图像的lstview,它可能会导致内存异常。消除这种情况的一种方法是回收视图。不回收可见视图(行)。顶部链接中的视频对此主题进行了详细解释

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>

使用自定义列表适配器提示:始终使用英文变量名。它真的很难看“setOpis”,即使它只是一个训练应用程序,也要用英文名字来训练并适应它。是的。这很难看,但只对我们(波兰人)来说。无论如何,我仍在学习Java,阅读代码对我来说更为清晰。@boski它可以工作。我真的不明白。你想让我发布一个相同的快照吗???@boski检查编辑可能会对你有所帮助。这是可行的,但你必须把它做好。正如您可以看到的那样,快照在一行中显示了两个文本视图。
<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>   
<?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;

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