gpt4 book ai didi

java - CustomListView未填充在Android的Fragment中

转载 作者:行者123 更新时间:2023-12-01 10:21:57 25 4
gpt4 key购买 nike

在 fragment (称为VideoCallFragment)启动时,第一个操作是向服务器(Ruby)请求数据并收集数据并填充到 ListView 。为了查看它是否完成,我制作了一个进度对话框和onPostExcute,我使用responseView.textSet(response)。然而,加载完成后数据并未填充。

但是,如果我在相同的布局中创建一个 EditText View (空的),并且如果我单击该 View (尽管我没有将此 EditText View 设为能够在此之后进行处理),则数据将被填充。但很明显,用户不会喜欢这个不必要的过程。这是由 VideoCallFragment 中 AsnyTask 和 rootView 初始化之间的延迟引起的吗?我为这种情况编写了代码。

并且对适配器(listview)也持怀疑态度,我

final ListView lv=(ListView) rootView.findViewById(R.id.listView_classroom);
CustomAdapter adapter = new CustomAdapter(getActivity(), prgmNameList, dateList, caseList);
lv.setAdapter(adapter);
adapter.notifyDatasetChanged();

其中 prgmNameList、dateList、caseList 都是我在 VideoCallFrament 类中作为变量创建的 ArrayList。

但是最后一行不起作用并显示“无法解析方法notifyDatasetChanged();所以我在这里迷失了:(

CustomAdapter 看起来像这样。

public class CustomAdapter extends BaseAdapter{

ArrayList classroom_name_result;
ArrayList date_result;
ArrayList case_result;
String [] tutor_result;

Context context;
int [] imageId;
private static LayoutInflater inflater=null;

public CustomAdapter(Activity mainActivity, ArrayList prgmNameList, ArrayList datelist, ArrayList caselist /*, int[] prgmImages*/) {
// TODO Auto-generated constructor stub

classroom_name_result= prgmNameList;
date_result = datelist;
case_result = caselist;
context=mainActivity;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return classroom_name_result.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public class Holder
{
TextView tv;
TextView date;
TextView mcase;
ImageView img;

}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);

holder.tv=(TextView) rowView.findViewById(R.id.lesson);
holder.tv.setText(classroom_name_result.get(position).toString());

holder.date=(TextView) rowView.findViewById(R.id.date);
holder.date.setText(date_result.get(position).toString());

holder.mcase=(TextView) rowView.findViewById(R.id.course);
holder.mcase.setText(case_result.get(position).toString());
return rowView;
}

}

下面第一个是具有 fragment 的 MainActivity。名为 VideoCallFragment 的 fragment 是未正确填充的类。

public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {

some variables...

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity2);

mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}

@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragmentToLaunch = getFragmentToLaunch(position);
fragmentManager.beginTransaction()
.replace(R.id.container, fragmentToLaunch)
.commit();
}

public void onSectionAttached(int number) {
switch (number) {
case CASE_SECTION_EDIT_PROFILE:
mTitle = getString(R.string.title_section1);
break;
case CASE_SECTION_VIDEO_CALL:
mTitle = getString(R.string.title_section2);
break;
default:
break;
}
}

public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_build_info) {
String versionInfo = "SDK Version: " + sg.com.temasys.skylink.sdk.BuildConfig.VERSION_NAME
+ "\n" + "Sample application version: " + BuildConfig.VERSION_NAME;
Log.d(TAG, versionInfo);
Toast.makeText(this, versionInfo, Toast.LENGTH_LONG).show();
return true;
}

return super.onOptionsItemSelected(item);
}


public Fragment getFragmentToLaunch(int position) {
Fragment fragmentToLaunch = null;
switch (position) {
case CASE_FRAGMENT_EDIT_PROFILE:
fragmentToLaunch = new EditProfileFragment();
break;
case CASE_FRAGMENT_VIDEO_CALL:
fragmentToLaunch = new VideoCallFragment();
break;

default:
break;
}

Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, position + 1);
fragmentToLaunch.setArguments(args);

return fragmentToLaunch;
}
}

视频通话 fragment

public class VideoCallFragment extends Fragment implements LifeCycleListener, MediaListener, RemotePeerListener {
some varaibles.....

private ArrayList<String> prgmNameList = new ArrayList<String>();
private ArrayList<String> caseList = new ArrayList<String>();
private ArrayList<String> dateList = new ArrayList<String>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_video_call, container, false);
parentFragment = (LinearLayout) rootView.findViewById(R.id.ll_video_call);

etRoomName = (EditText) rootView.findViewById(R.id.et_room_name);
etRoomName.setVisibility(View.GONE);

responseView = (TextView) rootView.findViewById(R.id.responseView);

SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(getContext());
String auth_token_string = settings.getString("token", ""/*default value*/);

final ListView lv=(ListView) rootView.findViewById(R.id.listView_classroom);

CustomAdapter adapter = new CustomAdapter(getActivity(), prgmNameList, dateList, caseList);
lv.setAdapter(adapter);


lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String classroom = ((TextView) view.findViewById(R.id.lesson)).getText().toString();
Toast.makeText(getActivity(), classroom, Toast.LENGTH_SHORT).show();
lv.setVisibility(View.GONE);
classroom_enter(classroom);
}
});
new soonClass().execute(auth_token_string);
return rootView;
}


class soonClass extends AsyncTask<String, String, String> {
ProgressDialog pd;
private Exception exception;
protected void onPreExecute() {

pd = new ProgressDialog(getActivity());
pd.setMessage("Logining..");
pd.show();

}

protected String doInBackground(String... args) {


String auth_token_string = args[0];

try {
URL url = new URL("url");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
/* token call */
String urlParameters = "token=" + auth_token_string ;

connection.setRequestMethod("POST");
connection.setDoOutput(true);
DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());

dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();

InputStream in = connection.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(in,"iso-8859-1"),8);
StringBuffer buffer = new StringBuffer();

String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line).append("\n");
}
reader.close();
connection.disconnect();
JSONObject jsonObj = null;

jsonObj = new JSONObject(buffer.toString().trim());

/* status:ok, lessons: xxxx <-- there are two object arrays. */

JSONArray classes = null;
classes = jsonObj.getJSONArray("lessons");

// looping through All Contacts
for (int i = 0; i < classes.length(); i++) {
JSONObject c = classes.getJSONObject(i);

prgmNameList.add("lesson_" + c.getString("id"));
caseList.add(c.getString("course"));
dateList.add(c.getString("date"));
}
return "Load Complete";
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}

protected void onPostExecute(String response) {

if(response == null) {
response = "THERE WAS AN ERROR";
}
//progressBar.setVisibility(View.GONE);
Log.i("INFO", response);
if (pd != null) {
pd.dismiss();
}
responseView.setText(response);
}
}


public void classroom_enter(String room_name) {
somefunction();
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
}


@Override
public void onResume() {
super.onResume();
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(getContext());
String auth_token_string = settings.getString("token", ""/*default value*/);
this.onCreate(null);
}


@Override
public void onDetach() {
//close the connection when the fragment is detached, so the streams are not open.
super.onDetach();
if (skylinkConnection != null && connected) {
skylinkConnection.disconnectFromRoom();
skylinkConnection.setLifeCycleListener(null);
skylinkConnection.setMediaListener(null);
skylinkConnection.setRemotePeerListener(null);
connected = false;
audioRouter.stopAudioRouting(getActivity().getApplicationContext());
}
this.onCreate(null);
}
}

VideoCallFragment 的 XML 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/ll_video_call"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_self_video"
android:orientation="horizontal"
>

<EditText
android:id="@+id/et_room_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Click to Reload"
android:textAppearance="?android:attr/textAppearanceMedium"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mute_audio"
android:id="@+id/toggle_audio"
android:layout_weight="1"
android:visibility="gone"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mute_video"
android:id="@+id/toggle_video"
android:layout_weight="1"
android:visibility="gone"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/responseView" />

</LinearLayout>

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView_classroom"
android:layout_gravity="center_horizontal" />


</LinearLayout>

mainactivity 的 XML 布局

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
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"
tools:context=".MainActivity">

<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->


<fragment
android:id="@+id/navigation_drawer"
android:name="com.example.sungpah.sungpahfirst.NavigationDrawerFragment"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

我尝试提供尽可能详细的信息,以便其他人在答案发布后可以轻松查看和引用!请帮助我!!

最佳答案

However the final line didn't work and says "Cannot resolve method notifyDatasetChanged();

这是因为方法签名是带有大写 S 的 notifyDataSetChanged()

However the data was not populated after loading is done.

这是因为适配器(以及 ListView)不知道其数据已更新。

  • 为您的 VideoCallFragment 提供对适配器的引用:

    private CustomAdapter adapter;
  • 设置此适配器引用

            final ListView lv=(ListView) rootView.findViewById(R.id.listView_classroom);

    adapter = new CustomAdapter(getActivity(), prgmNameList, dateList, caseList);
    lv.setAdapter(adapter);
  • 加载所有数据后,在 AsyncTask 中的适配器上调用 notifyDataSetChanged():

            // looping through All Contacts
    for (int i = 0; i < classes.length(); i++) {
    JSONObject c = classes.getJSONObject(i);

    prgmNameList.add("lesson_" + c.getString("id"));
    caseList.add(c.getString("course"));
    dateList.add(c.getString("date"));
    }

    adapter.notifyDataSetChanged();

    return "Load Complete";

关于java - CustomListView未填充在Android的Fragment中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35539251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com