为什么我的listview只显示第一项?

时间:2016-04-24 00:54:07

标签: android listview

这是我的Adapter类......你应该知道的另一件事是我有一个onItemClickListener并且它不能工作 我已经做了几乎所有事情而且没有任何想法

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.example.milad.androidproject4.R;
import com.example.milad.androidproject4.model.jenresTO;

import java.util.ArrayList;


public class listAdapter extends BaseAdapter {

Context context;
ArrayList<jenresTO> jenresTOArrayList;

LayoutInflater inflater;


public listAdapter(Context context, ArrayList<jenresTO> jenresTOArrayList) {
    this.context = context;
    this.jenresTOArrayList = jenresTOArrayList;
    inflater = LayoutInflater.from(context);
}


@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int i) {
    return false;
}

@Override
public void registerDataSetObserver(DataSetObserver dataSetObserver) {

}

@Override
public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

}

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

@Override
public Object getItem(int position) {
    return position;
}

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

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {

    final ViewHolder holder;

    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.itemelistemoon, null);
        holder.esmejenre = (TextView) convertView.findViewById(R.id.txtitem);
        holder.aksejenre = (ImageView) convertView.findViewById(R.id.imgitem);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.aksejenre.setImageResource(jenresTOArrayList.get(position).getPics());
    holder.esmejenre.setText(jenresTOArrayList.get(position).getJenres());
    return convertView;
}

private class ViewHolder {
    TextView esmejenre;
    ImageView aksejenre;
}

@Override
public int getItemViewType(int i) {
    return 0;
}

@Override
public int getViewTypeCount() {
    return 1;
}

@Override
public boolean isEmpty() {
    return false;
}

}

这是我的活动课

import android.app.Activity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.example.milad.androidproject4.R;
import com.example.milad.androidproject4.model.jenresTO;
import com.example.milad.androidproject4.page.pagerActivity;

import java.util.ArrayList;


public class listActivity extends Activity {
ArrayList<jenresTO> jenresArraylist = new ArrayList<>();
ListView list1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_activity);


    jenresTO jenres = new jenresTO();
    jenres.setJenres("pop");
    jenres.setPics(R.drawable.miladi);


    jenresTO jenres2 = new jenresTO();
    jenres.setJenres("rock");
    jenres.setPics(R.drawable.miladi);


    jenresTO jenres3 = new jenresTO();
    jenres.setJenres("rap");
    jenres.setPics(R.drawable.miladi);


    jenresTO jenres4 = new jenresTO();
    jenres.setJenres("metal");
    jenres.setPics(R.drawable.miladi);


    jenresTO jenres5 = new jenresTO();
    jenres.setJenres("jazz");
    jenres.setPics(R.drawable.miladi);


    jenresArraylist.add(jenres);
    jenresArraylist.add(jenres2);
    jenresArraylist.add(jenres3);
    jenresArraylist.add(jenres4);
    jenresArraylist.add(jenres5);


    list1 = (ListView) findViewById(R.id.list1);

    listAdapter adapter = new listAdapter(this, jenresArraylist);

    list1.setAdapter(adapter);

    list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            startActivity(new Intent(listActivity.this, pagerActivity.class)
                    .putExtra("listjenres", jenresArraylist)
                    .putExtra("position", position));

        }


    });


}

}

这是xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list1"></ListView>



</LinearLayout>

1 个答案:

答案 0 :(得分:1)

首先开始在jenres2.setJenres,jenres3.setJenres等中更改jenres.setJenres。你只是为jenres而不是2,3,4等分配值。