我创建的应用程序显示有关电影的信息。现在,问题是每当用户点击listview项目时,我都无法设置标记或将标题设为粗体。因此,用户可以知道他/她观看了哪些电影 我在getView()方法中添加了boolean变量,但这没有帮助解决问题 这是我的应用的screenshot:
MainActivity.java
import android.content.Intent;
import android.graphics.Movie;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
CustomAdapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int[] images = {R.drawable.dfg,R.drawable.godf,R.drawable.godf2,R.drawable.thedark,R.drawable.twelv,R.drawable.shindl};
String[] movie_titles = getResources().getStringArray(R.array.Movie_titles);
String[] movie_ratings=getResources().getStringArray(R.array.Movie_ratings);
String[] movie_janrs=getResources().getStringArray(R.array.Movie_janrs);
int[] arrows = {R.drawable.hjk,R.drawable.hjk,R.drawable.hjk,R.drawable.hjk,R.drawable.hjk,R.drawable.hjk};
setContentView(R.layout.activity_main);
ListView mylist = (ListView) findViewById(R.id.list);
myadapter = new CustomAdapter(this,images,movie_titles,movie_ratings,movie_janrs,arrows);
if(mylist!=null){
mylist.setAdapter(myadapter);
}
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==0){
Intent movie1 = new Intent(MainActivity.this,MovieOne.class);
startActivity(movie1);
}
}
});
}
}
CustomAdapter.java
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
static LayoutInflater vi=null;
int[] images;
String[] movie_titles;
String[] movie_ratings;
String[] movie_janrs;
int[] arrows;
public CustomAdapter(Context context, int images[], String movie_titles[], String movie_ratings[], String movie_janrs[],int arrows[]){
this.context = context;
this.images=images;
this.movie_titles=movie_titles;
this.movie_ratings=movie_ratings;
this.movie_janrs=movie_janrs;
this.arrows=arrows;
}
@Override
public int getCount() {
return movie_titles.length;
}
@Override
public Object getItem(int position) {
return getItemId(position);
}
@Override
public long getItemId(int position) {
return position;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if(convertView ==null){
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=vi.inflate(R.layout.custom_layout,null);
}
if(convertView!=null){
ImageView tvImage = (ImageView) convertView.findViewById(R.id.one);
TextView tvTittle = (TextView) convertView.findViewById(R.id.two);
TextView tvRating = (TextView) convertView.findViewById(R.id.three);
TextView tvJanr = (TextView) convertView.findViewById(R.id.four);
ImageView tvImage2 = (ImageView) convertView.findViewById(R.id.six);
if(tvImage!=null && tvTittle!=null && tvRating!=null && tvJanr!=null){
tvImage.setImageResource(images[position]);
tvTittle.setText(movie_titles[position]);
tvRating.setText(movie_ratings[position]);
tvJanr.setText(movie_janrs[position]);
tvImage2.setImageResource(arrows[position]);
}
}
return convertView;
}
}
MovieOne.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MovieOne extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movie_one);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onSupportNavigateUp() {
finish();
return true;
}
}
答案 0 :(得分:1)
对于列表项单击或读取,您必须保持项目单击的位置。
OnItemClickListener listViewOnItemClick = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View arg1, int position, long id) {
mSelectedItem = position;
mAdapter.notifyDataSetChanged();
}
};
并在您的getView()
方法中:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view = View.inflate(context, R.layout.item_list, null);
if (position == mSelectedItem) {
// set your color
}
return view;
}
快乐的编码!!
答案 1 :(得分:1)
创建长度为movie_titles的'clicked'布尔类型数组;将所有项初始化为false并将其传递给适配器。单击项目时,在getView中,获取其位置并将单击的项目设置为true