如何获取具有子字符串文本的真实数据文本?

时间:2017-08-24 09:10:12

标签: java android listview substring

我正在创建一个目录电影应用。我有一个ListView,其中包含ImageView,标题,描述和发布日期。

在ListView中,我获取了描述的子字符串,因为如果在ListView中显示它太长了,现在我想在DetailActivity(setOnItemClickListener)中获得真实的描述。

这是我的代码:

try {
    String title = object.getString("title");
    String description = object.getString("overview");
    double movieRatet = object.getDouble("vote_average");
    String movieRate = new DecimalFormat("#.#").format(movieRatet);
    String releaseDate = object.getString("release_date");
    String posterUrl = object.getString("poster_path");
    posterUrl = POSTER_BASE_URL + "w185" + posterUrl;
    description = description.length() > 64 ? description.substring(0,64)+"...":description;

    Log.d("movie poster", posterUrl);
    Log.d("movie title ", title);
    Log.d("movie description ", description);
    Log.d("movie release ", releaseDate);

    this.title = title;
    this.description = description;
    this.rate = releaseDate;
    this.imgurl = posterUrl;


}catch (Exception e){

    e.printStackTrace();
}

这是我的OnItemClickListener

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getApplicationContext(), "Clicked"+position,Toast.LENGTH_LONG).show();


            RelativeLayout listItem = (RelativeLayout) view.findViewById(R.id.rl_item);
            TextView clickedItemView = (TextView) listItem.findViewById(R.id.tv_judul);
            TextView clickedItemView2 = (TextView) listItem.findViewById(R.id.tv_deskripsi);
            TextView clickedItemView3 = (TextView) listItem.findViewById(R.id.tv_rate);
            String title = clickedItemView.getText().toString();
            String desk = clickedItemView2.getText().toString();
            String rate = clickedItemView3.getText().toString();
            Intent i = new Intent(getApplicationContext(), DetailActivity.class);
            desk.substring(0);
            i.putExtra("title", title);
            i.putExtra("desk", desk);
            i.putExtra("rate", rate);
            startActivity(i);

        }

    });

DetailActivity的图片:

picture of detailactivity

我想得到完整的描述,怎么样?

4 个答案:

答案 0 :(得分:1)

你可以添加一个params&#34; fullMessage&#34;存储所有消息,&#34;描述&#34;存储一些消息。然后使用i.putExtra(&#34; desk&#34;,fullMessage)将完整的消息传送到另一个活动。

另一种解决方案是将这些属性添加到ListView中的TextView

 <TextView
 <!--  other value -->
 android:maxLines="3"
 android:ellipsize="end"/>

您可以将完整的消息发送到此TextView,当消息太长时它会自动折叠。

答案 1 :(得分:0)

只是不要覆盖你的描述变量。而不是上面还有一个包含截断值的变量,比如概述。

String overview = description.length() > 64 ? description.substring(0,64)+"...":description;

然后您的描述变量将继续保持原始值。只需在您需要的地方使用它。

答案 2 :(得分:0)

试试这个:

mymodalclass:make class

public class MyModal {

    String title ;
    String description ;
    double movieRatet ;
    String movieRate;
    String releaseDate;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public double getMovieRatet() {
        return movieRatet;
    }

    public void setMovieRatet(double movieRatet) {
        this.movieRatet = movieRatet;
    }

    public String getMovieRate() {
        return movieRate;
    }

    public void setMovieRate(String movieRate) {
        this.movieRate = movieRate;
    }

    public String getReleaseDate() {
        return releaseDate;
    }

    public void setReleaseDate(String releaseDate) {
        this.releaseDate = releaseDate;
    }

    public String getPosterUrl() {
        return posterUrl;
    }

    public void setPosterUrl(String posterUrl) {
        this.posterUrl = posterUrl;
    }

    String posterUrl ;
}

// ***********

while parsing data :

Arraylist list = new ArrayList();

for(int i=0;i<yourjsonarray.length();i++){
JSONObject object = yourjsonarray.getJSONObject(i);
MyModal m = new MyModal();
m.setTitle(object.getString("title"));//like this set all data
//now add this object to list
list.add(m);

}




//now when you click list just get clicked position and by this position get that particular object and pass that to next activity
ex: position = your_clicked_position
MyModal clicked_modal_object = (MyModal) list.get(your_clicked_position);

//now pass this clicked_modal_object  to next activity

答案 3 :(得分:0)

你可以使用两个变量。(我确定这是@Gautam提到的)第一个是完整描述,第二个是较短的描述。

  String description = object.getString("overview"); // full
  String shortDesc = description.length() > 64 ? description.substring(0,64)+"...":description; // add this to listView
  Log.d("movie description ", shortDesc);
  

我想在DetailActivity中获得真实的描述   (setOnItemClickListener)

点击按钮后,获取说明string而不是

i.putExtra("desk", description);

注意:请确保将说明设置为全局。