iT邦幫忙

0

listview 讀取資料 幫幫我

https://ithelp.ithome.com.tw/upload/images/20181105/20109685Lge9f6aIDl.png

請問我想要出現兩筆資料 可是好像第二個會蓋到第一個的 請問我要怎麼改?

小魚 iT邦大師 1 級 ‧ 2018-11-05 12:06:57 檢舉
請直接貼程式碼,
上下都用```包起來就可以了,
而且你沒有貼Layout...
你是指哪裡啊?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nofood);
listView = findViewById(R.id.list);
ser =findViewById(R.id.search);
title = new String[]{"巧克力(可致命)","洋蔥(危險)","肝臟(危險)","骨頭(危險)","生雞蛋(危險)","生肉及家禽肉(危險~致命)","豬肉(不建議餵食)","菇類(不建議~致命)","酒(建議不要)" };
things_use = new String[]{"巧克力2(可致命)","洋蔥2(危險)","肝臟2(危險)","肝臟2(危險)","骨頭2(危險)","生雞蛋2(危險)","生肉及家禽肉2(危險~致命)","豬肉2(不建議餵食)","菇類2(不建議~致命)","酒2(建議不要)" };
arrayList = new ArrayList<>(Arrays.asList(title));
arrayList_things = new ArrayList<>(Arrays.asList(things_use));
adapter = new ArrayAdapter<>(this,R.layout.nofood_use,R.id.title,arrayList);
adapter_things = new ArrayAdapter<>(this,R.layout.nofood_use,R.id.things_use,arrayList_things);
listView.setTextFilterEnabled(true);
listView.setAdapter(adapter);
listView.setAdapter(adapter_things);
ser.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
nofood.this.adapter.getFilter().filter(charSequence);
adapter.notifyDataSetChanged();
}

@Override
public void afterTextChanged(Editable editable) {

}
});
}

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/things_use"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:text="TextView"
android:textAlignment="viewStart"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/title" />
</android.support.constraint.ConstraintLayout>
listView.setAdapter(adapter);
listView.setAdapter(adapter_things);
因為你設定到了兩次Adapter 他指判斷後者的Adapter
你該做改變的地方應該是arrayList_things 或是 arrayList
或是綜合兩個陣列new一個新的陣列來裝你所選擇的東西才對
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
神威
iT邦研究生 4 級 ‧ 2018-11-05 13:26:35
看更多先前的回應...收起先前的回應...

謝各位已找到解決方案,但收尋部分有點問題

錯誤代碼
Process: com.example.wells.ig, PID: 22224
java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter android.widget.ArrayAdapter.getFilter()' on a null object reference
at com.example.wells.ig.nofood$1.onQueryTextChange(nofood.java:101)

程式碼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nofood);
searchView = (SearchView)findViewById(R.id.searchView);
searchView.setIconifiedByDefault(false);// 關閉icon切換
searchView.setFocusable(false); // 不要進畫面就跳出輸入鍵盤
setSearch_function();
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.nofood_use,
new String[]{"title","info","img"},
new int[]{R.id.title,R.id.info,R.id.img});
setListAdapter(adapter);
}

            private List<Map<String, Object>> getData() {
                List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("title", "巧克力(可致命)");
                map.put("info", "它會使輸送部的血液流量減少,可能會造成心臟病和其他有致命威脅的問題。");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "洋蔥(危險)");
                map.put("info", "它對人體無害,卻會造成貓,狗,羊,馬,牛的紅血球氧化的危險。可能引發溶血性貧血(HemolyticAnemia)");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "肝臟(危險)");
                map.put("info", "吃食少量的肝臟其實對狗是不錯的,但如果過量卻可能引起營養問題。因為肝含有高單位的維他命A,吸取太多的維他命A,可能會引起維他命A中毒或維他命A過多症。");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "骨頭(危險)");
                map.put("info", "不要餵食會碎裂的骨頭(如雞骨頭),骨頭碎片可能會刺入狗的喉嚨,或割傷狗的嘴巴,食道,胃或腸。");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "生雞蛋(危險)");
                map.put("info", "生蛋白含有一種卵白素的蛋白質,它會耗盡狗體內的維生素H。維生素H是狗生長及促進毛皮健康不可或缺的營養。");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "生肉及家禽肉(危險~致命)");
                map.put("info", "狗在野外雖會獵食動物,但他們的免疫系統卻無法適應人工科學飼養出的家禽及肉類。常見的2種毒素是家禽肉類中的沙門氏菌及肉類中的芽胞桿菌。");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "豬肉(不建議餵食)");
                map.put("info", "豬肉內的脂肪球比其他肉類還大。可能會阻塞了狗的微血管。避免豬製品,尤其是含有硝酸鈉的培垠、香腸。");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "牛奶(不建議)");
                map.put("info", "許多狗都有乳糖不適症,如果你的狗喝了牛奶後會出現放屁,腹瀉,脫水或皮膚發炎等徵狀,應停止餵食牛奶。");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "菇類(不建議~致命)");
                map.put("info", "市售的食用香菇,磨菇等對狗是無害的,但還是避免讓狗食用,以免到野外時誤食有毒茹類。");
                map.put("img", R.drawable.no);
                list.add(map);
                map = new HashMap<String, Object>();
                map.put("title", "酒(建議不要)");
                map.put("info", "易增加肝、腎負擔。");
                map.put("img", R.drawable.no);
                list.add(map);
                 return list;
}
private void setSearch_function() {
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            adapter.getFilter().filter(newText);

            return true;
        }
    });

}

}

public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);

        return true;
    }
});
這裡的adapter要怎麼改??

xml
 <SearchView
    android:id="@+id/searchView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/view6" />
    
     <ListView
    android:id="@android:id/list"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/searchView" />
ant1017 iT邦新手 2 級 ‧ 2018-11-06 08:32:47 檢舉

提示訊息很明顯了...大大
java.lang.NullPointerException: Attempt to invoke virtual method 'android.widget.Filter android.widget.ArrayAdapter.getFilter()' on a null object reference

ant1017 不太懂 我知道那裏有錯誤 可是我不知道怎麼改

我要發表回答

立即登入回答