Я работаю над проектом колледжа с программированием на Android, но в настоящее время только на дизайне пользовательского интерфейса. Активность, содержащая 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>
Я только работаю над пользовательским интерфейсом, поэтому не буду создавать пользовательский адаптер. Я не могу использовать ListActivity, потому что мне нужна панель действий для показа, и мне нужно поддерживать согласованность в кодах, поэтому я не изменяю RelativeLayout.
Любая идея, как я сделал не так?
@PavneetSingh не работает, даже R.id.list находится на красные буквы (не может быть решена) – DarkGreiga
переместить добавление в список части до конкретизации адаптера –
@MehmetKologlu Я попробовал это тоже, он все еще не работает – DarkGreiga