我想在单独的活动上创建一个ListView(我弹出listview的主要和新活动上的按钮),我将此示例用作模板: https://www.youtube.com/watch?v=pAX8R6UH8CY
我想强调的是,当我做与视频中几乎相同的事情时,它工作正常。这是样本:
public class ShowActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_show);
String[] shoplist = {"bread", "juice", "something"};
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, shoplist));
}
}
但是当我尝试将LinkedList(我在主活动中填充)转换为String []然后在ArrayAdapter中使用它时,ListView没有显示(它不会崩溃 - 只启动空活动)。这是代码:
public class ShowActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_show);
String[] shoplist = new String[MainActivity.NameList.size()];
for (int i=0;i<MainActivity.NameList.size();i++){
shoplist[i]=MainActivity.NameList.get(i);
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.activity_list_item, shoplist));
}
}
这是我的主要活动:
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Buttons declaration
Button btnClickAdd = (Button) findViewById(R.id.btnadd);
btnClickAdd.setOnClickListener(this);
Button btnClickShow = (Button) findViewById(R.id.btnshow);
btnClickShow.setOnClickListener(this);
Button btnClickClear = (Button) findViewById(R.id.btnclear);
btnClickClear.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/** On click actions */
@Override
public void onClick(View v) {
// Declaration of text fields
EditText nameField = (EditText) this.findViewById(R.id.editText1);
EditText valueField = (EditText) this.findViewById(R.id.editText2);
//checking which button has been selected
switch (v.getId()) {
case R.id.btnadd: {
String name = nameField.getText().toString();
String price = valueField.getText().toString();
if(name.length()==0 || price.length()==0) break;
else {
NameList.add(name);
nameField.setText("");
ValueList.add(Double.parseDouble(price));
valueField.setText("");
}
break;
}
case R.id.btnshow: {
//new activity...
Intent intent = new Intent(this, ShowActivity.class);
startActivity(intent);
}
case R.id.btnclear: {
NameList.clear();
ValueList.clear();
break;
}
}
}
/** Instance variables */
//Lists declaration for data storage
public static LinkedList<String> NameList = new LinkedList<String>();
public static LinkedList<Double> ValueList = new LinkedList<Double>();
public static final String KLUCZ = "com.somethingsomething.ShopList.TEXT";
}
有人知道这里发生了什么吗?在此先感谢您的任何帮助
PS这里是xml的,如果有人需要它们: 主:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
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=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="@string/name_"
android:inputType="textMultiLine" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/value_"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.05"
android:gravity="bottom" >
<Button
android:id="@+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add" />
<Button
android:id="@+id/btnshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_all" />
<Button
android:id="@+id/btnclear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/clear_all" />
</LinearLayout>
</LinearLayout>
ListView xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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=".ShowActivit" >
</LinearLayout>