맞춤 ArrayAdapter을 사용하여 항목 목록을 표시하고 싶습니다.Android CustomListAdapter notifydatachange()가 작동하지 않습니다.
버튼을 클릭하여 데이터를 삽입합니다. 다음과 같이
MainFragment.java
@Override
public void onClick(View v) {
DatabaseClass feed = new DatabaseClass(getActivity());
new DatabaseClass(getActivity()).getList();
new MyFragment().updateList();
}
내 레이아웃 파일입니다.
main.xml에
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/update"
android:name="com.android.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MyFragment.java
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyFragment extends ListFragment {
// creating database reference
private static DatabaseClass feed;
private List<CustomModel> customList;
private static CustomListAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
feed = new DatabaseClass(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// To get List of CustomModel objects
feed.getList();
// CUSTOM_LIST is list constant
adapter = new CustomListAdapter(getActivity(),
R.layout.update, Utils.CUSTOM_LIST);
// setting adapter
setListAdapter(adapter);
View view = inflater.inflate(R.layout.update, container, false);
return view;
}
@Override
public void onStart() {
super.onStart();
}
// this method is called after inserting data into database
public void updateList() {
if (adapter != null) {
adapter.notifyDataSetChanged();
} else {
adapter = new CustomListAdapter(getActivity(),
R.layout.update, Utils.CUSTOM_LIST);
adapter.notifyDataSetChanged();
}
}
private class CustomListAdapter extends ArrayAdapter<CustomModel> {
private final Context context;
private final List<CustomModel> list;
private TextView name, message;
public CustomListAdapter(Context context, int resource,
List<CustomModel> list) {
super(context, resource, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.single_row, parent,
false);
name = (TextView) view.findViewById(R.id.name);
message = (TextView) view.findViewById(R.id.message);
CustomModel obj = list.get(position);
name.setText(obj.getName());
message.setText(obj.getMessage());
return view;
}
}
}
그리고 MyFragment.java에 대한 레이아웃 파일입니다
update.xml을
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@android:id/list"
/>
내 데이터베이스 클래스는
DatabaseClass.java 버튼의
public void getList(){
ourDatabase = this.getReadableDatabase();
String sql = "SELECT * FROM " + DATABASE_TABLE;
Cursor c = ourDatabase.rawQuery(sql, null);
int iName = c.getColumnIndex(KEY_NAME);
int iMessage = c.getColumnIndex(KEY_MESSAGE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
CustomModel model = new CustomModel();
model.setName(c.getString(iFromName));
model.setMessage(c.getString(iMessage));
Utils.LIST.add(model);
}
}
온 클릭하고, 데이터는 DB에 삽입됩니다. UI에는 반영되지 않습니다. 내 목록보기가 업데이트되지 않는 이유는 무엇입니까?
private class CustomListAdapter extends ArrayAdapter<CustomModel> {
private final Context context;
private final List<CustomModel> list;
private TextView name, message;
public CustomListAdapter(Context context, int resource,
List<CustomModel> list) {
super(context, resource, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.single_row, parent, false);
name = (TextView) view.findViewById(R.id.name);
message = (TextView) view.findViewById(R.id.message);
CustomModel obj = list.get(position);
name.setText(obj.getName());
message.setText(obj.getMessage());
return view;
}
public void updateList(List<CustomModel> list) {
this.list = list;
notifyDataSetChanged();
}
}
그래서 대신 notifyDataSetChanged();
전화를 호출하는 활동에서 notifyDataSetChanged();
그래서 방법을 어댑터 자체에 메소드 updateList을 추가하고 전화를 다음과 같이
감사
* 왜 내 목록보기가 업데이트되지 않습니까? * - 어떤 이상한 이유로 그 버튼을 클릭 할 때마다 전혀 사용되지 않는 인스턴스 인 MyFragment의 ** 새 ** 인스턴스가 만들어지기 때문에 . 그 대신에, FragmentManager'를 통해 메인 레이아웃에서 프래그먼트에 대한 참조를 얻고 그 인스턴스에서'updateList()'를 호출 할 필요가 있습니다. – Luksprog
어댑터는'Utils.CUSTOM_LIST'리스트를 기반으로하지만'getList()'메소드에서는'Utils.LIST'리스트를 업데이트합니다. 어떤거야? – Luksprog
두 상수는 모두 'Utils.LIST'입니다. 내 오타를 유감스럽게 생각합니다. 나는 당신이 제안한대로 이것을 시도했다. 'FragmentManager 관리자 getFragmentManager =()'' MyFragment 단편 = (MyFragment) getFragmentManager() findFragmentById (R.id.update)].'' fragment.updateList manager.beginTransaction =()'' 하여 FragmentTransaction 트랜잭션();' 'transaction.commit();' 하지만 작동하지 않았습니다. –