我有一个html表,其行数与html代码相同:
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.apple.qs.Activity.Activity.ActivityMain_txt;
import com.example.apple.qs.Activity.Activity.ActivityMessageDetails_txt;
import com.example.apple.qs.Activity.db.Constant;
import com.example.apple.qs.Activity.db.model.Message;
import com.example.apple.qs.Activity.db.Store.MessageStore;
import com.example.apple.qs.Activity.widget.CircleTransform;
import com.squareup.picasso.Picasso;
import com.example.apple.qs.Activity.R;
import java.util.ArrayList;
import java.util.List;
public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.ViewHolder> implements Filterable {
private final int mBackground;
private List<Message> original_items = new ArrayList<>();
private List<Message> filtered_items = new ArrayList<>();
private ItemFilter mFilter = new ItemFilter();
private final TypedValue mTypedValue = new TypedValue();
private Context ctx;
private ActivityMain_txt act;
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView title;
public TextView content;
public TextView time;
public ImageView image;
public LinearLayout lyt_parent;
public ViewHolder(View v) {
super(v);
title = (TextView) v.findViewById(R.id.title);
content = (TextView) v.findViewById(R.id.content);
time = (TextView) v.findViewById(R.id.time);
image = (ImageView) v.findViewById(R.id.image);
lyt_parent = (LinearLayout) v.findViewById(R.id.lyt_parent);
}
}
public Filter getFilter() {
return mFilter;
}
// Provide a suitable constructor (depends on the kind of dataset)
public MessageListAdapter(ActivityMain_txt act, Context ctx, List<Message> items) {
this.ctx = ctx;
this.act = act;
original_items = items;
filtered_items = items;
ctx.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
mBackground = mTypedValue.resourceId;
}
@Override
public MessageListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_message_txt, parent, false);
v.setBackgroundResource(mBackground);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
try {
final Message c = filtered_items.get(position);
holder.title.setText(c.contact.getNameOrNumber());
holder.time.setText(Constant.formatTime(c.date));
holder.content.setText(c.snippet);
if (c.contact.photoUri.equals("")) {
holder.image.setImageResource(R.drawable.unknown_avatar);
} else {
Picasso.with(ctx).load(c.contact.photoUri)
.resize(100, 100)
.transform(new CircleTransform())
.into(holder.image);
}
if (!c.read) {
holder.title.setTypeface(null, Typeface.BOLD);
holder.time.setTypeface(null, Typeface.BOLD);
holder.content.setTypeface(null, Typeface.BOLD);
} else {
holder.title.setTypeface(null, Typeface.NORMAL);
holder.time.setTypeface(null, Typeface.NORMAL);
holder.content.setTypeface(null, Typeface.NORMAL);
}
// view detail message conversation
holder.lyt_parent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
act.messageStore.markAsRead(c.threadId);
try {
act.f_message.mAdapter.notifyDataSetChanged();
}catch (Exception e){
}
Intent intent = new Intent(ctx, ActivityMessageDetails_txt.class);
intent.putExtra("obj_conv", c);
ctx.startActivity(intent);
}
});
holder.lyt_parent.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
dialogDeleteMessageConfirm(view, c);
return false;
}
});
}catch (Exception e){
Toast.makeText(ctx, "Ups.., Something error", Toast.LENGTH_SHORT).show();
}
}
private void dialogDeleteMessageConfirm(final View view, final Message c) {
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setTitle("Delete Confirmation");
builder.setMessage("All message from : " + c.contact.getNameOrNumber() + " will be deleted?");
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
boolean result = MessageStore.deleteMessage(ctx, c.threadId + "");
if (result) {
try {
Snackbar.make(view, "Delete success", Snackbar.LENGTH_SHORT).show();
ActivityMain_txt.messageList.remove(c.threadId);
ActivityMain_txt.f_message.mAdapter.notifyDataSetChanged();
} catch (Exception e) {
}
}
}
});
builder.setNegativeButton("Cancel", null);
builder.show();
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
System.out.println(filtered_items);
if(filtered_items.size()==0 || filtered_items==null) {
return 1;
}
else return filtered_items.size();
}
@Override
public long getItemId(int position) {
return filtered_items.get(position).threadId;
}
public int getItemPosition(long threadId){
int pos = -1;
for (int i = 0; i < filtered_items.size(); i++) {
if(filtered_items.get(i).threadId == threadId){
return i;
}
}
return pos;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
String query = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<Message> list = original_items;
final List<Message> result_list = new ArrayList<>(list.size());
for (int i = 0; i < list.size(); i++) {
String str_title = list.get(i).contact.getNameOrNumber();
if (str_title.toLowerCase().contains(query)) {
result_list.add(list.get(i));
}
}
results.values = result_list;
results.count = result_list.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filtered_items = (List<Message>) results.values;
notifyDataSetChanged();
}
}
}
输出:
我想用备用行背景颜色进行着色。像:
每一行都在循环中,例如A,B和C(主循环)。
答案 0 :(得分:0)
添加此CSS
<style>
.style1{
background-color: cyan;
}
.style2{
background-color: blue;
}
</style>
HTML:
echo "<table>";
$select = "select * from table";
$query = mysql_query($select)
$i=0;
while($i < mysql_fetch_array($query))
{
if($i%2==0){
echo "<tr class='style1'>";
}
else{
echo "<tr class='style2'>";
}
echo "<td>A</td>";
echo "</tr>";
$i++;
}
echo "</table>";