Android列表如何查看项目在单击数据库时转到额外的详细信息页面

时间:2016-04-05 17:37:30

标签: android android-intent

我创建了一个应用程序,我的数据库中有不同的活动,这些活动显示在我自己制作的自定义列表视图中,其中显示了用户的基本活动信息。我已将onItemClickListener添加到列表视图中,因此它将用户带到“详细信息”页面,我希望从该页面显示数据库中的更多信息。意图的作用是将用户发送到新活动,我也在我的意图中使用了putExtra函数来发送列表视图点击的位置,并在新页面上以吐司形式出现该位置以测试它是否正常工作。一切都很好,但现在我试图从数据库中获取信息。我试过看了许多不同的教程,但没有任何工作,我想如果我可以在putExtra中获取活动的标题,那么我将能够显示来自活动表的东西,其中putExtra标题信息=表格中的标题。但我似乎无法从表中获取标题并将其放入putExtra函数中。我正在使用Sugar ORM数据库。

这是我在ViewData类上显示列表视图的地方:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_data);
    final ListView listView = (ListView) findViewById(R.id.viewAll_listview);
    long count = Activitytable.count(Activitytable.class);
    if(count>0) {
        Activitytable.listAll(Activitytable.class);
        final List<Activitytable> activitytable = Activitytable.listAll(Activitytable.class);
        ViewAllListView madapter = new ViewAllListView(getApplicationContext(), activitytable);
        listView.setAdapter(madapter);
        listView.setClickable(true);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?>parent, View v, int position, long id) {

                Object o = getItem(position);
                Activitytable AT = Activitytable.findById(Activitytable.class,activitytable.get(position).getId());
                Intent i = new Intent(getApplicationContext(),Details.class);
                i.putExtra("listPosition",position);
                startActivity(i);

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

    }
        else

            {
                Toast.makeText(getApplicationContext(), "No Data Available in Table", Toast.LENGTH_LONG);
            }
        }

这是我的自定义列表视图类ViewAllListView:

public class ViewAllListView extends BaseAdapter {

public Context tcontext;
private LayoutInflater inflater;
public ViewHolder holder=null;
private List<Activitytable> activitytable = new ArrayList<>();

ViewAllListView(Context context, List<Activitytable> values)
{
    this.tcontext = context;
    this.activitytable = values;
    this.inflater = LayoutInflater.from(context);
}


public int getCount(){
    return activitytable.size();
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent){
    if(convertView == null){
        convertView=inflater.inflate(R.layout.layout_for_listview_show,null);
        holder = new ViewHolder();
        holder.title = (TextView) convertView.findViewById(R.id.title_textview);
        holder.Overall_rating = (TextView) convertView.findViewById(R.id.overallrating_textview);
        holder.Category = (TextView) convertView.findViewById(R.id.category_textview);
        holder.Average_price = (TextView) convertView.findViewById(R.id.averageprice_textview);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder)convertView.getTag();
    }
    holder.title.setText(this.activitytable.get(position).Title);
    holder.Overall_rating.setText(this.activitytable.get(position).Overall_rating);
    holder.Category.setText(this.activitytable.get(position).Category);
    holder.Average_price.setText(this.activitytable.get(position).Average_price);
    return convertView;
}
static class ViewHolder
{
    TextView title, Overall_rating, Category, Average_price;
}
}

到目前为止,这是我在Details类中的内容:

public class Details extends AppCompatActivity {

TextView titletext;

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

    titletext = (TextView) findViewById(R.id.titletext);

    String data = getIntent().getSerializableExtra("listPosition").toString();

    Toast.makeText(getBaseContext(),String.valueOf(data),Toast.LENGTH_LONG).show();



}
}    

1 个答案:

答案 0 :(得分:0)

您可能需要考虑这些变化:

ViewAllListView.java (您需要返回该项才能访问它)

 public class ViewAllListView extends BaseAdapter {

  .....

  public Object getItem(int position) {
     //instead of return position;
     return activitytable.get(position); 
  }

  .....
}

ViewData.java (您可以检索项目,将其投射到Activitytable对象中)并获取名称)

 @Override
 protected void onCreate(Bundle savedInstanceState) {  

       ......
       public void onItemClick(AdapterView<?>parent, View v, int position, long id) {

            Activitytable o = (Activitytable) getItem(position);
            //you can retrieve the name from object o 
            i.putExtra("listName", o.getName());

       ......
 }

让我知道它是否有帮助!快乐的节目!