如何在单击按钮时更改列表项的背景颜色?

时间:2019-04-22 13:31:52

标签: java android listview baseadapter

我有一个ListView和一个CustomAdapter。所有元素均已成功加载到列表中。现在,我想通过单击外部按钮来更改列表中某个元素的背景色。但是我不知道如何访问列表中的特定项目。

这是CustomAdapter类:

var Players = [];
var list = [];
var count = 0;

// So the code below organizes players by stats. 
// For example, if you call touchdowns, it'll organize from most touchdowns to lowest.
function myFunction(Position, Stat, startingpoint, endingpoint) {
  readRecords(Position, {}, function(records) {
    var temp = 0;
    var name = "";
    for (var i = 0; i < records.length; i++) {
      for (var j = i + 1; j < records.length; j++) {
        if ((records[i])[Stat] < records[j][Stat]) {
          temp = records[i][Stat];
          name = records[i].Player;
          (records[i])[Stat] = (records[j])[Stat];
          records[i].Player = records[j].Player;
          records[j][Stat] = temp;
          records[j].Player = name;
        }
      }
      insertItem(Players, i, (records[i]).Player + " : " + (records[i])[Stat] + " " + Stat + "\n");
    }
    for (var k = startingpoint; k < endingpoint; k++) {
      appendItem(list, Players[k]);
      var x = list.join(" ");
    }

    setText("label2", x);
    for (var l = endingpoint; l >= startingpoint; l--) {
      removeItem(list, Players[l]);
    }
  });

}

// So I made this part cause I thought let's say I had 60 players, and 20 players per page. 
// How would the program know if I wanna see players 0-20 or 20-40 if I click previous? 
// It's to organize that based on a count variable that changes when you click back and forward.
onEvent("button1", "click", function() {
    count = count + 1;
});

onEvent("button2", "click", function() {
    count = count - 1;
});

// And this was supposed to call back my original function to organize stats when I click the next or previous button. 
// Guess this is where my problem is.
function callback(Stats) {
    count = 0;
    setText("label2", "");
    onEvent("button1", "click", function() {
        if (count == 1) {
            myFunction("Quarterbacks", Stats, 40, 69);
        }
    });

    onEvent("button2", "click", function() {
        if (count == 0) {
            myFunction("Quarterbacks", Stats, 0, 40);
        }
    });
}

// Quarterback stats
onEvent("QuarterBackmenu", "change", function() {
    setScreen("OffenseStatScreen");

    if (getText("QuarterBackmenu") == "Touchdowns") {
        setText("label2", "");
        myFunction("Quarterbacks", "Touchdowns", 0, 40);
        setText("QuarterBackmenu", "Quarterback");
        callback("Touchdowns");
    } else if ((getText("QuarterBackmenu") == "Completions")) {
        setText("label2", "");
        myFunction("Quarterbacks", "Completions", 0, 40);
        setText("QuarterBackmenu", "Quarterback");
        callback("Completions");
    }
});

2 个答案:

答案 0 :(得分:1)

您可以在getView()方法中做到这一点

view.setOnClickListener(new OnClickListener() 
   { 
     @Override
     public void onClick(View v)
         { 
           view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));
          }
   });

如果视图上有一个按钮,则对该按钮执行侦听器

如果您想从父活动中获取选定的项目视图,则:

yourlistview.setOnItemClickListener(new AdapterView.OnItemClickListener() 
{ 
 @Override 
  public void onItemClick(AdapterView<?>     parent,View view, int position, long id) 
    { 
       selectedposition = position ;
     }
  });


  View view = listView.getAdapter().getView(selectedposition,null,listview);

然后更改背景:

view.setBackgroundColor(ContextCompat.getColor(this, R.color.yourcolor));

请在color.xml文件中定义颜色

如果您的视图不止一个,则创建一个ArrayList<View>并进行一些循环

答案 1 :(得分:0)

在您的活动和您的活动中创建一个自定义侦听器界面     适配器将实现此功能。

public interface OnClickListenerFromActivity {
    void onActivityButtonClick(int position);
}

在单击按钮时调用侦听器的方法

    mOnClickListenerFromActivity.onActivityButtonClick(mList.getItem(yourPostion));

将此侦听器实现到您的适配器中

public class CustomAdapter extends BaseAdapter implements Activity.OnClickListenerFromActivity {

private Context ctx;
private int resource;
private List<ItemModel> items;


public PreorderListAdapter(Context context, int resource, List<ItemModel> items){
    this.ctx = context;
    this.resource = resource;
    this.items = items;
}

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

@Override
public ItemModel getItem(int position) {
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@NonNull
@Override
public View getView(int i, View convertView, @NonNull ViewGroup parent) {

    View view = convertView;

    if(view == null){
        LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(resource, null);
    }


    TextView text1 = (TextView) view.findViewById(R.id.text1);
    TextView text2 = (TextView) view.findViewById(R.id.text2);
    TextView text3 = (TextView) view.findViewById(R.id.text3);

    ItemModel item = items.get(i);

    text1.setText(item.getName());
    text2.setText(item.getOption2());
    text3.setText(item.getOption3());

    return view;
}

public void onActivityButtonClick(int position) {
    // get your item through position and
    // set your color here
}

}