我正在使用Android应用程序,似乎无法在运行应用程序时显示我的listView。我已经开始使用一些xml,但我仍然经常使用新的和Android更新,所以教程不断变得过时。实际的java代码对我来说似乎很好,我看不出太大的区别那里。我真的可以用一双额外的眼睛,也许有一些我不知道的东西。
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray = {
"Today, colder than bumblefuck",
"Friday, condsider moving",
"Saturday, not even gonna bother",
"Sunday, Partly Cloudy",
"Monday, Hell Froze over",
"Tuesday, Cloudy with a chance of meatballs",
"Wednesday,Light Showers"
};
List<String> weekForeCast = new ArrayList<String>(
Arrays.asList(forecastArray));
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) inflater.inflate(R.layout.fragment_main, container, false).findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview_forecast">
</ListView>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id ="@+id/list_item_forecast_textview"
android:visibility="visible"
android:enabled="false">
</TextView>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.alesterlewis.sunshine.app.MainActivityFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alesterlewis.sunshine.app" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:0)
问题是您在从onCreateView返回时正在充气新视图。因此,您设置适配器和列表视图的那个丢失了。这是错误。
首先在视图中为片段的布局充气
View view = inflater.inflate(R.layout.fragment_main, container, false);
然后初始化它的组件,在这里你可以设置列表视图适配器等,如果你有更多的视图,初始化相同。
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) view.findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
并最终返回最初被编辑和处理的相同视图!
答案 1 :(得分:-1)
您是否为列表设置了适配器?也许你的适配器列表由于某种原因是空的。
<强>更新强>
尝试预测视图创建:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {
"Today, colder than bumblefuck",
"Friday, condsider moving",
"Saturday, not even gonna bother",
"Sunday, Partly Cloudy",
"Monday, Hell Froze over",
"Tuesday, Cloudy with a chance of meatballs",
"Wednesday,Light Showers"
};
List<String> weekForeCast = new ArrayList<String>(
Arrays.asList(forecastArray));
ArrayAdapter<String> bindIt= new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,weekForeCast);
ListView listView = (ListView) view.findViewById(R.id.listview_forecast);
listView.setAdapter(bindIt);
return view;
}