gpt4 book ai didi

java - 向 ListView 行中的每个项目添加按钮时出现错误

转载 作者:行者123 更新时间:2023-12-02 03:51:18 25 4
gpt4 key购买 nike

OneFragment.java

package com.pixalstudio.musicadda;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class OneFragment extends android.support.v4.app.Fragment {


public OneFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);


ArrayList<String> songs = new ArrayList<String>();

List<String> Songs = Arrays.asList(getResources().getStringArray(R.array.songs));

// list.add("item1");
// list.add("item2");

MyCustomAdapter adapter = new MyCustomAdapter(songs, getActivity());

ListView listView = (ListView) view.findViewById(R.id.listView_fragone);
listView.setAdapter(adapter);
return view;
}



/*@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_one, container, false);

ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");

MyCustomAdapter adapter = new MyCustomAdapter(list, this);

ListView listView = (ListView) getView().findViewById(R.id.listView_fragone);
listView.setAdapter(adapter);
return view;
}*/


}

我在该文件上遇到错误。

1

Error:(43, 13) error: <identifier> expected
Error:(43, 14) error: illegal start of type
Error:(44, 13) error: <identifier> expected
Error:(44, 14) error: illegal start of type
Error:(49, 24) error: <identifier> expected
Error:(49, 32) error: <identifier> expected

[这是 FragmentOne.java,我在其中遇到错误。]

MyCustomAdapter.java

    package com.pixalstudio.musicadda;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

/**
* Created by akkie on 3/7/2016.
*/
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {

private ArrayList<String> list = new ArrayList<String>();
private Context context;

public MyCustomAdapter(ArrayList<String> list, Context context) {

this.list = list;
this.context = context;

}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int pos) {
return list.get(pos);
}

@Override
public long getItemId(int pos) {
return 0;
//just return 0 if your list items do not have an Id variable.
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.cust_layout, null);
}

//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));

//Handle buttons and add onClickListeners
Button deleteBtn = (Button)view.findViewById(R.id.delete_btn);
Button addBtn = (Button)view.findViewById(R.id.add_btn);

deleteBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
list.remove(position); //or some other task
notifyDataSetChanged();
}
});
addBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
//do something
notifyDataSetChanged();
}
});

return view;
}
}

添加了 MyCustomAdapter.java,请检查它并让我知道是否需要进行更改。

Updated FragmentOne.java

最佳答案

尝试将代码放入 onCreateView 方法中。

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

...

ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");

MyCustomAdapter adapter = new MyCustomAdapter(list, this);

ListView listView = (ListView) getView().findViewById(R.id.listView_fragone);
listView.setAdapter(adapter);

...
}

关于java - 向 ListView 行中的每个项目添加按钮时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35850026/

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