【问题标题】:Can't get view to set elements of LISTVIEW无法查看设置 LISTVIEW 的元素
【发布时间】:2015-08-16 17:23:00
【问题描述】:

“listView”有问题。我到处搜索,但问题没有出现。让我解释一下:

我的“布局”中有经典的“列表视图”:

<?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:id="@+id/roza"
android:tileMode="repeat">

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#80ffffff"
    android:alpha="200">
</ListView>

<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/no_todos" />

 </LinearLayout>

我创建自定义 list_row xml:

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


    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"
    android:id="@+id/list_name"
    android:layout_alignParentTop="true"
    android:textSize="25dp"
    android:layout_alignParentStart="true"
    android:layout_alignEnd="@+id/list_rune" />

   ------------bla bla bla -----------------
   <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Text"

    android:layout_alignTop="@+id/list_date_update"
    android:layout_toLeftOf="@+id/list_rune"
    android:layout_toStartOf="@+id/list_rune"
    android:id="@+id/textTester" />


    </RelativeLayout>

我的主要课程 -

  public class MainActivity extends ListActivity implements         AdapterView.OnItemLongClickListener,       SharedPreferences.OnSharedPreferenceChangeListener {


     private NordBase dbHelper;
   private Cursor cursor;
  int rage = list_row; // my custom row

  -------------------on create----------------
 setContentView(R.layout.activity_main);
 this.getListView().setDividerHeight(2);

       dbHelper = new NordBase(this);
       fillData();

-----------------fillData-----------------

   cursor = dbHelper.getAll();
       startManagingCursor(cursor);


       String[] from = new String[] { NordBase.NORD_NAME,    NordBase.NORD_DESCRIPTION,
                                    NordBase.NORD_PUNKTS,    NordBase.NORD_PUNKTS_CROSS,
            NordBase.NORD_DATE_CREATE, NordBase.NORD_DATE_UPDATE, NordBase.NORD_RUNE,
            NordBase.NORD_IMPORTANT, NordBase.NORD_SUM };

      int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
            R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
            R.id.list_rune, R.id.list_important, R.id.list_sum, };

    notes = new SimpleCursorAdapter(this,
            rage, cursor, from, to);


    setListAdapter(notes);
    getListView().setOnItemLongClickListener(this);

唉,我无法访问 list_row 中的项目,为比较运算符绑定它们。对于 exp:如果 price == 0,则 textview.setvisible = false...

我尝试了 LayoutInflater,但他使视图元素与列表视图中的元素不同。 我尝试了适配器活页夹类 - 但我认为它对此没有好处。 大多数情况下我有一个错误 - 不能为“空元素”做那个......这种情况 - 看起来就像我所做的一切 - 我的程序在 list_row 中找不到 ViewById 元素。

只有一个联系方式

     int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
                  R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
                R.id.list_rune, R.id.list_important, R.id.list_sum, };

          notes = new SimpleCursorAdapter(this,
                rage, cursor, from, to);

所以请帮助我 Obi-Van Kenobi,并为我的假 ENG 感到抱歉,我不确定我是否以正确的方式过去了 cod)))

【问题讨论】:

  • 您能否更具体地说明问题所在?
  • 我试图做到尽可能具体))我想在 list_row 中找到 ViewByID 元素。正如我在下面学习答案 - 我需要制作自定义适配器类,没有它我无法忍受)))

标签: android listview simplecursoradapter listactivity


【解决方案1】:

首先你的布局没有你在适配器中提到的视图

R.id.list_description, R.id.list_punkts,
                  R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
                R.id.list_rune, R.id.list_important, R.id.list_sum

把这个东西放到布局list_row xml中 如果你的意思是------------bla bla bla ----- 这个意见比ok simpleCursorAdapter 也不会解决您的目的,因为您想根据各自视图中的数据隐藏和显示视图 所以我的第一个建议将您的布局更改为linearLayouttableLayout 或您感觉的其他布局,然后在您的CustomAdapter 课程中执行此操作 喜欢

class MyAdapter extends BaseAdapter
{
   @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//here you infalteYour layout and and do your logical thing
//you can take your data out from cursor and store in the arrayList<YourModelClass> type and have better control
     }
}

否则在相对布局中只填充数据不要隐藏它,这样它可能会起作用,但如果你隐藏某些视图的机会很高,它不会解决你的问题

还有错别字,我希望如此

int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
                  R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
                R.id.list_rune, R.id.list_important, R.id.list_sum, };

//这里没有项目,但你仍然在 R.id.list_sum 之后写了 ",",就像 "R.id.list_sum,"

          notes = new SimpleCursorAdapter(this,
                rage, cursor, from, to);

【讨论】:

  • 所以((我必须构建自定义适配器。没有它就没有机会获得视图......我担心它(不要了解它)^^谢谢队友的回答......
  • P.S.:有一个多余的“,”但代码工作正常 - 我删除它但都是一样的,我必须去了解 MyAdapter 类扩展 BaseAdapter。非常感谢!
猜你喜欢
  • 2020-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多