2017-02-11 2 views
-2

저는 Android 프로그래밍을 사용하여 대학 프로젝트를 진행하고 있지만 현재 UI 디자인에만 있습니다. ListView가 포함 된 활동에는 ListView를 제외한 모든 항목이 표시됩니다. 어댑터가 목록에 설정되기 전후에 텍스트를 추가하는 코드를 넣으려고했으나 아무 것도 작동하지 않습니다. 나는 어댑터의 카운트에 대한 로그를 사용하여 시도했지만 정확한 시간에 올바른 숫자 (추가하기 전에 0, 추가 한 후 5)를 반환합니다.Android ListView에 데이터가 채워지지만 아무 것도 표시하지 않습니다.

ProductsList.java

public class ProductsList extends AppCompatActivity { 
private ArrayList<String> listProduct=new ArrayList<String>(); 
private ArrayAdapter<String> adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_catalogue); 
    setTitle("CATALOGUE"); 

    TextView tv=(TextView)findViewById(R.id.textViewWelcome); 
    Bundle b=getIntent().getExtras(); 
    tv.setText("Welcome: "+b.getString("UserName")); 

    adapter=new ArrayAdapter<String>(this,R.layout.list,listProduct); 
    ListView lv=(ListView)findViewById(R.id.prodList); 
    lv.setAdapter(adapter); 

    listProduct.add("Product 1"); 
    listProduct.add("Product 2"); 
    listProduct.add("Product 3"); 
    listProduct.add("Product 4"); 
    listProduct.add("Product 5"); 

    adapter.notifyDataSetChanged(); 
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      //some code 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.catalogue,menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id=item.getItemId(); 
    if(id==R.id.action_settings){ 
     Toast.makeText(ProductsList.this,"Menu Setting",Toast.LENGTH_SHORT).show(); 
     return true; 
    } 
    else if(id==R.id.action_1){ 
     Toast.makeText(ProductsList.this,"Menu 1",Toast.LENGTH_SHORT).show(); 
     return true; 
    } 
    else if(id==R.id.action_2){ 
     Toast.makeText(ProductsList.this,"Exit",Toast.LENGTH_SHORT).show(); 
     Intent i=new Intent(Intent.ACTION_MAIN) 
       .addCategory(Intent.CATEGORY_HOME) 
       .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     startActivity(i); 
     finish(); 
     System.exit(0); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 
} 

activity_catalogue.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/catalogue_page" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.uitest.ProductsList"> 

<TextView 
    android:id="@+id/textViewWelcome" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="welcome" /> 

<RelativeLayout 
    android:id="@+id/button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <Button 
     android:id="@+id/btnAdd" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:text="Add Product" /> 

    <Button 
     android:id="@+id/btnTransaction" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@id/btnAdd" 
     android:layout_toRightOf="@id/btnAdd" 
     android:text="View Transaction" /> 

    <Button 
     android:id="@+id/btnClose" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@id/btnTransaction" 
     android:layout_toRightOf="@id/btnTransaction" 
     android:text="Exit" /> 

</RelativeLayout> 

<ListView 
    android:id="@+id/prodList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textViewWelcome" 
    android:layout_above="@+id/button" 
    android:layout_marginTop="10dp"/> 

</RelativeLayout> 

list.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_marginTop="5dp" 
    android:text="Test"> 
</TextView> 

UI에서만 작업 중이므로 사용자 지정 어댑터를 만드는 것을 신경 쓰지 않을 것입니다. 표시 할 작업 표시 줄이 필요하고 코드의 일관성을 유지해야하므로 ListActivity를 사용할 수 없으므로 RelativeLayout을 변경하지는 않습니다.

내가 어떻게 잘못했는지 생각해보십시오. adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listProducts);

+0

, 심지어 R.id.list 빨간색 글자에 너무 것을 시도 @MehmetKologlu하여 어댑터 – DarkGreiga

+0

(해결 될 수없는)되고, 여전히 작동하지 않습니다. –

+0

의 인스턴스화 전에 목록 부분에 추가로 이동 – DarkGreiga

답변

0

사용이 귀하의 목록보기 android:layout_belowandroid:layout_above 속성에서 +를 제거합니다.

<ListView 
    android:id="@+id/prodList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/textViewWelcome" 
    android:layout_above="@id/button" 
    android:layout_marginTop="10dp"/> 
@PavneetSingh이 작동하지 않는
+0

입니다. 그것은 심지어 컴파일하지 않는다 – DarkGreiga

+0

무엇이 오류가 –

+0

nvm이라고 말하는가, 나는 그 문제를 해결할 수 있었다. 도와 줘서 고마워! – DarkGreiga

0

에 대한