2017-02-11 12 views
-2

私はアンドロイドプログラミングで大学のプロジェクトに取り組んでいますが、現在のところUIデザインのみです。 ListViewを含むアクティビティには、ListView以外のすべてが表示されます。私は、アダプタがリストに設定される前と後にテキストを追加するコードを入れてみましたが、何も動作していません。私はアダプタのカウントのログを使用しようとしましたが、正しい時刻に正しい数値を返します(追加する前に0、追加後に5)。Androidリストビューには何も表示されません

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を変更していません。

どのように私は間違っていましたか?

+0

@PavneetSinghは動作しません、でもR.id.listは、赤い文字である(解決できない)私はあまりにもことを試みたアダプタ – DarkGreiga

+0

それはまだ動作しません –

+0

@MehmetKologluのインスタンス化する前にリスト部分に追加を移動 – DarkGreiga

答えて

0

はあなたのリストビューandroid:layout_belowandroid:layout_above属性から+を削除adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,listProducts);

+0

それもコンパイルされません – DarkGreiga

+0

何がエラー –

+0

nvmと言っている、私は問題を解決することができた。助けてくれてありがとう! – DarkGreiga

0

のためにこれを使用してください。

<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"/> 
関連する問題