使用Android中的BaseAdapter将字符串任意值添加到ListView

时间:2014-08-02 00:50:42

标签: android listview baseadapter

我一直在网上搜索如何使用BaseAdapter在ListView中添加可修改的文本/字符串但到目前为止没有运气。这就是我想要做的事情,如下所示:

              Item_1      $price1
              Item_2      $price2

上面显示的格式是我希望listView采用的模板格式。 Item_1和$ price1在第一行,而item_2和$ price2在第二行。我不想要一个预定义的列表,而是一个用户应该能够添加项目及其价格的列表,并且应该能够相应地修改它。

下面是我试图在ToDoAdapter扩展BaseAdapter类中使用的代码的一部分:

    public View getView(int position, View convertView, ViewGroup parent) {  
           View v = convertView;  
           CompleteListViewHolder viewHolder;  
           if (convertView == null) {  
                LayoutInflater inflater = (LayoutInflater) mContext  
                          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
                v = inflater.inflate(R.layout.list_layout, null); 


                viewHolder = new CompleteListViewHolder(v);  
                v.setTag(viewHolder);  
           } else {  
                viewHolder = (CompleteListViewHolder) v.getTag();  
           }  

           viewHolder.mTVItem.setText(mList.get(position));
           viewHolder.mTvItem2.setText(mList.get(position)); 

           return v;  
      }  
 }  
 class CompleteListViewHolder {  
      public TextView mTVItem, mTvItem2;  
      public CompleteListViewHolder(View base) {  
          mTVItem = (TextView) base.findViewById(R.id.listTV);  
          mTvItem2 =  (TextView) base.findViewById(R.id.listTv2);
      }

主要活动:

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

          initViews();  
          mItems = new ArrayList<String>();  
          mListAdapter = new CompleteListAdapter(this, mItems);  
          mCompleteListView.setAdapter(mListAdapter);  
     }  

     private void initViews() {  
          mCompleteListView = (ListView) findViewById(R.id.completeList);  
          mAddItemToList = (Button) findViewById(R.id.addItemToList);  
          mAddItemToList.setOnClickListener(this);  
     }  

     private void addItemsToList() {  

         mListAdapter.notifyDataSetChanged();
     } 

     @Override  
     public void onClick(View v) {  
          switch (v.getId()) {  
          case R.id.addItemToList:  
               addItemsToList();  
               break;  
          }  
     }  

XML布局:

<?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical" >

     <TextView
       android:id="@+id/listTV"
       android:layout_width="142dp"
       android:layout_height="60dp"
       android:gravity="center_vertical"
       android:text="Income"
       android:textSize="42sp"
       android:typeface="sans" />

   <TextView
       android:id="@+id/listTv2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignBaseline="@+id/listTV"
       android:layout_alignBottom="@+id/listTV"
       android:layout_marginLeft="45dp"
       android:layout_toRightOf="@+id/listTV"
       android:text="Amount"
       android:textAppearance="?android:attr/textAppearanceLarge" />

 </RelativeLayout>  

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=".CompleteListActivity" >  
   <ListView  
     android:id="@+id/completeList"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:layout_alignParentTop="true"  
     android:layout_marginBottom="80dp" >  
   </ListView>  
   <Button  
     android:id="@+id/addItemToList"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_alignParentBottom="true"  
     android:layout_margin="10dp"  
     android:text="@string/add_item" />  
 </RelativeLayout>  

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

您的按钮通知适配器数据已更改,但实际上数据未更改

所以您需要做的是首先修改适配器数据,然后调用notifyDataSetChanged()