Я пытаюсь реализовать простой список дел, используя это решение Add user input into a ListView on button click, но когда я реализую onCreate решения, я получаю следующие ошибки: http://pastebin.com/9CBCMrjv Я использую фрагменты в этом приложении, и мне интересно, может ли это быть причиной ошибки, поскольку, возможно, конструкторы не относятся к фрагментам.Как реализовать простой список дел с помощью listview?
Может кто-нибудь понять ошибки или где я ошибаюсь в этой реализации? Мое понимание ошибок заключается в том, что это решение не может быть применено к моему классу, поскольку оно связано с фрагментом вместо простой активности.
Это полный класс, чтобы дать вам лучшее понимание проблемы:
public class TopRatedFragment extends Fragment implements OnClickListener {
ListView mListView;
EditText mValue;
Button mAdd;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_top_rated);
mAdd = (Button)findViewById(R.id.newList);
mAdd.setOnClickListener(this);
mValue = (EditText)findViewById(R.id.listData);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the lv variable to your list in the xml
mListView=(ListView)findViewById(R.id.listView);
mListView.setAdapter(adapter);
}
public void onClick(View v)
{
String input = mValue.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}
}
}
Ошибки
The method setContentView(int) is undefined for the type TopRatedFragment
The method findViewById(int) is undefined for the type TopRatedFragment
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (TopRatedFragment)
The method findViewById(int) is undefined for the type TopRatedFragment
The constructor ArrayAdapter<String>(TopRatedFragment, int, ArrayList<String>) is undefined
The method findViewById(int) is undefined for the type TopRatedFragment
Просто собираюсь протестировать это сейчас, надеюсь, это устранит проблему. –
@BrianJ предложение правильное. также убедитесь, что у вас есть правильный импорт. это то, о чем я не упоминал. Остальное все должно быть хорошо – Raghunandan
Да, сработало, может добавить еще несколько функций, например, если я выберу позицию, которую я могу удалить или ввести количество. –