mainActivity.java
SELECT id FROM books WHERE book_author=$book_author AND book_category=$book_category
ListView_data.java
public class MainActivity extends AppCompatActivity {
ListView listView;
ListViewAdaptor adapter;
TextView txtname1,txtrank1;
RelativeLayout relativeLayout;
Context context;
ArrayList<String> name;
ArrayList<Integer> rank;
int highestRank = 39;
public MainActivity() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listview);
relativeLayout = (RelativeLayout) findViewById(R.id.rlFloter);
txtname1 = findViewById(R.id.txtName);
txtrank1 = findViewById(R.id.txtRank);
name = new ArrayList<>();
rank = new ArrayList<>();
for(int i=1 ; i<40 ; i++){
name.add("Winit Employer"+i);
rank.add(i); }
txtname1.setText("Winit Employer39");
txtrank1.setText("39");
adapter = new ListViewAdaptor(this,name,rank);
listView.setAdapter(adapter);
}
public void visible() {
txtname1.setVisibility(View.VISIBLE);
txtrank1.setVisibility(View.VISIBLE);
}
public void gone() {
txtname1.setVisibility(View.GONE);
txtrank1.setVisibility(View.GONE);
}
public void toast() {
Toast.makeText(context,"Toast",Toast.LENGTH_LONG).show();
}
/////////////
Logcat
public class ListViewAdaptor extends BaseAdapter {
private final Context context;
ArrayList<String> name;
ArrayList<Integer> rank;
Integer highestRank = 39;
MainActivity ma = (MainActivity) getActivity();
public ListViewAdaptor(Context context,ArrayList<String> name, ArrayList<Integer> rank) {
this.name = name;
this.rank = rank;
this.context=context;
}
@Override
public int getCount() {
return name.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.listview_data, null);
TextView txtname = (TextView) rowView.findViewById(R.id.txtName);
TextView txtRank = (TextView) rowView.findViewById(R.id.txtRank);
txtname.setText(name.get(i));
txtRank.setText(rank.get(i).toString());
main.visible();//hear it shows error
if(highestRank == rank.get(i)){
main.gone();
main.toast();
}
return rowView;
}
}
答案 0 :(得分:1)
变量context
未初始化。
删除变量,并在活动吐司方法中使用this
代替
Toast.makeText(this,"Toast",Toast.LENGTH_LONG).show();
答案 1 :(得分:1)
有两种解决方法,一种是@SamuelRobert建议的,另一种是添加
context = MainActivity.this
位于MainActivity的onCreate方法中。