2015-04-08 61 views
0

我有一个带有listview的片段。该适配器只有一个字段:项目ID。Android ListView显示空的内容

当我使用适配器将项目添加到列表中时,列表显示项目ID。

但是,如果我把一个简单的物品ID检查之前(一个for-cycle检查是否物品ID是等于一个特定的ID,如果是这样,然后添加物品ID),列表显示一个空行添加了一些东西),但没有显示项目ID。

可能是什么?我确定“检查部分”有效。

编辑:我忘了指定item id是一个String。

编辑2:我添加代码的检查部。它通过list循环,其中包含我想要添加到其他片段列表的项目ID。 item是我正在查看的项目。 blueF是片段。

for(int i=0; i<list.getCount();i++) { 
    if(list.getItem(i).equals(item.getId())) { 
    //send the item to the listview of the fragment 
    blueF.getArrayAdapter().addItem(item); 
    break; 
    } 
} 

编辑3:一些测试后,我发现里面的片段列表视图有项目里(我用getCount将和也被印刷在商品ID),但是它没有显示该项目的ID!我的自定义适配器在“add”方法中有一个notifyDataSetChanged(),并且在调用活动中的add方法之后,我也回忆起notifyDataSetChanged ...但没有任何结果。

编辑4:可能是有些问题的布局? 我有这样的布局列表行:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/inventory_item_container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<TextView 
    android:id="@+id/inventory_item" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="0.4" 
    android:textSize="17sp" 
    android:typeface="monospace" > 

</TextView> 

<TextView 
    android:id="@+id/inventory_item_counter" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:textSize="17sp" 
    android:typeface="monospace" /> 

</LinearLayout> 

片段布局列表视图:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" > 

    <ListView 

    android:id="@+id/listBlueTagView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    </ListView> 

</RelativeLayout> 
+1

你能告诉我们你的代码吗?你是否还核实了检查是否属实,因此确实将该项目添加到列表中? –

+0

张贴您的代码请 –

+0

抱歉,但我不能告诉你的代码。我确定检查是真的。我还在同一行中添加了第二个(整数)字段。适配器添加1行,将整数放入其中但不包含项目标识。 – user261591

回答

0

从你的问题,我理解这一点,

在您的主要活动试试这个,这些只是伪代码,可能有一些错别字。

public class Main extends Activity{ 
EditText etID; //Your Item ID field. 
ListView listView; // Listview in your XML. 
int check_ID; // ID which you want to compare with Edit Texts' value. 
List<String> arrayList= new ArrayList<String>(); 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.Your_Layout_XML); 

etID = (EditText) findViewById(R.id.editTextID); //ITEM_ID editText id in XML 
listView = (ListView) findViewById(R.id.your_ListView_ID); 
check_ID = 1; // suppose you want to add into your list if user enters 1 in editText as Item ID 
if(check_ID = Integer.parseInt(etID.getText().toString())) //get value from EditText then check it with your desired value and on being equal add item to list 
// add item here and set it to listView 
arrayList.add("Mango"); 
ArrayAdapter<String> array_Adapter = new ArrayAdapter<String>(
      this, 
      android.R.layout.simple_list_item_1, 
      arrayList); 

    listView .setAdapter(array_Adapter); 


} else{ 
    Toast.makeText(getApplicationContext(),"Wrong ITEM ID",Toast.LENGTH_SHORT).show();   
    } 

} 
+0

谢谢。 所以基本上你建议检查“int vs int”而不是“string vs string”对不对? – user261591

+0

你说的ID?在你的问题中,你也可以比较字符串和小小的调整。 比较这样的字符串。 String ID =“WhatEva”; 如果(id.equals(editext.getText()。的toString()){在这里做你的工作} –

0

您的过滤器代码应该在设置适配器的数据之前完成,因此适配器只有它将显示的项目。

+0

您好,感谢的提示,但我不能这样做(如果我理解你的意思),因为我必须其他项目添加dinamically – user261591

-1

我已经解决了这个问题。

这是在适配器的getView中生成的图形问题。基本上,一些元素覆盖了ID的颜色文本,使其不可见。

我将文本颜色设置为Color.BLACK,现在我可以看到该项目的ID。